@@ -98,9 +98,11 @@ void ConnectionPoolManager::add(const Address& address) {
98
98
}
99
99
100
100
void ConnectionPoolManager::remove (const Address& address) {
101
- ScopedWriteLock rl (&rwlock_);
101
+ ScopedReadLock rl (&rwlock_);
102
102
ConnectionPool::Map::iterator it = pools_.find (address);
103
103
if (it == pools_.end ()) return ;
104
+ // The connection pool will remove itself from the manager when all of its
105
+ // connections are closed.
104
106
it->second ->close ();
105
107
}
106
108
@@ -141,7 +143,8 @@ void ConnectionPoolManager::add_pool(const ConnectionPool::Ptr& pool, Protected)
141
143
}
142
144
143
145
void ConnectionPoolManager::remove_pool (ConnectionPool* pool, Protected) {
144
- internal_remove_pool (pool->address ());
146
+ ScopedWriteLock wl (&rwlock_);
147
+ internal_remove_pool (wl, pool->address ());
145
148
}
146
149
147
150
void ConnectionPoolManager::notify_up (ConnectionPool* pool, Protected) {
@@ -163,20 +166,23 @@ void ConnectionPoolManager::notify_critical_error(ConnectionPool* pool,
163
166
if (listener_ != NULL ) {
164
167
listener_->on_critical_error (pool->address (), code, message);
165
168
}
166
- internal_remove_pool (pool->address ());
169
+ ScopedWriteLock wl (&rwlock_);
170
+ internal_remove_pool (wl, pool->address ());
167
171
}
168
172
169
173
void ConnectionPoolManager::internal_add_pool (const ConnectionPool::Ptr& pool) {
170
174
LOG_DEBUG (" Adding pool for host %s" , pool->address ().to_string ().c_str ());
171
175
pools_[pool->address ()] = pool;
172
176
}
173
177
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) {
176
180
pools_.erase (address);
177
181
maybe_closed (wl);
178
182
}
179
183
184
+ // This must be the last call in a function because it can potentially
185
+ // deallocate the manager.
180
186
void ConnectionPoolManager::maybe_closed (ScopedWriteLock& wl) {
181
187
if (is_closing_ && pools_.empty ()) {
182
188
wl.unlock (); // The manager is destroyed in this step it must be unlocked
0 commit comments