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>
93 asio::ssl::context::method method = asio::ssl::context::tls_client,
94 std::size_t max_read_size = (std::numeric_limits<std::size_t>::max)())
95 : impl_{ex, method, max_read_size}
102 asio::io_context& ioc,
103 asio::ssl::context::method method = asio::ssl::context::tls_client,
104 std::size_t max_read_size = (std::numeric_limits<std::size_t>::max)())
147 class CompletionToken = asio::default_completion_token_t<executor_type>>
152 CompletionToken token = CompletionToken{})
154 using this_type = basic_connection<executor_type>;
158 return asio::async_compose
160 , void(system::error_code)
161 >(detail::reconnection_op<this_type, Logger>{
this, l}, token, timer_);
189 class CompletionToken = asio::default_completion_token_t<executor_type>
194 CompletionToken token = CompletionToken{})
196 return impl_.async_receive(
response, token);
224 class CompletionToken = asio::default_completion_token_t<executor_type>
230 CompletionToken&& token = CompletionToken{})
232 return impl_.async_exec(req, resp, std::forward<CompletionToken>(token));
287 asio::basic_waitable_timer<
288 std::chrono::steady_clock,
289 asio::wait_traits<std::chrono::steady_clock>,
292 template <
class,
class>
friend struct detail::reconnection_op;
317 asio::ssl::context::method method = asio::ssl::context::tls_client,
318 std::size_t max_read_size = (std::numeric_limits<std::size_t>::max)());
323 asio::io_context& ioc,
324 asio::ssl::context::method method = asio::ssl::context::tls_client,
325 std::size_t max_read_size = (std::numeric_limits<std::size_t>::max)());
332 template <
class CompletionToken>
335 return asio::async_initiate<
336 CompletionToken, void(boost::system::error_code)>(
339 self->async_run_impl(*cfg, l, std::move(handler));
340 }, token,
this, &cfg, l);
344 template <
class Response,
class CompletionToken>
351 template <
class Response,
class CompletionToken>
354 return impl_.
async_exec(req, resp, std::move(token));
381 asio::any_completion_handler<
void(boost::system::error_code)> token);
A SSL connection to the Redis server.
void reset_stream()
Resets the underlying stream.
auto async_receive(Response &response, CompletionToken token=CompletionToken{})
Receives server side pushes asynchronously.
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.
basic_connection(executor_type ex, asio::ssl::context::method method=asio::ssl::context::tls_client, std::size_t max_read_size=(std::numeric_limits< std::size_t >::max)())
Contructs from an executor.
auto & get_ssl_context() noexcept
Returns the ssl context.
auto const & get_ssl_context() const noexcept
Returns the ssl context.
auto const & next_layer() const noexcept
Returns a const reference to the next layer.
basic_connection(asio::io_context &ioc, asio::ssl::context::method method=asio::ssl::context::tls_client, std::size_t max_read_size=(std::numeric_limits< std::size_t >::max)())
Contructs from a context.
void cancel(operation op=operation::all)
Cancel operations.
auto & next_layer() noexcept
Returns a reference to the next layer.
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.
auto async_run(config const &cfg, logger l, CompletionToken token)
Calls boost::redis::basic_connection::async_run.
asio::any_io_executor executor_type
Executor type.
connection(executor_type ex, asio::ssl::context::method method=asio::ssl::context::tls_client, std::size_t max_read_size=(std::numeric_limits< std::size_t >::max)())
Contructs from an executor.
auto & next_layer() noexcept
Calls boost::redis::basic_connection::next_layer.
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.
connection(asio::io_context &ioc, asio::ssl::context::method method=asio::ssl::context::tls_client, std::size_t max_read_size=(std::numeric_limits< std::size_t >::max)())
Contructs from a context.
executor_type get_executor() noexcept
Returns the underlying executor.
void reset_stream()
Calls boost::redis::basic_connection::reset_stream.
auto get_executor()
Returns the associated executor.
virtual void cancel(operation op)
Cancels specific operations.
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.
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.