Skip to content

Commit 5caac19

Browse files
committed
move connection_pool_map related functions from threadpool to http_client_asio
1 parent ca5cf69 commit 5caac19

File tree

3 files changed

+72
-78
lines changed

3 files changed

+72
-78
lines changed

Release/include/pplx/threadpool.h

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@
4242
#include "pplx/pplx.h"
4343
#endif
4444

45-
namespace web { namespace http { namespace client { namespace details {
46-
class asio_connection_pool;
47-
}}}}
48-
4945
namespace crossplat {
5046

5147
#if (defined(ANDROID) || defined(__ANDROID__))
@@ -101,31 +97,6 @@ class threadpool
10197
return m_service;
10298
}
10399

104-
template<typename PoolGenerator>
105-
std::shared_ptr<web::http::client::details::asio_connection_pool> obtain_connection_pool(const std::string &key, PoolGenerator pool_generator)
106-
{
107-
std::lock_guard<std::mutex> lg(m_connection_pool_map_mutex);
108-
109-
auto &pool = m_connection_pool_map[key];
110-
if (!pool)
111-
{
112-
pool = pool_generator();
113-
}
114-
115-
return pool;
116-
}
117-
118-
template<typename PoolReleaseHandler>
119-
void release_connection_pool(const std::string &key, PoolReleaseHandler handler)
120-
{
121-
std::lock_guard<std::mutex> lg(m_connection_pool_map_mutex);
122-
123-
auto pool = m_connection_pool_map[key];
124-
handler(pool);
125-
}
126-
127-
void free_connection_pool(const boost::system::error_code &ec, const std::string &key);
128-
129100
private:
130101
struct _cancel_thread { };
131102

@@ -185,9 +156,6 @@ class threadpool
185156
std::vector<pthread_t> m_threads;
186157
boost::asio::io_service m_service;
187158
boost::asio::io_service::work m_work;
188-
189-
std::mutex m_connection_pool_map_mutex;
190-
std::map<std::string, std::shared_ptr<web::http::client::details::asio_connection_pool>> m_connection_pool_map;
191159
};
192160

193161
}

Release/src/http/client/http_client_asio.cpp

Lines changed: 72 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,28 @@ enum class httpclient_errorcode_context
7272
};
7373

7474
class asio_connection_pool;
75+
76+
class asio_connection_pool_map
77+
{
78+
public:
79+
static asio_connection_pool_map &instance();
80+
81+
std::shared_ptr<asio_connection_pool> obtain_connection_pool(const std::string &pool_key, bool start_with_ssl, std::chrono::seconds idle_timeout);
82+
83+
void release_connection_pool(const std::string &pool_key);
84+
85+
void free_connection_pool(const boost::system::error_code &ec, const std::string &pool_key);
86+
87+
private:
88+
asio_connection_pool_map()
89+
: m_threadpool(crossplat::threadpool::shared_instance())
90+
{}
91+
92+
crossplat::threadpool &m_threadpool;
93+
std::mutex m_connection_pool_map_mutex;
94+
std::map<std::string, std::shared_ptr<asio_connection_pool>> m_connection_pool_map;
95+
};
96+
7597
class asio_connection
7698
{
7799
friend class asio_connection_pool;
@@ -257,8 +279,7 @@ class asio_connection_pool
257279
m_start_with_ssl(start_with_ssl),
258280
m_ssl_context_callback(ssl_context_callback),
259281
m_pool_timeout_secs(60), // Clean this connection pool 60 secs after the last asio_client release it.
260-
m_pool_timer(io_service),
261-
m_use_count(0)
282+
m_pool_timer(io_service)
262283
{}
263284

264285
~asio_connection_pool()
@@ -320,11 +341,6 @@ class asio_connection_pool
320341
}
321342
}
322343

323-
int &use_count()
324-
{
325-
return m_use_count;
326-
}
327-
328344
private:
329345

330346
// Using weak_ptr here ensures bind() to this handler will not prevent the connection object from going out of scope.
@@ -354,9 +370,49 @@ class asio_connection_pool
354370

355371
const int m_pool_timeout_secs;
356372
boost::asio::deadline_timer m_pool_timer;
357-
int m_use_count;
358373
};
359374

