lintel 0.2.0
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Friends | List of all members
lintel::db::ConnectionPool Class Reference

#include <ConnectionPool.h>

Classes

class  Lease
 

Public Types

using Connector = std::function< std::unique_ptr< Connection >()>
 
using HealthCheck = std::function< bool(const Connection &)>
 

Public Member Functions

 ConnectionPool (std::shared_ptr< DatabaseConnectionEntry > connectionEntry, PoolSettings settings, HealthCheck healthCheck={}, Connector connector={})
 
 ConnectionPool (const ConnectionPool &)=delete
 
ConnectionPool & operator= (const ConnectionPool &)=delete
 
Lease acquire (std::source_location location=std::source_location::current())
 
const std::string & name () const
 
std::size_t maxConnections () const
 
std::size_t outstanding () const
 
std::size_t available () const
 

Friends

class Lease
 

Detailed Description

A bounded pool of database connections handed out through RAII leases.

The pool opens a TCP connection (connect, auth, optional TLS) once per slot instead of once per call and hands the slot out through a Lease. All operations are thread-safe; a caller that has to wait longer than PoolSettings::acquireTimeout gets a PoolTimeoutException that lists the outstanding leases.

Re-entrancy is detected: a thread that already holds a lease of this pool and calls acquire() again fails immediately instead of waiting for the timeout. This is the typical poolSize=1 + nested-call deadlock, which an unbounded wait would only surface much later.

On release the connection is probed by Connection::isHealthy (see the injectable HealthCheck); a broken connection is closed and dropped, so the next acquire() builds a fresh one instead of handing out a dead handle.

State leaks across reuse are the caller's responsibility: a pooled connection keeps its session state (temporary tables, SET, prepared statements), so code that runs on a lease must not rely on connection-local state surviving a release. See the Connection pooling section of docs/architecture.dox.

Member Typedef Documentation

◆ Connector

using lintel::db::ConnectionPool::Connector = std::function<std::unique_ptr<Connection>()>

Creates a fresh connection for the pool. Defaults to a connection built from the entry.

◆ HealthCheck

Reports whether a connection may return to the pool. Injectable for tests.

Constructor & Destructor Documentation

◆ ConnectionPool() [1/2]

lintel::db::ConnectionPool::ConnectionPool ( std::shared_ptr< DatabaseConnectionEntry >  connectionEntry,
PoolSettings  settings,
HealthCheck  healthCheck = {},
Connector  connector = {} 
)
explicit

Construct a pool.

Parameters
connectionEntryconnection definition the pool connects with
settingspool size and acquire timeout
healthCheckprobe run before a connection returns to the pool; defaults to Connection::isHealthy (injectable for tests)
connectorfactory for fresh connections; defaults to a connection built from connectionEntry (injectable for tests)

◆ ConnectionPool() [2/2]

lintel::db::ConnectionPool::ConnectionPool ( const ConnectionPool &  )
delete

Member Function Documentation

◆ acquire()

Lease lintel::db::ConnectionPool::acquire ( std::source_location  location = std::source_location::current())

Acquire a connection, waiting up to PoolSettings::acquireTimeout.

Parameters
locationcaller location, recorded for diagnostics
Returns
a lease owning the connection
Exceptions
PoolTimeoutExceptionon timeout, on re-entrancy (the calling thread already holds a lease of this pool) or when pooling is disabled (maxConnections == 0)

◆ available()

std::size_t lintel::db::ConnectionPool::available ( ) const
Returns
number of idle connections parked in the pool (for tests and diagnostics)

◆ maxConnections()

std::size_t lintel::db::ConnectionPool::maxConnections ( ) const
inline
Returns
the configured maximum number of connections

◆ name()

const std::string & lintel::db::ConnectionPool::name ( ) const
Returns
the configured connection name

◆ operator=()

ConnectionPool & lintel::db::ConnectionPool::operator= ( const ConnectionPool &  )
delete

◆ outstanding()

std::size_t lintel::db::ConnectionPool::outstanding ( ) const
Returns
number of connections currently handed out (for tests and diagnostics)

Friends And Related Symbol Documentation

◆ Lease

friend class Lease
friend

The documentation for this class was generated from the following file: