11// Copyright (c) Microsoft Corporation.
22// Licensed under the MIT license.
33
4- // INFO|TODO - Note that is file is Windows specific right now. Making it arch agnostic will be
5- // taken up in future.
4+ // INFO|TODO - Note that is file is Windows specific right now. Making it
5+ // arch agnostic will be taken up in future.
66
7- #include " connection_pool.h"
7+ #include " connection/ connection_pool.h"
88#include < exception>
9+ #include < memory>
10+ #include < vector>
911
1012ConnectionPool::ConnectionPool (size_t max_size, int idle_timeout_secs)
11- : _max_size(max_size), _idle_timeout_secs(idle_timeout_secs), _current_size(0 ) {}
13+ : _max_size(max_size), _idle_timeout_secs(idle_timeout_secs),
14+ _current_size(0 ) {}
1215
13- std::shared_ptr<Connection> ConnectionPool::acquire (const std::wstring& connStr, const py::dict& attrs_before) {
16+ std::shared_ptr<Connection> ConnectionPool::acquire (
17+ const std::wstring& connStr, const py::dict& attrs_before) {
1418 std::vector<std::shared_ptr<Connection>> to_disconnect;
1519 std::shared_ptr<Connection> valid_conn = nullptr ;
1620 {
@@ -21,7 +25,8 @@ std::shared_ptr<Connection> ConnectionPool::acquire(const std::wstring& connStr,
2125 // Phase 1: Remove stale connections, collect for later disconnect
2226 _pool.erase (std::remove_if (_pool.begin (), _pool.end (),
2327 [&](const std::shared_ptr<Connection>& conn) {
24- auto idle_time = std::chrono::duration_cast<std::chrono::seconds>(now - conn->lastUsed ()).count ();
28+ auto idle_time = std::chrono::duration_cast<
29+ std::chrono::seconds>(now - conn->lastUsed ()).count ();
2530 if (idle_time > _idle_timeout_secs) {
2631 to_disconnect.push_back (conn);
2732 return true ;
@@ -30,7 +35,8 @@ std::shared_ptr<Connection> ConnectionPool::acquire(const std::wstring& connStr,
3035 }), _pool.end ());
3136
3237 size_t pruned = before - _pool.size ();
33- _current_size = (_current_size >= pruned) ? (_current_size - pruned) : 0 ;
38+ _current_size = (_current_size >= pruned) ?
39+ (_current_size - pruned) : 0 ;
3440
3541 // Phase 2: Attempt to reuse healthy connections
3642 while (!_pool.empty ()) {
@@ -56,7 +62,8 @@ std::shared_ptr<Connection> ConnectionPool::acquire(const std::wstring& connStr,
5662 valid_conn->connect (attrs_before);
5763 ++_current_size;
5864 } else if (!valid_conn) {
59- throw std::runtime_error (" ConnectionPool::acquire: pool size limit reached" );
65+ throw std::runtime_error (
66+ " ConnectionPool::acquire: pool size limit reached" );
6067 }
6168 }
6269
@@ -76,8 +83,7 @@ void ConnectionPool::release(std::shared_ptr<Connection> conn) {
7683 if (_pool.size () < _max_size) {
7784 conn->updateLastUsed ();
7885 _pool.push_back (conn);
79- }
80- else {
86+ } else {
8187 conn->disconnect ();
8288 if (_current_size > 0 ) --_current_size;
8389 }
@@ -97,7 +103,8 @@ void ConnectionPool::close() {
97103 try {
98104 conn->disconnect ();
99105 } catch (const std::exception& ex) {
100- LOG (" ConnectionPool::close: disconnect failed: {}" , ex.what ());
106+ LOG (" ConnectionPool::close: disconnect failed: {}" ,
107+ ex.what ());
101108 }
102109 }
103110}
@@ -107,18 +114,21 @@ ConnectionPoolManager& ConnectionPoolManager::getInstance() {
107114 return manager;
108115}
109116
110- std::shared_ptr<Connection> ConnectionPoolManager::acquireConnection (const std::wstring& connStr, const py::dict& attrs_before) {
117+ std::shared_ptr<Connection> ConnectionPoolManager::acquireConnection (
118+ const std::wstring& connStr, const py::dict& attrs_before) {
111119 std::lock_guard<std::mutex> lock (_manager_mutex);
112120
113121 auto & pool = _pools[connStr];
114122 if (!pool) {
115123 LOG (" Creating new connection pool" );
116- pool = std::make_shared<ConnectionPool>(_default_max_size, _default_idle_secs);
124+ pool = std::make_shared<ConnectionPool>(_default_max_size,
125+ _default_idle_secs);
117126 }
118127 return pool->acquire (connStr, attrs_before);
119128}
120129
121- void ConnectionPoolManager::returnConnection (const std::wstring& conn_str, const std::shared_ptr<Connection> conn) {
130+ void ConnectionPoolManager::returnConnection (
131+ const std::wstring& conn_str, const std::shared_ptr<Connection> conn) {
122132 std::lock_guard<std::mutex> lock (_manager_mutex);
123133 if (_pools.find (conn_str) != _pools.end ()) {
124134 _pools[conn_str]->release ((conn));
0 commit comments