Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
orbit::database::ConnectionPool< ClientType > Class Template Reference

A thread-safe connection pool for managing reusable database client connections. More...

#include <ConnectionPool.hpp>

Inheritance diagram for orbit::database::ConnectionPool< ClientType >:
Collaboration diagram for orbit::database::ConnectionPool< ClientType >:

Public Types

using ClientFactory = std::function< std::shared_ptr< ClientType >()>
 
using Connector = std::function< void(std::shared_ptr< ClientType >, std::function< void(bool)>)>
 

Public Member Functions

 ConnectionPool (size_t max_size, ClientFactory factory, PoolOptions< ClientType > options={})
 Constructs a new ConnectionPool.
 
void init (Connector connector, std::function< void(bool success)> on_ready)
 Initializes the pool by establishing the initial set of connections.
 
void acquire (std::function< void(std::shared_ptr< ClientType >)> callback)
 Acquires a database connection asynchronously from the pool.
 
void release (std::shared_ptr< ClientType > client)
 Releases a connection back to the pool.
 
size_t idle_count ()
 Idle clients ready to be acquired.
 
size_t waiting_count ()
 Acquirers waiting for a client.
 
size_t reconnecting_count ()
 Replacements being connected (or waiting out a backoff).
 

Private Member Functions

void replace (std::chrono::milliseconds delay)
 

Private Attributes

size_t max_size_
 
ClientFactory factory_
 
PoolOptions< ClientType > options_
 
std::mutex mutex_
 
Connector connector_
 
size_t reconnecting_ = 0
 
std::queue< std::shared_ptr< ClientType > > idle_connections_
 
std::queue< std::function< void(std::shared_ptr< ClientType >)> > wait_queue_
 

Detailed Description

template<typename ClientType>
class orbit::database::ConnectionPool< ClientType >

A thread-safe connection pool for managing reusable database client connections.

Template Parameters
ClientTypeThe type of database client to manage.

Member Typedef Documentation

◆ ClientFactory

template<typename ClientType >
using orbit::database::ConnectionPool< ClientType >::ClientFactory = std::function<std::shared_ptr<ClientType>()>

◆ Connector

template<typename ClientType >
using orbit::database::ConnectionPool< ClientType >::Connector = std::function<void(std::shared_ptr<ClientType>, std::function<void(bool)>)>

Constructor & Destructor Documentation

◆ ConnectionPool()

template<typename ClientType >
orbit::database::ConnectionPool< ClientType >::ConnectionPool ( size_t  max_size,
ClientFactory  factory,
PoolOptions< ClientType >  options = {} 
)
inline

Constructs a new ConnectionPool.

Parameters
max_sizeThe maximum number of connections to maintain in the pool.
factoryA callable that creates new instances of ClientType.
optionsHealth checking and reconnecting (off by default).

Member Function Documentation

◆ acquire()

template<typename ClientType >
void orbit::database::ConnectionPool< ClientType >::acquire ( std::function< void(std::shared_ptr< ClientType >)>  callback)
inline

Acquires a database connection asynchronously from the pool.

With a health check, an idle client that is no longer usable is dropped (and replaced in the background) rather than handed out.

Parameters
callbackCallback invoked with a shared pointer to a ready client when available.

◆ idle_count()

template<typename ClientType >
size_t orbit::database::ConnectionPool< ClientType >::idle_count ( )
inline

Idle clients ready to be acquired.

◆ init()

template<typename ClientType >
void orbit::database::ConnectionPool< ClientType >::init ( Connector  connector,
std::function< void(bool success)>  on_ready 
)
inline

Initializes the pool by establishing the initial set of connections.

Parameters
connectorA function used to connect a client asynchronously.
on_readyCallback invoked when the pool is initialized. The parameter is true if initialization was successful. With a health check, connections that failed are retried in the background.

◆ reconnecting_count()

template<typename ClientType >
size_t orbit::database::ConnectionPool< ClientType >::reconnecting_count ( )
inline

Replacements being connected (or waiting out a backoff).

◆ release()

template<typename ClientType >
void orbit::database::ConnectionPool< ClientType >::release ( std::shared_ptr< ClientType >  client)
inline

Releases a connection back to the pool.

With a health check, a client that is no longer usable is dropped and a replacement is connected in the background.

Parameters
clientThe client to return to the pool.

◆ replace()

template<typename ClientType >
void orbit::database::ConnectionPool< ClientType >::replace ( std::chrono::milliseconds  delay)
inlineprivate

◆ waiting_count()

template<typename ClientType >
size_t orbit::database::ConnectionPool< ClientType >::waiting_count ( )
inline

Acquirers waiting for a client.

Member Data Documentation

◆ connector_

template<typename ClientType >
Connector orbit::database::ConnectionPool< ClientType >::connector_
private

◆ factory_

template<typename ClientType >
ClientFactory orbit::database::ConnectionPool< ClientType >::factory_
private

◆ idle_connections_

template<typename ClientType >
std::queue<std::shared_ptr<ClientType> > orbit::database::ConnectionPool< ClientType >::idle_connections_
private

◆ max_size_

template<typename ClientType >
size_t orbit::database::ConnectionPool< ClientType >::max_size_
private

◆ mutex_

template<typename ClientType >
std::mutex orbit::database::ConnectionPool< ClientType >::mutex_
private

◆ options_

template<typename ClientType >
PoolOptions<ClientType> orbit::database::ConnectionPool< ClientType >::options_
private

◆ reconnecting_

template<typename ClientType >
size_t orbit::database::ConnectionPool< ClientType >::reconnecting_ = 0
private

◆ wait_queue_

template<typename ClientType >
std::queue<std::function<void(std::shared_ptr<ClientType>)> > orbit::database::ConnectionPool< ClientType >::wait_queue_
private

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