|
lintel 0.2.0
|
#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 |
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.
| 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.
| using lintel::db::ConnectionPool::HealthCheck = std::function<bool(const Connection &)> |
Reports whether a connection may return to the pool. Injectable for tests.
|
explicit |
Construct a pool.
| connectionEntry | connection definition the pool connects with |
| settings | pool size and acquire timeout |
| healthCheck | probe run before a connection returns to the pool; defaults to Connection::isHealthy (injectable for tests) |
| connector | factory for fresh connections; defaults to a connection built from connectionEntry (injectable for tests) |
|
delete |
| Lease lintel::db::ConnectionPool::acquire | ( | std::source_location | location = std::source_location::current() | ) |
Acquire a connection, waiting up to PoolSettings::acquireTimeout.
| location | caller location, recorded for diagnostics |
| PoolTimeoutException | on timeout, on re-entrancy (the calling thread already holds a lease of this pool) or when pooling is disabled (maxConnections == 0) |
| std::size_t lintel::db::ConnectionPool::available | ( | ) | const |
|
inline |
| const std::string & lintel::db::ConnectionPool::name | ( | ) | const |
|
delete |
| std::size_t lintel::db::ConnectionPool::outstanding | ( | ) | const |