375+
asio_connection_pool_map &asio_connection_pool_map::instance()
376+
{
377+
static asio_connection_pool_map s_instance;
378+
return s_instance;
379+
}
380+
381+
std::shared_ptr<asio_connection_pool> asio_connection_pool_map::obtain_connection_pool(const std::string &pool_key, bool start_with_ssl, std::chrono::seconds idle_timeout)
382+
{
383+
std::lock_guard<std::mutex> lg(m_connection_pool_map_mutex);
384+
auto &pool = m_connection_pool_map[pool_key];
385+
if (!pool)
386+
{
387+
pool = std::make_shared<asio_connection_pool>(crossplat::threadpool::shared_instance().service(), start_with_ssl, idle_timeout, nullptr);
388+
}
389+
390+
pool->cancel_pool_timer();
391+
return pool;
392+
}
393+
394+
void asio_connection_pool_map::release_connection_pool(const std::string &pool_key)
395+
{
396+
std::lock_guard<std::mutex> lg(m_connection_pool_map_mutex);
397+
auto &pool = m_connection_pool_map[pool_key];
398+
if (pool)
399+
{
400+
pool->start_pool_timer(boost::bind(&asio_connection_pool_map::free_connection_pool, this, boost::asio::placeholders::error, pool_key));
401+
}
402+
}
403+
404+
void asio_connection_pool_map::free_connection_pool(const boost::system::error_code &ec, const std::string &pool_key)
405+
{
406+
if (!ec)
407+
{
408+
std::lock_guard<std::mutex> lg(m_connection_pool_map_mutex);
409+
auto &pool = m_connection_pool_map[pool_key];
410+
if (pool && pool.use_count() == 1)
411+
{
412+
m_connection_pool_map.erase(pool_key);
413+
}
414+
}
415+
}
360416

361417
class asio_client : public _http_client_communicator
362418
{
@@ -365,13 +421,14 @@ class asio_client : public _http_client_communicator
365421
: _http_client_communicator(std::move(address), std::move(client_config))
366422
, m_resolver(crossplat::threadpool::shared_instance().service())
367423
{
368-
if (this->client_config().get_ssl_context_callback())
424+
bool start_with_ssl = base_uri().scheme() == "https" && !_http_client_communicator::client_config().proxy().is_specified();
425+
const std::chrono::seconds idle_timeout(30); // Unused sockets are kept in pool for 30 seconds.
426+
auto ssl_context_callback = this->client_config().get_ssl_context_callback();
427+
428+
if (ssl_context_callback)
369429
{
370430
// The pool is not added to the map because there is no better approaches to compare callback functors.
371-
m_pool = std::make_shared<asio_connection_pool>(crossplat::threadpool::shared_instance().service(),
372-
base_uri().scheme() == "https" && !_http_client_communicator::client_config().proxy().is_specified(),
373-
std::chrono::seconds(30), // Unused sockets are kept in pool for 30 seconds.
374-
this->client_config().get_ssl_context_callback());
431+
m_pool = std::make_shared<asio_connection_pool>(crossplat::threadpool::shared_instance().service(), start_with_ssl, idle_timeout, ssl_context_callback);
375432
}
376433
else
377434
{
@@ -393,37 +450,15 @@ class asio_client : public _http_client_communicator
393450
}
394451
}
395452

396-
m_pool = crossplat::threadpool::shared_instance().obtain_connection_pool(m_pool_key, [this]()
397-
{
398-
return std::make_shared<asio_connection_pool>(crossplat::threadpool::shared_instance().service(),
399-
base_uri().scheme() == "https" && !_http_client_communicator::client_config().proxy().is_specified(),
400-
std::chrono::seconds(30), // Unused sockets are kept in pool for 30 seconds.
401-
nullptr);
402-
});
403-
404-
if (m_pool->use_count() == 0)
405-
{
406-
m_pool->cancel_pool_timer();
407-
}
408-
++m_pool->use_count();
453+
m_pool = asio_connection_pool_map::instance().obtain_connection_pool(m_pool_key, start_with_ssl, idle_timeout);
409454
}
410455
}
411456

412457
~asio_client()
413458
{
414459
if (!m_pool_key.empty())
415460
{
416-
crossplat::threadpool::shared_instance().release_connection_pool(m_pool_key, [this](std::shared_ptr<web::http::client::details::asio_connection_pool> pool)
417-
{
418-
if (pool)
419-
{
420-
--pool->use_count();
421-
if (pool->use_count() == 0)
422-
{
423-
pool->start_pool_timer(boost::bind(&crossplat::threadpool::free_connection_pool, &crossplat::threadpool::shared_instance(), boost::asio::placeholders::error, m_pool_key));
424-
}
425-
}
426-
});
461+
asio_connection_pool_map::instance().release_connection_pool(m_pool_key);
427462
}
428463
}
429464

Release/src/pplx/threadpool.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,6 @@ threadpool& threadpool::shared_instance()
6767
return s_shared;
6868
}
6969

70-
void threadpool::free_connection_pool(const boost::system::error_code &ec, const std::string &key)
71-
{
72-
if (!ec)
73-
{
74-
std::lock_guard<std::mutex> lg(m_connection_pool_map_mutex);
75-
m_connection_pool_map.erase(key);
76-
}
77-
}
78-
7970
#endif
8071

8172
}

0 commit comments

Comments
 (0)