Skip to content

Commit 81dfb17

Browse files
author
Valtteri Heikkila
committed
Removed weak_ptr reference to linux_connection_pool and made pool timeout to bind pool object instead of linux_connection.
1 parent 0aed1db commit 81dfb17

File tree

1 file changed

+20
-29
lines changed

1 file changed

+20
-29
lines changed

Release/src/http/client/http_linux.cpp

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,13 @@ namespace web { namespace http
5353
};
5454

5555
class linux_connection_pool;
56-
class linux_connection : public std::enable_shared_from_this<linux_connection>
56+
class linux_connection
5757
{
5858
friend class linux_connection_pool;
5959
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) :
6161
m_socket(io_service),
6262
m_pool_timer(io_service),
63-
m_pool_weak(pool_weak),
6463
m_is_reused(false),
6564
m_keep_alive(true)
6665
{}
@@ -88,8 +87,7 @@ namespace web { namespace http
8887
bool is_reused() const { return m_is_reused; }
8988

9089
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; }
9391
tcp::socket& socket() { return m_socket; }
9492

9593
private:
@@ -110,12 +108,11 @@ namespace web { namespace http
110108

111109
tcp::socket m_socket;
112110
boost::asio::deadline_timer m_pool_timer;
113-
std::weak_ptr<linux_connection_pool> m_pool_weak;
114111
bool m_is_reused;
115112
bool m_keep_alive;
116113
};
117114

118-
class linux_connection_pool : public std::enable_shared_from_this<linux_connection_pool>
115+
class linux_connection_pool
119116
{
120117
public:
121118

@@ -142,7 +139,7 @@ namespace web { namespace http
142139
{
143140
connection->cancel();
144141
// 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));
146143

147144
{
148145
std::lock_guard<std::mutex> lock(m_connections_mutex);
@@ -162,7 +159,7 @@ namespace web { namespace http
162159
{
163160
// No connections in pool => create new connection instance.
164161
// 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);
166163
}
167164
else
168165
{
@@ -179,6 +176,14 @@ namespace web { namespace http
179176
m_connections.erase(connection);
180177
}
181178

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+
182187
private:
183188
bool is_pool_empty()
184189
{
@@ -268,7 +273,7 @@ namespace web { namespace http
268273
boost::asio::streambuf m_body_buf;
269274
boost::asio::deadline_timer m_timeout_timer;
270275

271-
~linux_client_request_context();
276+
virtual ~linux_client_request_context();
272277

273278
void handle_timeout_timer(const boost::system::error_code& ec)
274279
{
@@ -300,7 +305,7 @@ namespace web { namespace http
300305
: _http_client_communicator(std::move(address), client_config)
301306
, m_resolver(crossplat::threadpool::shared_instance().service())
302307
, 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())
304309
{}
305310

306311
unsigned long open()
@@ -429,7 +434,7 @@ namespace web { namespace http
429434
}
430435

431436
boost::asio::io_service& m_io_service;
432-
std::shared_ptr<linux_connection_pool> m_pool;
437+
linux_connection_pool m_pool;
433438

434439
private:
435440
tcp::resolver m_resolver;
@@ -504,7 +509,7 @@ namespace web { namespace http
504509
ctx->m_timeout_timer.cancel();
505510

506511
ctx->m_connection->close();
507-
ctx->m_connection = m_pool->obtain();
512+
ctx->m_connection = m_pool.obtain();
508513

509514
auto endpoint = *endpoints;
510515
if (ctx->m_ssl_stream)
@@ -1091,29 +1096,15 @@ namespace web { namespace http
10911096
std::shared_ptr<_http_client_communicator> &client, http_request &request)
10921097
{
10931098
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());
10951100
return std::make_shared<linux_client_request_context>(client, request, connection);
10961101
}
10971102

10981103
linux_client_request_context::~linux_client_request_context()
10991104
{
11001105
m_timeout_timer.cancel();
11011106
// 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);
11171108
}
11181109

11191110
}}}} // namespaces

0 commit comments

Comments
 (0)