OZO 「お象」
Boost.Asio and libpq based asynchronous PostgreSQL unofficial header-only C++17 client library.
ozo::connection_pool< Source, ThreadSafety > Class Template Reference

#include <ozo/connection_pool.h>

Description

template<typename Source, typename ThreadSafety = std::decay_t<decltype(thread_safe)>>
class ozo::connection_pool< Source, ThreadSafety >

Connection pool implementation.

This is a simple implementation connection pool (wikipedia). Connection pool allows to store established connections and reuse it to avoid a connect operation for each request. It supports asynchronous request for connections using a queue with optional limits of capacity and wait time. Also connection in the pool may be closed when it is not used for some time - idle timeout.

This is how connection_pool handles user request to get a Connection object:

  • If there is a free connection — it will be provided for a user immediately.
  • If all connections are busy but its number less than the limit, ConnectionSource creates a new connection and provides it to a user.
  • If all connections are busy and there is no room to create a new one — the request will be placed into the internal queue to wait for the free connection.

The request may be limited by time via optional connection_pool_timeouts argument of the connection_pool::operator().

connection_pool models ConnectionSource concept itself using underlying ConnectionSource.

Template Parameters
Source— underlying ConnectionSource which is being used to create connection to a database.
ThreadSafety— admissibility to use in multithreaded environment without additional synchronization. Thread safe by default.

Example

See examples/connection_pool.cpp.

Creating the connection_pool instance:

// To make a connection to a database we need to make a ConnectionSource.
const ozo::connection_info connection_info(argv[1]);
ozo::connection_pool_config connection_pool_config;
// Maximum limit for number of stored connections
connection_pool_config.capacity = 1;
// Maximum limit for number of waiting requests for connection
connection_pool_config.queue_capacity = 10;
// Maximum time duration to store unused open connection
connection_pool_config.idle_timeout = std::chrono::seconds(60);
// Maximum time duration to keep connection open
connection_pool_config.lifespan = std::chrono::hours(24);
// Creating connection pool from connection_info as the underlying ConnectionSource
ozo::connection_pool connection_pool(connection_info, connection_pool_config);

Creating a ConnectionProvider from the connection_pool instance:

// The next step is bind io_context with ConnectionSource to setup executor for all
// callbacks. This line is for the exposition and education purpose only.
// The best practice is to use simple inline call like
//
// ozo::request(connection_pool[io], query, ozo::deadline(1s), ozo::into(res), yield);
//
const auto connector = connection_pool[io];
Models
ConnectionSource

Types

using connection_type = std::shared_ptr< pooled_connection< yamail::resource_pool::handle< connection_rep_type > >>
 

Public Member Functions

 connection_pool (Source source, const connection_pool_config &config, const ThreadSafety &=ThreadSafety{})
 
template<typename TimeConstraint , typename Handler >
void operator() (io_context &io, TimeConstraint t, Handler &&handler)
 

Related Functions

(Note that these are not member functions.)

template<typename ConnectionSource , typename ThreadSafety = decltype(thread_safe)>
auto make_connection_pool (ConnectionSource &&source, const connection_pool_config &config, const ThreadSafety &thread_safety=ThreadSafety{})
 Connection pool construct helper function. More...
 

Types

◆ connection_type

template<typename Source , typename ThreadSafety = std::decay_t<decltype(thread_safe)>>
using ozo::connection_pool< Source, ThreadSafety >::connection_type = std::shared_ptr<pooled_connection<yamail::resource_pool::handle<connection_rep_type> >>

Type of connection depends on connection type of Source. The definition is used to model ConnectionSource

Constructors

◆ connection_pool()

template<typename Source , typename ThreadSafety = std::decay_t<decltype(thread_safe)>>
ozo::connection_pool< Source, ThreadSafety >::connection_pool ( Source  source,
const connection_pool_config config,
const ThreadSafety &  = ThreadSafety{} 
)

Construct a new connection pool object

Parameters
sourceConnectionSource object which is being used to create connection to a database.
config— pool configuration.
thread_safety– admissibility to use in multithreaded environment without additional synchronization. Thread safe by default (ozo::thread_safety<true>).

Member Function Documentation

◆ operator()()

template<typename Source , typename ThreadSafety >
template<typename TimeConstraint , typename Handler >
void ozo::connection_pool< Source, ThreadSafety >::operator() ( io_context &  io,
TimeConstraint  t,
Handler &&  handler 
)

Get connection is bound to the given io_context object. This operation has a time constrain and would be interrupted if the time constrain expired by cancelling IO on a Connection or wait operation in the pool's queue.

Parameters
ioio_context for the connection IO.
t— operation time constraint.
handlerHandler.
ozo::connection_pool
Connection pool implementation.
Definition: connection_pool.h:302
ozo::connection_pool_config::idle_timeout
time_traits::duration idle_timeout
time interval to close connection after last usage
Definition: connection_pool.h:23
ozo::connection_pool_config::capacity
std::size_t capacity
maximum number of stored connections
Definition: connection_pool.h:21
ozo::connection_pool::connection_pool
connection_pool(Source source, const connection_pool_config &config, const ThreadSafety &=ThreadSafety{})
Definition: connection_pool.h:317
ozo::connection_pool_config
Connection pool configuration.
Definition: connection_pool.h:20
ozo::connection_info
Connection source to a single host.
Definition: connection_info.h:28
ozo::connection_pool_config::lifespan
time_traits::duration lifespan
time interval to keep connection open
Definition: connection_pool.h:24
ozo::connection_pool_config::queue_capacity
std::size_t queue_capacity
maximum number of queued requests to get available connection
Definition: connection_pool.h:22