7#include <condition_variable>
50 return std::format(
"Connection pool error: {} (Code: {})", error.
message, error.
error_code);
65 std::shared_ptr<PostgreSQLConnection> connection;
66 std::chrono::steady_clock::time_point last_used;
85 return std::shared_ptr<PostgreSQLConnectionPool>(
118 template <
typename Func>
121 using ResultType = std::invoke_result_t<Func, PooledConnection&>;
126 return std::unexpected(conn_result.error());
131 if constexpr (std::is_same_v<ResultType, void>) {
135 return func(*conn_result);
146 std::shared_ptr<PostgreSQLConnection> connection_;
147 std::weak_ptr<PostgreSQLConnectionPool> pool_;
154 const std::shared_ptr<PostgreSQLConnectionPool>& pool)
155 : connection_(
std::move(connection)), pool_(pool) {}
161 if (
auto pool = pool_.lock()) {
162 pool->return_connection(std::move(connection_));
183 explicit operator bool()
const {
return connection_ !=
nullptr; }
188 std::atomic<size_t> active_connections_{0};
189 std::atomic<size_t> total_connections_{0};
191 mutable std::mutex pool_mutex_;
192 std::condition_variable conn_available_;
193 std::queue<PoolEntry> idle_connections_;
197 [[nodiscard]] ConnectionPoolResult<std::shared_ptr<PostgreSQLConnection>> get_raw_connection();
201 void return_connection(std::shared_ptr<PostgreSQLConnection> connection);
205 ConnectionPoolResult<std::shared_ptr<PostgreSQLConnection>> create_connection();
210 static bool validate_connection(
const std::shared_ptr<PostgreSQLConnection>& connection);
213 void cleanup_idle_connections();
A wrapper for a connection that automatically returns it to the pool.
PostgreSQLConnection * operator->()
Forward -> operator to the underlying connection.
PooledConnection(const PooledConnection &)=delete
PooledConnection(std::shared_ptr< PostgreSQLConnection > connection, const std::shared_ptr< PostgreSQLConnectionPool > &pool)
Constructor takes a connection and its parent pool.
PooledConnection & operator=(PooledConnection &&)=default
PooledConnection(PooledConnection &&)=default
const PostgreSQLConnection * operator->() const
Forward const -> operator to the underlying connection.
PooledConnection & operator=(const PooledConnection &)=delete
~PooledConnection()
Destructor automatically returns connection to pool if available.
PostgreSQL connection pool that manages a collection of PostgreSQL connections.
PostgreSQLConnectionPool & operator=(const PostgreSQLConnectionPool &)=delete
ConnectionPoolResult< PooledConnection > get_connection()
Get a connection from the pool with automatic return when out of scope.
ConnectionPoolResult< void > initialize()
Initialize the connection pool.
auto with_connection(Func &&func) -> ConnectionPoolResult< std::invoke_result_t< Func, PooledConnection & > >
Execute a function with a connection from the pool.
size_t active_connections() const
Get the current number of active connections.
size_t idle_connections() const
Get the current number of idle connections.
PostgreSQLConnectionPool(const PostgreSQLConnectionPool &)=delete
PostgreSQLConnectionPool(PostgreSQLConnectionPool &&)=delete
~PostgreSQLConnectionPool()
Destructor that cleans up all connections.
static std::shared_ptr< PostgreSQLConnectionPool > create(PostgreSQLConnectionPoolConfig config)
Create a new connection pool.
PostgreSQLConnectionPool & operator=(PostgreSQLConnectionPool &&)=delete
PostgreSQL implementation of the Connection interface.
std::expected< T, ConnectionPoolError > ConnectionPoolResult
Type alias for result of connection pool operations.
std::string format_error(const ConnectionPoolError &error)
Format a ConnectionPoolError for exception messages.
Error type for connection pool operations.
Basic parameters for a PostgreSQL connection.
Configuration for PostgreSQL connection pool.
bool validate_connections
Whether to validate connections before returning them.
PostgreSQLConnectionParams connection_params
Connection parameters for PostgreSQL.
std::chrono::milliseconds connection_timeout
Maximum time to wait for a connection before timeout (ms)
std::chrono::milliseconds max_idle_time
Maximum idle time before a connection is closed (ms)
size_t max_size
Maximum number of connections allowed.
size_t initial_size
Initial number of connections to create.