Template Class base_connection_pool

Class Documentation

template<typename Connector, typename Strategy = greedy_strategy<Connector>>
class stream_client::connector::base_connection_pool

Class to maintain filled pool of connected sockets (sockets).

This class own instance of Connector with which it automatically fills internal std::list of sockets (Connector::stream_type). Internal connector is responsible for DNS-resolution and spawning connected sockets via its’ new_session() method. Once stream in pulled from the pool with get_session() it will replaced with new one, for reuse of already established sockets you should return them back with return_session().

Note

Thread-safe. Single instance support concurrent operation.

tparam Connector

Type of connector to use to create sockets.

tparam Strategy

Type of reconnection strategy. For more info look in pool_strategy.hpp.

Public Types

using connector_type = typename std::remove_reference<Connector>::type
using stream_type = typename connector_type::stream_type
using protocol_type = typename stream_type::protocol_type
using clock_type = typename connector_type::clock_type
using time_duration_type = typename connector_type::time_duration_type
using time_point_type = typename connector_type::time_point_type

Public Functions

template<typename ...ArgN>
base_connection_pool(std::size_t size, time_duration_type idle_timeout, ArgN&&... argn)

Parametrized constructor.

Constructs pool for desired connector (protocol). Passed argn forwarded to Connector constructor. This operation starts background thread to fill the pool with opened sockets, therefore subsequent get_session() calls may take longer time compared with the state when pool is full.

Template Parameters

...ArgN – Types of argn.

Parameters
  • [in] size – Number of connected sockets to maintain in the pool. Note that real number of established connections my be size + 1. This happens when you pull a stream with get_session() , the pool establishes new one to replace it, and later you return pulled stream back with return_session().

  • [in] idle_timeout – sessions which are in the pool for a longer time are replaced with new ones.

  • [in] ...argn – Arguments to pass to Connector constructor.

template<typename Arg1, typename ...ArgN, typename std::enable_if<!std::is_convertible<Arg1, typename Connector::time_duration_type>::value>::type* = nullptr>
base_connection_pool(std::size_t size, Arg1 &&arg1, ArgN&&... argn)

Parametrized constructor.

Constructs pool for desired connector (protocol). Passed argn forwarded to Connector constructor. This operation starts background thread to fill the pool with opened sockets, therefore subsequent get_session() calls may take longer time compared with the state when pool is full.

Template Parameters

...ArgN – Types of argn.

Parameters
  • [in] size – Number of connected sockets to maintain in the pool. Note that real number of established connections my be size + 1. This happens when you pull a stream with get_session() , the pool establishes new one to replace it, and later you return pulled stream back with return_session().

  • [in] ...argn – Arguments to pass to Connector constructor.

base_connection_pool(const base_connection_pool<Connector> &other) = delete

Copy constructor is not permitted.

base_connection_pool<Connector> &operator=(const base_connection_pool<Connector> &other) = delete

Copy assignment is not permitted.

base_connection_pool(base_connection_pool<Connector> &&other) = delete

Move constructor is not permitted.

base_connection_pool<Connector> &operator=(base_connection_pool<Connector> &&other) = delete

Move assignment is not permitted.

virtual ~base_connection_pool()

Destructor.

std::unique_ptr<stream_type> get_session(boost::system::error_code &ec, const time_point_type &deadline)

Pull a session (stream) from the pool.

Tries to get stream from the pool until specified deadline is reached.

Note

Since returned stream was established prior to this call, there is no guarantee it’s still valid to send or receive data. It may be closed on server-side because of inactivity or anything else. It this case you may retry to get_session() until you get valid one.

Parameters
  • [in] deadline – Expiration time-point.

  • [out] ec – Set to indicate what error occurred, if any.

Returns

A stream wrapped in std::unique_ptr or nullptr.

inline std::unique_ptr<stream_type> get_session(boost::system::error_code &ec, const time_duration_type &timeout)

Pull a session (stream) from the pool.

Tries to get stream from the pool until specified timeout elapsed.

Note

Since returned stream was established prior to this call, there is no guarantee it’s still valid to send or receive data. It may be closed on server-side because of inactivity or anything else. It this case you may retry to get_session() until you get valid one.

Parameters
  • [in] timeout – Expiration duration.

  • [out] ec – Set to indicate what error occurred, if any.

Returns

A stream wrapped in std::unique_ptr or nullptr.

inline std::unique_ptr<stream_type> get_session(boost::system::error_code &ec)

Pull a session (stream) from the pool.

Get stream from the pool. Timeout for this operation is defined by default connection timeout (returned by get_connect_timeout).

Note

