#include <ozo/connection_pool.h>
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:
ConnectionSource
creates a new connection and provides it to a user.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
.
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. |
See examples/connection_pool.cpp.
Creating the connection_pool instance:
Creating a ConnectionProvider from the connection_pool instance:
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... | |
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
ozo::connection_pool< Source, ThreadSafety >::connection_pool | ( | Source | source, |
const connection_pool_config & | config, | ||
const ThreadSafety & | = ThreadSafety{} |
||
) |
Construct a new connection pool object
source | — ConnectionSource 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> ). |
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.
io | — io_context for the connection IO. |
t | — operation time constraint. |
handler | — Handler. |