finished statement cache class

This commit is contained in:
2025-08-05 19:30:40 +02:00
parent 31964e55c1
commit 35fad9f47c
13 changed files with 410 additions and 74 deletions
+38 -15
View File
@@ -71,9 +71,14 @@ connection_pool::connection_pool(const std::string& dns, size_t count)
connection_ptr connection_pool::acquire() {
std::unique_lock lock(mutex_);
while (idle_connections_.empty()) {
cv.wait(lock);
if (!cv.wait_for(lock,
std::chrono::seconds(30),
[this] { return !idle_connections_.empty(); })) {
return {nullptr, this};
}
// while (idle_connections_.empty()) {
// cv.wait(lock);
// }
return get_next_connection();
}
@@ -89,23 +94,40 @@ connection_ptr connection_pool::try_acquire() {
connection_ptr connection_pool::acquire(const size_t id) {
using namespace std::chrono_literals;
pointer next_connection{nullptr};
auto try_count{0};
std::unique_lock lock(mutex_);
do {
if (auto it = idle_connections_.find(id); it != idle_connections_.end()) {
next_connection = it->second;
auto node = idle_connections_.extract(it);
inuse_connections_.insert(std::move(node));
} else {
lock.unlock();
std::this_thread::sleep_for(100ms);
lock.lock();
}
} while(try_count++ < 5);
if (!cv.wait_for(lock,
5s,
[this, id] {
return idle_connections_.find(id) != idle_connections_.end();
})) {
return {nullptr, this};
}
auto it = idle_connections_.find(id);
auto next_connection = it->second;
auto node = idle_connections_.extract(it);
inuse_connections_.insert(std::move(node));
return {next_connection, this};
// using namespace std::chrono_literals;
// pointer next_connection{nullptr};
// auto try_count{0};
// std::unique_lock lock(mutex_);
//
// do {
// if (auto it = idle_connections_.find(id); it != idle_connections_.end()) {
// next_connection = it->second;
// auto node = idle_connections_.extract(it);
// inuse_connections_.insert(std::move(node));
// } else {
// lock.unlock();
// std::this_thread::sleep_for(100ms);
// lock.lock();
// }
// } while(try_count++ < 5);
//
// return {next_connection, this};
}
void connection_pool::release(identifiable_connection* c) {
@@ -116,6 +138,7 @@ void connection_pool::release(identifiable_connection* c) {
if (const auto it = inuse_connections_.find(c->id); it != inuse_connections_.end()) {
auto node = inuse_connections_.extract(it);
idle_connections_.insert(std::move(node));
cv.notify_one();
}
}