Since returned stream was established prior to this call, there is no guarantee it’s still valid to send or receive data. It may be closed on server-side because of inactivity or anything else. It this case you may retry to get_session() until you get valid one.

Parameters

[out] ec – Set to indicate what error occurred, if any.

Returns

A stream wrapped in std::unique_ptr or nullptr.

template<typename Time>
inline std::unique_ptr<stream_type> get_session(const Time &timeout_or_deadline)

Pull a session (stream) from the pool.

Get stream from the pool. Timeout for this operation is defined by default connection timeout (returned by get_connect_timeout).

Note

Since returned stream was established prior to this call, there is no guarantee it’s still valid to send or receive data. It may be closed on server-side because of inactivity or anything else. It this case you may retry to get_session() until you get valid one.

Parameters

[in] timeout_or_deadline – Expiration time-point or duration.

Throws

boost::system::system_error – Thrown on failure.

Returns

A stream wrapped in std::unique_ptr. Guaranteed to return valid pointer.

inline std::unique_ptr<stream_type> get_session()

Pull a session (stream) from the pool.

Get stream from the pool. Timeout for this operation is defined by default connection timeout (returned by get_connect_timeout).

Note

Since returned stream was established prior to this call, there is no guarantee it’s still valid to send or receive data. It may be closed on server-side because of inactivity or anything else. It this case you may retry to get_session() until you get valid one.

Throws

boost::system::system_error – Thrown on failure.

Returns

A stream wrapped in std::unique_ptr. Guaranteed to return valid pointer.

std::unique_ptr<stream_type> try_get_session(boost::system::error_code &ec, const time_point_type &deadline)

Try to pull a session (stream) from the pool.

Tries to get stream from the pool until specified deadline is reached.

Note

Unlike the get_session method, this method doesn’t wait to fill up the pool in a timeout.

Parameters
  • [in] deadline – Expiration time-point.

  • [out] ec – Set to indicate what error occurred, if any.

Returns

A stream wrapped in std::unique_ptr or nullptr.

inline std::unique_ptr<stream_type> try_get_session(boost::system::error_code &ec, const time_duration_type &timeout)

Try to pull a session (stream) from the pool.

Tries to get stream from the pool until specified timeout elapsed.

Note

Unlike the get_session method, this method doesn’t wait to fill up the pool in a timeout.

Parameters
  • [in] timeout – Expiration duration.

  • [out] ec – Set to indicate what error occurred, if any.

Returns

A stream wrapped in std::unique_ptr or nullptr.

void return_session(std::unique_ptr<stream_type> &&session)

Return the session pulled earlier from the pool.

Remark

Return session only if you don’t expect incoming data to appear or handle such cases by yourself.

Note

Is is better to return sessions after successful usage, otherwise pool will try to refill itself and if you pulling sessions fast enough you will get new sockets spawned constantly.

bool is_connected(boost::system::error_code &ec, const time_point_type &deadline) const

Check if pool has at least one stream.

Waits until deadline for pool to become non-empty.

Parameters
  • [in] deadline – Expiration time-point.

  • [out] ec – Set to boost::asio::error::timed_out if failed to acquire lock over the pool.

Returns

true - if pool is not empty; false - is it is empty or timeout reached.

inline bool is_connected(boost::system::error_code &ec, const time_duration_type &timeout) const

Check if pool has at least one stream.

Waits for timeout for pool to become non-empty.

Parameters
  • [in] deadline – Expiration duration.

  • [out] ec – Set to boost::asio::error::timed_out if failed to acquire lock over the pool.

Returns

true - if pool is not empty; false - is it is empty or timeout reached.

inline bool is_connected(boost::system::error_code &ec) const

Check if pool has at least one stream.

Waits for pool to become non-empty. Timeout for this operation is defined by default connection timeout (returned by get_connect_timeout).

Parameters

[out] ec – Set to boost::asio::error::timed_out if failed to acquire lock over the pool.

Returns

true - if pool is not empty; false - is it is empty or timeout reached.

inline bool is_connected() const

Check if pool has at least one stream.

Waits for pool to become non-empty. Timeout for this operation is defined by default connection timeout (returned by get_connect_timeout).

Throws

boost::system::system_error – Thrown with boost::asio::error::timed_out in case of timeout.

Returns

true - if pool is not empty; false - is it is empty.

inline const time_duration_type &get_resolve_timeout() const

Get resolve operations timeout.

Returns

A const reference to the timeout.

inline const time_duration_type &get_connect_timeout() const

Get connect operations default timeout.

Returns

A const reference to the timeout.

inline const time_duration_type &get_operation_timeout() const

Get I/O timeout for operations on sockets in the pool.

Returns

A const reference to the timeout.