@@ -53,14 +53,13 @@ namespace web { namespace http
53
53
};
54
54
55
55
class linux_connection_pool ;
56
- class linux_connection : public std ::enable_shared_from_this<linux_connection>
56
+ class linux_connection
57
57
{
58
58
friend class linux_connection_pool ;
59
59
public:
60
- linux_connection (std::weak_ptr<linux_connection_pool> pool_weak, boost::asio::io_service& io_service) :
60
+ linux_connection (boost::asio::io_service& io_service) :
61
61
m_socket (io_service),
62
62
m_pool_timer (io_service),
63
- m_pool_weak (pool_weak),
64
63
m_is_reused (false ),
65
64
m_keep_alive (true )
66
65
{}
@@ -88,8 +87,7 @@ namespace web { namespace http
88
87
bool is_reused () const { return m_is_reused; }
89
88
90
89
void set_keep_alive (bool keep_alive) { m_keep_alive = keep_alive; }
91
- bool keep_alive () const { return m_keep_alive; }
92
-
90
+ bool keep_alive () const { return m_keep_alive; }
93
91
tcp::socket& socket () { return m_socket; }
94
92
95
93
private:
@@ -110,12 +108,11 @@ namespace web { namespace http
110
108
111
109
tcp::socket m_socket;
112
110
boost::asio::deadline_timer m_pool_timer;
113
- std::weak_ptr<linux_connection_pool> m_pool_weak;
114
111
bool m_is_reused;
115
112
bool m_keep_alive;
116
113
};
117
114
118
- class linux_connection_pool : public std ::enable_shared_from_this<linux_connection_pool>
115
+ class linux_connection_pool
119
116
{
120
117
public:
121
118
@@ -142,7 +139,7 @@ namespace web { namespace http
142
139
{
143
140
connection->cancel ();
144
141
// Remove idle connections from pool after timeout.
145
- connection->start_pool_timer (m_timeout_secs, boost::bind (&linux_connection ::handle_pool_timer, connection , boost::asio::placeholders::error));
142
+ connection->start_pool_timer (m_timeout_secs, boost::bind (&linux_connection_pool ::handle_pool_timer, this , boost::asio::placeholders::error, connection ));
146
143
147
144
{
148
145
std::lock_guard<std::mutex> lock (m_connections_mutex);
@@ -162,7 +159,7 @@ namespace web { namespace http
162
159
{
163
160
// No connections in pool => create new connection instance.
164
161
// shared_from_this() is only to create a weak_ptr to pool.
165
- return std::make_shared<linux_connection>(shared_from_this (), m_io_service);
162
+ return std::make_shared<linux_connection>(m_io_service);
166
163
}
167
164
else
168
165
{
@@ -179,6 +176,14 @@ namespace web { namespace http
179
176
m_connections.erase (connection);
180
177
}
181
178
179
+ void handle_pool_timer (const boost::system::error_code& ec, std::shared_ptr<linux_connection> connection)
180
+ {
181
+ if (!ec)
182
+ {
183
+ remove (connection);
184
+ }
185
+ }
186
+
182
187
private:
183
188
bool is_pool_empty ()
184
189
{
@@ -268,7 +273,7 @@ namespace web { namespace http
268
273
boost::asio::streambuf m_body_buf;
269
274
boost::asio::deadline_timer m_timeout_timer;
270
275
271
- ~linux_client_request_context ();
276
+ virtual ~linux_client_request_context ();
272
277
273
278
void handle_timeout_timer (const boost::system::error_code& ec)
274
279
{
@@ -300,7 +305,7 @@ namespace web { namespace http
300
305
: _http_client_communicator(std::move(address), client_config)
301
306
, m_resolver(crossplat::threadpool::shared_instance().service())
302
307
, m_io_service(crossplat::threadpool::shared_instance().service())
303
- , m_pool(std::make_shared<linux_connection_pool>( crossplat::threadpool::shared_instance().service(), client_config.timeout() ))
308
+ , m_pool(crossplat::threadpool::shared_instance().service(), client_config.timeout())
304
309
{}
305
310
306
311
unsigned long open ()
@@ -429,7 +434,7 @@ namespace web { namespace http
429
434
}
430
435
431
436
boost::asio::io_service& m_io_service;
432
- std::shared_ptr< linux_connection_pool> m_pool;
437
+ linux_connection_pool m_pool;
433
438
434
439
private:
435
440
tcp::resolver m_resolver;
@@ -504,7 +509,7 @@ namespace web { namespace http
504
509
ctx->m_timeout_timer .cancel ();
505
510
506
511
ctx->m_connection ->close ();
507
- ctx->m_connection = m_pool-> obtain ();
512
+ ctx->m_connection = m_pool. obtain ();
508
513
509
514
auto endpoint = *endpoints;
510
515
if (ctx->m_ssl_stream )
@@ -1091,29 +1096,15 @@ namespace web { namespace http
1091
1096
std::shared_ptr<_http_client_communicator> &client, http_request &request)
1092
1097
{
1093
1098
auto client_cast (std::static_pointer_cast<linux_client>(client));
1094
- auto connection (client_cast->m_pool -> obtain ());
1099
+ auto connection (client_cast->m_pool . obtain ());
1095
1100
return std::make_shared<linux_client_request_context>(client, request, connection);
1096
1101
}
1097
1102
1098
1103
linux_client_request_context::~linux_client_request_context ()
1099
1104
{
1100
1105
m_timeout_timer.cancel ();
1101
1106
// Give connection back to the pool where it can be reused.
1102
- std::static_pointer_cast<linux_client>(m_http_client)->m_pool ->release (m_connection);
1103
- }
1104
-
1105
- void linux_connection::handle_pool_timer (const boost::system::error_code& ec)
1106
- {
1107
- if (!ec)
1108
- {
1109
- if (auto pool_ptr = m_pool_weak.lock ())
1110
- {
1111
- // Remove connection from pool only if pool still exists (lock succeeds).
1112
- pool_ptr->remove (shared_from_this ());
1113
- }
1114
- // If lock fails, pool has been destroyed and we let connection object
1115
- // to expire via shared_ptr. This should happen when this method returns.
1116
- }
1107
+ std::static_pointer_cast<linux_client>(m_http_client)->m_pool .release (m_connection);
1117
1108
}
1118
1109
1119
1110
}}}} // namespaces
0 commit comments