added a timeout parameter to connection_pool::acquire and a connection_pool::try_acquire with connection id

This commit is contained in:
Sascha Kühl
2025-11-13 16:28:41 +01:00
parent d6de8da3c8
commit 167e2ef382
3 changed files with 23 additions and 32 deletions
+15 -29
View File
@@ -1,6 +1,5 @@
#include "matador/sql/connection_pool.hpp"
#include <chrono>
#include <thread>
#include <utility>
@@ -98,42 +97,29 @@ connection_ptr connection_pool::try_acquire() {
return get_next_connection();
}
connection_ptr connection_pool::acquire(const size_t id) {
using namespace std::chrono_literals;
connection_ptr connection_pool::try_acquire(const size_t id) {
std::unique_lock lock(mutex_);
const auto it = idle_connections_.find(id);
if (it == idle_connections_.end()) {
return {nullptr, this};
}
return {it->second, this};
}
connection_ptr connection_pool::acquire(const size_t id, const std::chrono::milliseconds timeout) {
std::unique_lock lock(mutex_);
if (!cv.wait_for(lock,
5s,
[this, id] {
return idle_connections_.find(id) != idle_connections_.end();
})) {
if (!cv.wait_for(lock, timeout, [this, id] {
return idle_connections_.find(id) != idle_connections_.end();
})) {
return {nullptr, this};
}
}
auto it = idle_connections_.find(id);
const 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) {