diff options
Diffstat (limited to 'src/mongo/client/connection_pool.h')
-rw-r--r-- | src/mongo/client/connection_pool.h | 262 |
1 files changed, 132 insertions, 130 deletions
diff --git a/src/mongo/client/connection_pool.h b/src/mongo/client/connection_pool.h index cce69e03c22..211d77d4b06 100644 --- a/src/mongo/client/connection_pool.h +++ b/src/mongo/client/connection_pool.h @@ -40,167 +40,169 @@ namespace mongo { +/** + * Represents a pool of connections to a MongoDB server. The pool is synchronized internally + * and its methods can be called from multiple threads, however once a connection is obtained + * it should only be used from one thread in accordance with the rules of DBClientInterface. + */ +class ConnectionPool { + MONGO_DISALLOW_COPYING(ConnectionPool); + +public: /** - * Represents a pool of connections to a MongoDB server. The pool is synchronized internally - * and its methods can be called from multiple threads, however once a connection is obtained - * it should only be used from one thread in accordance with the rules of DBClientInterface. + * Information about a connection in the pool. */ - class ConnectionPool { - MONGO_DISALLOW_COPYING(ConnectionPool); - public: - /** - * Information about a connection in the pool. - */ - struct ConnectionInfo { - ConnectionInfo() : conn(NULL) {} - ConnectionInfo(DBClientConnection* theConn, Date_t date) - : conn(theConn), - creationDate(date) {} + struct ConnectionInfo { + ConnectionInfo() : conn(NULL) {} + ConnectionInfo(DBClientConnection* theConn, Date_t date) + : conn(theConn), creationDate(date) {} - // A connection in the pool. - DBClientConnection* const conn; + // A connection in the pool. + DBClientConnection* const conn; - // The date at which the connection was created. - Date_t creationDate; - }; + // The date at which the connection was created. + Date_t creationDate; + }; - typedef stdx::list<ConnectionInfo> ConnectionList; - typedef unordered_map<HostAndPort, ConnectionList> HostConnectionMap; - typedef std::map<HostAndPort, Date_t> HostLastUsedMap; + typedef stdx::list<ConnectionInfo> ConnectionList; + typedef unordered_map<HostAndPort, ConnectionList> HostConnectionMap; + typedef std::map<HostAndPort, Date_t> HostLastUsedMap; - /** - * RAII class for connections from the pool. To use the connection pool, instantiate one of - * these with a pointer to the pool, the identity of the target node and the timeout for - * network operations, use it like a pointer to a connection, and then call done() on - * successful completion. Failure to call done() will lead to the connection being reaped - * when the holder goes out of scope. - */ - class ConnectionPtr { - MONGO_DISALLOW_COPYING(ConnectionPtr); - public: - /** - * Constructs a ConnectionPtr referring to a connection to "target" drawn from "pool", - * with the network timeout set to "timeout". - * - * Throws DBExceptions if the connection cannot be established. - */ - ConnectionPtr(ConnectionPool* pool, - const HostAndPort& target, - Date_t now, - Milliseconds timeout); - - /** - * Destructor reaps the connection if it wasn't already returned to the pool by calling - * done(). - */ - ~ConnectionPtr(); - - /** - * Obtains the underlying connection which can be used for making calls to the server. - */ - DBClientConnection* get() const { return _connInfo->conn; } - - /** - * Releases the connection back to the pool from which it was drawn. - */ - void done(Date_t now); - - private: - ConnectionPool* _pool; - const ConnectionList::iterator _connInfo; - }; - + /** + * RAII class for connections from the pool. To use the connection pool, instantiate one of + * these with a pointer to the pool, the identity of the target node and the timeout for + * network operations, use it like a pointer to a connection, and then call done() on + * successful completion. Failure to call done() will lead to the connection being reaped + * when the holder goes out of scope. + */ + class ConnectionPtr { + MONGO_DISALLOW_COPYING(ConnectionPtr); + public: /** - * Instantiates a new connection pool with the specified tags to be applied to the - * messaging ports that it opens. + * Constructs a ConnectionPtr referring to a connection to "target" drawn from "pool", + * with the network timeout set to "timeout". * - * @param messagingPortTags tags to be applied to the messaging ports for each of the - * connections. If no tags are required, use 0. + * Throws DBExceptions if the connection cannot be established. */ - ConnectionPool(int messagingPortTags); - ~ConnectionPool(); + ConnectionPtr(ConnectionPool* pool, + const HostAndPort& target, + Date_t now, + Milliseconds timeout); /** - * Acquires a connection to "target" with the given "timeout", or throws a DBException. - * Intended for use by ConnectionPtr. + * Destructor reaps the connection if it wasn't already returned to the pool by calling + * done(). */ - ConnectionList::iterator acquireConnection(const HostAndPort& target, - Date_t now, - Milliseconds timeout); + ~ConnectionPtr(); /** - * Releases a connection back into the pool. - * Intended for use by ConnectionPtr. - * Call this for connections that can safely be reused. + * Obtains the underlying connection which can be used for making calls to the server. */ - void releaseConnection(ConnectionList::iterator iter, Date_t now); + DBClientConnection* get() const { + return _connInfo->conn; + } /** - * Destroys a connection previously acquired from the pool. - * Intended for use by ConnectionPtr. - * Call this for connections that cannot be reused. + * Releases the connection back to the pool from which it was drawn. */ - void destroyConnection(ConnectionList::iterator); + void done(Date_t now); - /** - * Closes all connections currently in use, to ensure that the network threads - * terminate promptly during shutdown. - */ - void closeAllInUseConnections(); + private: + ConnectionPool* _pool; + const ConnectionList::iterator _connInfo; + }; - /** - * Reaps all connections in the pool that are too old as of "now". - */ - void cleanUpOlderThan(Date_t now); - private: - /** - * Returns true if the given connection is young enough to keep in the pool. - */ - bool _shouldKeepConnection(Date_t now, const ConnectionInfo& connInfo) const; + /** + * Instantiates a new connection pool with the specified tags to be applied to the + * messaging ports that it opens. + * + * @param messagingPortTags tags to be applied to the messaging ports for each of the + * connections. If no tags are required, use 0. + */ + ConnectionPool(int messagingPortTags); + ~ConnectionPool(); - /** - * Apply cleanup policy to any host(s) not active in the last kCleanupInterval milliseconds. - */ - void _cleanUpStaleHosts_inlock(Date_t now); + /** + * Acquires a connection to "target" with the given "timeout", or throws a DBException. + * Intended for use by ConnectionPtr. + */ + ConnectionList::iterator acquireConnection(const HostAndPort& target, + Date_t now, + Milliseconds timeout); - /** - * Implementation of cleanUpOlderThan which assumes that _mutex is already held. - */ - void _cleanUpOlderThan_inlock(Date_t now); + /** + * Releases a connection back into the pool. + * Intended for use by ConnectionPtr. + * Call this for connections that can safely be reused. + */ + void releaseConnection(ConnectionList::iterator iter, Date_t now); - /** - * Reaps connections in "hostConns" that are too old or have been in the pool too long as of - * "now". Expects _mutex to be held. - */ - void _cleanUpOlderThan_inlock(Date_t now, ConnectionList* hostConns); + /** + * Destroys a connection previously acquired from the pool. + * Intended for use by ConnectionPtr. + * Call this for connections that cannot be reused. + */ + void destroyConnection(ConnectionList::iterator); - /** - * Destroys the connection associated with "iter" and removes "iter" fron connList. - */ - static void _destroyConnection_inlock(ConnectionList* connList, - ConnectionList::iterator iter); + /** + * Closes all connections currently in use, to ensure that the network threads + * terminate promptly during shutdown. + */ + void closeAllInUseConnections(); + + /** + * Reaps all connections in the pool that are too old as of "now". + */ + void cleanUpOlderThan(Date_t now); + +private: + /** + * Returns true if the given connection is young enough to keep in the pool. + */ + bool _shouldKeepConnection(Date_t now, const ConnectionInfo& connInfo) const; + /** + * Apply cleanup policy to any host(s) not active in the last kCleanupInterval milliseconds. + */ + void _cleanUpStaleHosts_inlock(Date_t now); - // Flags to apply to the opened connections - const int _messagingPortTags; + /** + * Implementation of cleanUpOlderThan which assumes that _mutex is already held. + */ + void _cleanUpOlderThan_inlock(Date_t now); - // Mutex guarding members of the connection pool - stdx::mutex _mutex; + /** + * Reaps connections in "hostConns" that are too old or have been in the pool too long as of + * "now". Expects _mutex to be held. + */ + void _cleanUpOlderThan_inlock(Date_t now, ConnectionList* hostConns); - // Map from HostAndPort to idle connections. - HostConnectionMap _connections; + /** + * Destroys the connection associated with "iter" and removes "iter" fron connList. + */ + static void _destroyConnection_inlock(ConnectionList* connList, ConnectionList::iterator iter); - // List of non-idle connections. - ConnectionList _inUseConnections; - // Map of HostAndPorts to when they were last used. - HostLastUsedMap _lastUsedHosts; - - // Time representing when the connections were last cleaned. - Date_t _lastCleanUpTime; - }; + // Flags to apply to the opened connections + const int _messagingPortTags; + + // Mutex guarding members of the connection pool + stdx::mutex _mutex; + + // Map from HostAndPort to idle connections. + HostConnectionMap _connections; + + // List of non-idle connections. + ConnectionList _inUseConnections; + + // Map of HostAndPorts to when they were last used. + HostLastUsedMap _lastUsedHosts; + + // Time representing when the connections were last cleaned. + Date_t _lastCleanUpTime; +}; -} // namespace mongo +} // namespace mongo |