1 // Copyright (C) 2017-2021 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef HTTP_CONNECTION_POOL_H
8 #define HTTP_CONNECTION_POOL_H
9 
10 #include <http/connection.h>
11 
12 #include <list>
13 #include <mutex>
14 
15 namespace isc {
16 namespace http {
17 
18 /// @brief Pool of active HTTP connections.
19 ///
20 /// The HTTP server is designed to handle many connections simultaneously.
21 /// The communication between the client and the server may take long time
22 /// and the server must be able to react on other events while the communication
23 /// with the clients is in progress. Thus, the server must track active
24 /// connections and gracefully close them when needed. An obvious case when the
25 /// connections must be terminated by the server is when the shutdown signal
26 /// is received.
27 ///
28 /// This object is a simple container for the server connections which provides
29 /// means to terminate them on request.
30 class HttpConnectionPool {
31 public:
32 
33     /// @brief Start new connection.
34     ///
35     /// The connection is inserted to the pool and the
36     /// @ref HttpConnection::asyncAccept is invoked.
37     ///
38     /// @param connection Pointer to the new connection.
39     void start(const HttpConnectionPtr& connection);
40 
41     /// @brief Removes a connection from the pool and shutdown it.
42     ///
43     /// Shutdown is specific to TLS and is a first part of graceful close (note it is
44     /// NOT the same as TCP shutdown system call).
45     ///
46     /// @note if the TLS connection stalls e.g. the peer does not try I/O
47     /// on it the connection has to be explicitly stopped.
48     ///
49     /// @param connection Pointer to the connection.
50     void shutdown(const HttpConnectionPtr& connection);
51 
52     /// @brief Removes a connection from the pool and stops it.
53     ///
54     /// @param connection Pointer to the connection.
55     void stop(const HttpConnectionPtr& connection);
56 
57     /// @brief Stops all connections and removes them from the pool.
58     void stopAll();
59 
60 protected:
61 
62     /// @brief Stops all connections and removes them from the pool.
63     ///
64     /// Must be called from with a thread-safe context.
65     void stopAllInternal();
66 
67     /// @brief Set of connections.
68     std::list<HttpConnectionPtr> connections_;
69 
70     /// @brief Mutex to protect the internal state.
71     std::mutex mutex_;
72 };
73 
74 }
75 }
76 
77 #endif
78 
79