@@ -98,9 +98,11 @@ void ConnectionPoolManager::add(const Address& address) {
9898}
9999
100100void ConnectionPoolManager::remove (const Address& address) {
101- ScopedWriteLock rl (&rwlock_);
101+ ScopedReadLock rl (&rwlock_);
102102 ConnectionPool::Map::iterator it = pools_.find (address);
103103 if (it == pools_.end ()) return ;
104+ // The connection pool will remove itself from the manager when all of its
105+ // connections are closed.
104106 it->second ->close ();
105107}
106108
@@ -141,7 +143,8 @@ void ConnectionPoolManager::add_pool(const ConnectionPool::Ptr& pool, Protected)
141143}
142144
143145void ConnectionPoolManager::remove_pool (ConnectionPool* pool, Protected) {
144- internal_remove_pool (pool->address ());
146+ ScopedWriteLock wl (&rwlock_);
147+ internal_remove_pool (wl, pool->address ());
145148}
146149
147150void ConnectionPoolManager::notify_up (ConnectionPool* pool, Protected) {
@@ -163,20 +166,23 @@ void ConnectionPoolManager::notify_critical_error(ConnectionPool* pool,
163166 if (listener_ != NULL ) {
164167 listener_->on_critical_error (pool->address (), code, message);
165168 }
166- internal_remove_pool (pool->address ());
169+ ScopedWriteLock wl (&rwlock_);
170+ internal_remove_pool (wl, pool->address ());
167171}
168172
169173void ConnectionPoolManager::internal_add_pool (const ConnectionPool::Ptr& pool) {
170174 LOG_DEBUG (" Adding pool for host %s" , pool->address ().to_string ().c_str ());
171175 pools_[pool->address ()] = pool;
172176}
173177
174- void ConnectionPoolManager::internal_remove_pool (const Address& address) {
175- ScopedWriteLock wl (&rwlock_);
178+ void ConnectionPoolManager::internal_remove_pool (ScopedWriteLock& wl,
179+ const Address& address) {
176180 pools_.erase (address);
177181 maybe_closed (wl);
178182}
179183
184+ // This must be the last call in a function because it can potentially
185+ // deallocate the manager.
180186void ConnectionPoolManager::maybe_closed (ScopedWriteLock& wl) {
181187 if (is_closing_ && pools_.empty ()) {
182188 wl.unlock (); // The manager is destroyed in this step it must be unlocked
0 commit comments