-
Notifications
You must be signed in to change notification settings - Fork 28
FEAT: C++ support for pooling #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
4591b55
refactor native layer and create reusable components
e375b20
add newline
b5a7d5a
Delete copy/assign for DriverLoader singleton
fc5903a
resolve review comments
998b25e
initial edit
7f9304b
working flow with c++ connection class
65d19a8
working interation with access token
be946d0
cleanup free() in sqlhandle
aad1e22
removed unnecessary prints
179ebf2
removing comment
70f6aa3
resolving conflict
b22b197
working
e302c0c
final working-fix test
gargsaumya 6a077c6
minor updates
gargsaumya 2c9c3d7
Merge branch 'saumya/conn_implementation' into saumya/integratec++class
gargsaumya 5181a0e
updating file
gargsaumya 3240394
added a TODO comment to address review comments
gargsaumya a9ee888
Merge branch 'saumya/conn_implementation' into saumya/integratec++class
gargsaumya d373e9b
adding c++ support for pooling
gargsaumya 079bf57
Merge remote-tracking branch 'origin/main' into saumya/pool-c++
gargsaumya 2cc10bf
Merge branch 'main' into saumya/pool-c++
gargsaumya f576b08
addressed review comments
gargsaumya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
|
|
||
| // INFO|TODO - Note that is file is Windows specific right now. Making it arch agnostic will be | ||
| // taken up in future. | ||
|
|
||
| #include "connection_pool.h" | ||
| #include <iostream> | ||
| #include <exception> | ||
|
|
||
| ConnectionPool::ConnectionPool(size_t max_size, int idle_timeout_secs) | ||
| : _max_size(max_size), _idle_timeout_secs(idle_timeout_secs), _current_size(0) {} | ||
|
|
||
| std::shared_ptr<Connection> ConnectionPool::acquire(const std::wstring& connStr, const py::dict& attrs_before) { | ||
| std::vector<std::shared_ptr<Connection>> to_disconnect; | ||
| std::shared_ptr<Connection> valid_conn = nullptr; | ||
| { | ||
| std::lock_guard<std::mutex> lock(_mutex); | ||
| auto now = std::chrono::steady_clock::now(); | ||
| size_t before = _pool.size(); | ||
|
|
||
| // Phase 1: Remove stale connections, collect for later disconnect | ||
| _pool.erase(std::remove_if(_pool.begin(), _pool.end(), | ||
| [&](const std::shared_ptr<Connection>& conn) { | ||
| auto idle_time = std::chrono::duration_cast<std::chrono::seconds>(now - conn->lastUsed()).count(); | ||
| if (idle_time > _idle_timeout_secs) { | ||
| to_disconnect.push_back(conn); | ||
| return true; | ||
| } | ||
| return false; | ||
| }), _pool.end()); | ||
|
|
||
| size_t pruned = before - _pool.size(); | ||
| _current_size = (_current_size >= pruned) ? (_current_size - pruned) : 0; | ||
|
|
||
| // Phase 2: Attempt to reuse healthy connections | ||
| while (!_pool.empty()) { | ||
| auto conn = _pool.front(); | ||
| _pool.pop_front(); | ||
| if (conn->isAlive()) { | ||
| if (!conn->reset()) { | ||
| to_disconnect.push_back(conn); | ||
| --_current_size; | ||
| continue; | ||
| } | ||
| valid_conn = conn; | ||
| break; | ||
| } else { | ||
| to_disconnect.push_back(conn); | ||
| --_current_size; | ||
| } | ||
| } | ||
|
|
||
| // Create new connection if none reusable | ||
| if (!valid_conn && _current_size < _max_size) { | ||
| valid_conn = std::make_shared<Connection>(connStr, true); | ||
| valid_conn->connect(attrs_before); | ||
| ++_current_size; | ||
| } else if (!valid_conn) { | ||
| throw std::runtime_error("ConnectionPool::acquire: pool size limit reached"); | ||
| } | ||
| } | ||
|
|
||
| // Phase 3: Disconnect expired/bad connections outside lock | ||
| for (auto& conn : to_disconnect) { | ||
| try { | ||
| conn->disconnect(); | ||
| } catch (const std::exception& ex) { | ||
| std::cout << "disconnect() failed: " << ex.what() << std::endl; | ||
| } | ||
| } | ||
| return valid_conn; | ||
| } | ||
|
|
||
| void ConnectionPool::release(std::shared_ptr<Connection> conn) { | ||
| std::lock_guard<std::mutex> lock(_mutex); | ||
| if (_pool.size() < _max_size) { | ||
| conn->updateLastUsed(); | ||
| _pool.push_back(conn); | ||
| } | ||
| else { | ||
| conn->disconnect(); | ||
| if (_current_size > 0) --_current_size; | ||
| } | ||
| } | ||
|
|
||
| ConnectionPoolManager& ConnectionPoolManager::getInstance() { | ||
| static ConnectionPoolManager manager; | ||
| return manager; | ||
| } | ||
|
|
||
| std::shared_ptr<Connection> ConnectionPoolManager::acquireConnection(const std::wstring& connStr, const py::dict& attrs_before) { | ||
| std::lock_guard<std::mutex> lock(_manager_mutex); | ||
|
|
||
| auto& pool = _pools[connStr]; | ||
| if (!pool) { | ||
| LOG("Creating new connection pool"); | ||
| pool = std::make_shared<ConnectionPool>(_default_max_size, _default_idle_secs); | ||
| } | ||
| return pool->acquire(connStr, attrs_before); | ||
| } | ||
|
|
||
| void ConnectionPoolManager::returnConnection(const std::wstring& conn_str, const std::shared_ptr<Connection> conn) { | ||
| std::lock_guard<std::mutex> lock(_manager_mutex); | ||
| if (_pools.find(conn_str) != _pools.end()) { | ||
| _pools[conn_str]->release((conn)); | ||
| } | ||
| } | ||
|
|
||
| void ConnectionPoolManager::configure(int max_size, int idle_timeout_secs) { | ||
| std::lock_guard<std::mutex> lock(_manager_mutex); | ||
| _default_max_size = max_size; | ||
| _default_idle_secs = idle_timeout_secs; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.