Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/include/dbconnector/pool.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "pool/connection_pool_config.hpp"
#include "pool/pooled_connection.hpp"
#include "pool/thread_local_connection_cache.hpp"
#include "pool/connection_pool.hpp"
Expand Down
12 changes: 2 additions & 10 deletions src/include/dbconnector/pool/connection_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <thread>

#include "dbconnector/pool/cached_connection.hpp"
#include "dbconnector/pool/connection_pool_config.hpp"
#include "dbconnector/pool/pooled_connection.hpp"
#include "dbconnector/pool/thread_local_connection_cache.hpp"

Expand All @@ -18,16 +19,7 @@ namespace pool {
template <typename ConnectionT>
class ConnectionPool : public std::enable_shared_from_this<ConnectionPool<ConnectionT>> {
public:
static constexpr uint64_t DEFAULT_POOL_SIZE = 4;
static constexpr uint64_t DEFAULT_POOL_TIMEOUT_MS = 30000;

enum class ThreadLocalCacheState {
CACHE_ENABLED,
CACHE_DISABLED,
};

ConnectionPool(uint64_t max_connections = DEFAULT_POOL_SIZE, uint64_t wait_timeout_millis = DEFAULT_POOL_TIMEOUT_MS,
ThreadLocalCacheState tl_cache_state = ThreadLocalCacheState::CACHE_DISABLED);
ConnectionPool(ConnectionPoolConfig config = ConnectionPoolConfig());
virtual ~ConnectionPool();

PooledConnection<ConnectionT> WaitAcquire();
Expand Down
18 changes: 18 additions & 0 deletions src/include/dbconnector/pool/connection_pool_config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <cstdint>

namespace dbconnector {
namespace pool {

struct ConnectionPoolConfig {
uint64_t max_connections = 4;
uint64_t wait_timeout_millis = 30000;
bool tl_cache_enabled = true;
uint64_t max_lifetime_millis = 0;
uint64_t idle_timeout_millis = 0;
bool start_reaper_thread = false;
};

} // namespace pool
} // namespace dbconnector
12 changes: 8 additions & 4 deletions src/include/dbconnector/pool/connection_pool_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
#include "dbconnector/defer.hpp"

#include "dbconnector/pool/connection_pool.hpp"
#include "dbconnector/pool/connection_pool_config.hpp"
#include "dbconnector/pool/pool_exception.hpp"

namespace dbconnector {
namespace pool {

template <typename ConnectionT>
ConnectionPool<ConnectionT>::ConnectionPool(uint64_t max_connections_p, uint64_t timeout_millis_p,
ThreadLocalCacheState tl_cache_state)
: max_connections(max_connections_p), wait_timeout_millis(timeout_millis_p), total_connections(0),
shutdown_flag(false), tl_cache_enabled(ThreadLocalCacheState::CACHE_ENABLED == tl_cache_state) {
ConnectionPool<ConnectionT>::ConnectionPool(ConnectionPoolConfig config)
: max_connections(config.max_connections), wait_timeout_millis(config.wait_timeout_millis),
max_lifetime_millis(config.max_lifetime_millis), idle_timeout_millis(config.idle_timeout_millis),
tl_cache_enabled(config.tl_cache_enabled) {
if (config.start_reaper_thread) {
EnsureReaperRunning();
}
}

template <typename ConnectionT>
Expand Down
18 changes: 12 additions & 6 deletions test/test_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,9 @@ class TestConnection {

class TestConnectionPool : public dbconnector::pool::ConnectionPool<TestConnection> {
public:
TestConnectionPool(size_t max_connections = DEFAULT_POOL_SIZE, size_t timeout_ms = DEFAULT_POOL_TIMEOUT_MS,
bool tl_cache_enabled = true)
TestConnectionPool(size_t max_connections = 4, size_t timeout_ms = 30000, bool tl_cache_enabled = true)
: dbconnector::pool::ConnectionPool<TestConnection>(
max_connections, timeout_ms,
tl_cache_enabled
? dbconnector::pool::ConnectionPool<TestConnection>::ThreadLocalCacheState::CACHE_ENABLED
: dbconnector::pool::ConnectionPool<TestConnection>::ThreadLocalCacheState::CACHE_DISABLED) {
CreateConfig(max_connections, timeout_ms, tl_cache_enabled)) {
}

protected:
Expand All @@ -48,6 +44,16 @@ class TestConnectionPool : public dbconnector::pool::ConnectionPool<TestConnecti
void ResetConnection(TestConnection &) override {
// no-op
}

private:
static dbconnector::pool::ConnectionPoolConfig CreateConfig(size_t max_connections, size_t timeout_ms,
bool tl_cache_enabled = true) {
dbconnector::pool::ConnectionPoolConfig config;
config.max_connections = max_connections;
config.wait_timeout_millis = timeout_ms;
config.tl_cache_enabled = tl_cache_enabled;
return config;
}
};

TEST_CASE("Test connection pool basic", group_name) {
Expand Down
Loading