@@ -242,72 +242,59 @@ class asio_connection_pool
242
242
// This will destroy and remove the connection from pool after the set timeout.
243
243
// We use 'this' because async calls to timer handler only occur while the pool exists.
244
244
connection->start_pool_timer (m_timeout_secs, boost::bind (&asio_connection_pool::handle_pool_timer, this , boost::asio::placeholders::error, connection));
245
-
246
- put_to_pool (connection);
245
+
246
+ std::lock_guard<std::mutex> lock (m_connections_mutex);
247
+ m_connections.push_back (connection);
247
248
}
248
249
// Otherwise connection is not put to the pool and it will go out of scope.
249
250
}
250
251
251
252
std::shared_ptr<asio_connection> obtain ()
252
253
{
253
- if (is_pool_empty ())
254
+ std::unique_lock<std::mutex> lock (m_connections_mutex);
255
+ if (m_connections.empty ())
254
256
{
257
+ lock.unlock ();
258
+
255
259
// No connections in pool => create a new connection instance.
256
260
return std::make_shared<asio_connection>(m_io_service, m_use_ssl);
257
261
}
258
262
else
259
263
{
260
264
// Reuse connection from pool.
261
- auto connection (get_head ());
265
+ auto connection = m_connections.back ();
266
+ m_connections.pop_back ();
267
+ lock.unlock ();
268
+
262
269
connection->start_reuse ();
263
270
return connection;
264
271
}
265
272
}
266
273
267
274
private:
268
-
269
- bool is_pool_empty ()
270
- {
271
- std::lock_guard<std::mutex> lock (m_connections_mutex);
272
- return m_connections.empty ();
273
- }
274
-
275
- std::shared_ptr<asio_connection> get_head ()
276
- {
277
- std::lock_guard<std::mutex> lock (m_connections_mutex);
278
- auto connection (*m_connections.begin ());
279
- m_connections.erase (m_connections.begin ());
280
- return connection;
281
- }
282
-
283
- void put_to_pool (const std::shared_ptr<asio_connection> &connection)
284
- {
285
- std::lock_guard<std::mutex> lock (m_connections_mutex);
286
- m_connections.insert (connection);
287
- }
288
-
289
- void remove (const std::shared_ptr<asio_connection> &connection)
290
- {
291
- std::lock_guard<std::mutex> lock (m_connections_mutex);
292
- m_connections.erase (connection);
293
- }
294
-
275
+
295
276
// Using weak_ptr here ensures bind() to this handler will not prevent the connection object from going out of scope.
296
277
void handle_pool_timer (const boost::system::error_code& ec, const std::weak_ptr<asio_connection> &connection)
297
278
{
298
279
if (!ec)
299
280
{
300
- if (auto connection_shared = connection.lock ())
281
+ auto connection_shared = connection.lock ();
282
+ if (connection_shared)
301
283
{
302
- remove (connection_shared);
284
+ std::lock_guard<std::mutex> lock (m_connections_mutex);
285
+ const auto &iter = std::find (m_connections.begin (), m_connections.end (), connection_shared);
286
+ if (iter != m_connections.end ())
287
+ {
288
+ m_connections.erase (iter);
289
+ }
303
290
}
304
291
}
305
292
}
306
293
307
294
boost::asio::io_service& m_io_service;
308
295
const int m_timeout_secs;
309
296
const bool m_use_ssl;
310
- std::unordered_set <std::shared_ptr<asio_connection> > m_connections;
297
+ std::vector <std::shared_ptr<asio_connection> > m_connections;
311
298
std::mutex m_connections_mutex;
312
299
};
313
300
0 commit comments