7#ifndef BOOST_REDIS_CONNECTION_HPP
8#define BOOST_REDIS_CONNECTION_HPP
10#include <boost/redis/detail/connection_base.hpp>
11#include <boost/redis/logger.hpp>
12#include <boost/redis/config.hpp>
13#include <boost/asio/io_context.hpp>
14#include <boost/asio/coroutine.hpp>
15#include <boost/asio/steady_timer.hpp>
16#include <boost/asio/any_io_executor.hpp>
17#include <boost/asio/any_completion_handler.hpp>
23namespace boost::redis {
26template <
class Connection,
class Logger>
27struct reconnection_op {
28 Connection* conn_ =
nullptr;
30 asio::coroutine coro_{};
33 void operator()(Self& self, system::error_code ec = {})
35 BOOST_ASIO_CORO_REENTER (coro_)
for (;;)
38 conn_->impl_.async_run(conn_->cfg_, logger_, std::move(self));
40 logger_.on_connection_lost(ec);
41 if (!conn_->will_reconnect() || is_cancelled(self)) {
43 self.complete(!!ec ? ec : asio::error::operation_aborted);
47 conn_->timer_.expires_after(conn_->cfg_.reconnect_wait_interval);
49 conn_->timer_.async_wait(std::move(self));
50 BOOST_REDIS_CHECK_OP0(;)
51 if (!conn_->will_reconnect()) {
52 self.complete(asio::error::operation_aborted);
55 conn_->reset_stream();
71template <
class Executor>
82 template <
class Executor1>
99 asio::ssl::context ctx = asio::ssl::context{asio::ssl::context::tlsv12_client},
100 std::size_t max_read_size = (std::numeric_limits<std::size_t>::max)())
101 : impl_{ex, std::move(ctx), max_read_size}
108 asio::io_context& ioc,
109 asio::ssl::context ctx = asio::ssl::context{asio::ssl::context::tlsv12_client},
110 std::size_t max_read_size = (std::numeric_limits<std::size_t>::max)())
152 class Logger = logger,
153 class CompletionToken = asio::default_completion_token_t<executor_type>>
158 CompletionToken token = CompletionToken{})
160 using this_type = basic_connection<executor_type>;
164 return asio::async_compose
166 , void(system::error_code)
167 >(detail::reconnection_op<this_type, Logger>{
this, l}, token, timer_);
192 template <
class CompletionToken = asio::default_completion_token_t<executor_type>>
194 {
return impl_.async_receive(std::move(token)); }
210 return impl_.receive(ec);
215 class CompletionToken = asio::default_completion_token_t<executor_type>
217 [[deprecated(
"Set the response with set_receive_response and use the other overload.")]]
221 CompletionToken token = CompletionToken{})
223 return impl_.async_receive(
response, token);
251 class CompletionToken = asio::default_completion_token_t<executor_type>
257 CompletionToken&& token = CompletionToken{})
259 return impl_.async_exec(req, resp, std::forward<CompletionToken>(token));
308 template <
class Response>
310 { impl_.set_receive_response(
response); }
314 {
return impl_.get_usage(); }
318 asio::basic_waitable_timer<
319 std::chrono::steady_clock,
320 asio::wait_traits<std::chrono::steady_clock>,
323 template <
class,
class>
friend struct detail::reconnection_op;
348 asio::ssl::context ctx = asio::ssl::context{asio::ssl::context::tlsv12_client},
349 std::size_t max_read_size = (std::numeric_limits<std::size_t>::max)());
354 asio::io_context& ioc,
355 asio::ssl::context ctx = asio::ssl::context{asio::ssl::context::tlsv12_client},
356 std::size_t max_read_size = (std::numeric_limits<std::size_t>::max)());
363 template <
class CompletionToken>
366 return asio::async_initiate<
367 CompletionToken, void(boost::system::error_code)>(
370 self->async_run_impl(*cfg, l, std::move(handler));
371 }, token,
this, &cfg, l);
375 template <
class Response,
class CompletionToken>
376 [[deprecated(
"Set the response with set_receive_response and use the other overload.")]]
383 template <
class CompletionToken>
394 template <
class Response,
class CompletionToken>
397 return impl_.
async_exec(req, resp, std::move(token));
420 template <
class Response>
437 asio::any_completion_handler<
void(boost::system::error_code)> token);
A SSL connection to the Redis server.
void reset_stream()
Resets the underlying stream.
bool will_reconnect() const noexcept
Returns true if the connection was canceled.
executor_type get_executor() noexcept
Returns the underlying executor.
auto async_exec(request const &req, Response &resp=ignore, CompletionToken &&token=CompletionToken{})
Executes commands on the Redis server asynchronously.
usage get_usage() const noexcept
Returns connection usage information.
auto const & get_ssl_context() const noexcept
Returns the ssl context.
basic_connection(asio::io_context &ioc, asio::ssl::context ctx=asio::ssl::context{asio::ssl::context::tlsv12_client}, std::size_t max_read_size=(std::numeric_limits< std::size_t >::max)())
Contructs from a context.
auto const & next_layer() const noexcept
Returns a const reference to the next layer.
basic_connection(executor_type ex, asio::ssl::context ctx=asio::ssl::context{asio::ssl::context::tlsv12_client}, std::size_t max_read_size=(std::numeric_limits< std::size_t >::max)())
Constructor.
void set_receive_response(Response &response)
Sets the response object of async_receive operations.
void cancel(operation op=operation::all)
Cancel operations.
auto & next_layer() noexcept
Returns a reference to the next layer.
std::size_t receive(system::error_code &ec)
Receives server pushes synchronously without blocking.
auto async_receive(CompletionToken token=CompletionToken{})
Receives server side pushes asynchronously.
auto async_run(config const &cfg={}, Logger l=Logger{}, CompletionToken token=CompletionToken{})
Starts underlying connection operations.
Executor executor_type
Executor type.
Rebinds the socket type to another executor.
A basic_connection that type erases the executor.
bool will_reconnect() const noexcept
Calls boost::redis::basic_connection::will_reconnect.
auto async_exec(request const &req, Response &resp, CompletionToken token)
Calls boost::redis::basic_connection::async_exec.
auto async_receive(Response &response, CompletionToken token)
Calls boost::redis::basic_connection::async_receive.
std::size_t receive(system::error_code &ec)
Calls boost::redis::basic_connection::receive.
auto async_run(config const &cfg, logger l, CompletionToken token)
Calls boost::redis::basic_connection::async_run.
connection(executor_type ex, asio::ssl::context ctx=asio::ssl::context{asio::ssl::context::tlsv12_client}, std::size_t max_read_size=(std::numeric_limits< std::size_t >::max)())
Contructs from an executor.
asio::any_io_executor executor_type
Executor type.
connection(asio::io_context &ioc, asio::ssl::context ctx=asio::ssl::context{asio::ssl::context::tlsv12_client}, std::size_t max_read_size=(std::numeric_limits< std::size_t >::max)())
Contructs from a context.
auto & next_layer() noexcept
Calls boost::redis::basic_connection::next_layer.
auto const & get_ssl_context() const noexcept
Returns the ssl context.
void set_receive_response(Response &response)
Sets the response object of async_receive operations.
auto const & next_layer() const noexcept
Calls boost::redis::basic_connection::next_layer.
void cancel(operation op=operation::all)
Calls boost::redis::basic_connection::cancel.
usage get_usage() const noexcept
Returns connection usage information.
auto async_receive(CompletionToken token)
Calls boost::redis::basic_connection::async_receive.
executor_type get_executor() noexcept
Returns the underlying executor.
void reset_stream()
Calls boost::redis::basic_connection::reset_stream.
Base class for high level Redis asynchronous connections.
auto get_executor()
Returns the associated executor.
void reset_stream()
Resets the underlying stream.
auto & next_layer() noexcept
Returns a reference to the next layer.
auto const & get_ssl_context() const noexcept
Returns the ssl context.
void cancel(operation op)
Cancels specific operations.
std::chrono::steady_clock::duration reconnect_wait_interval
Time waited before trying a reconnection.
std::string log_prefix
Logger prefix, see boost::redis::logger.
ignore_t ignore
Global ignore object.
std::decay_t< decltype(std::ignore)> ignore_t
Type used to ignore responses.
std::tuple< adapter::result< Ts >... > response
Response with compile-time size.
operation
Connection operations that can be cancelled.
@ reconnection
Cancels reconnection.
@ all
Refers to all operations.
@ receive
Refers to connection::async_receive operations.
Configure parameters used by the connection classes.
Connection usage information.