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_);
186 template <
class CompletionToken = asio::default_completion_token_t<executor_type>>
188 {
return impl_.async_receive(std::move(token)); }
204 return impl_.receive(ec);
209 class CompletionToken = asio::default_completion_token_t<executor_type>
211 [[deprecated(
"Set the response with set_receive_response and use the other overload.")]]
215 CompletionToken token = CompletionToken{})
217 return impl_.async_receive(
response, token);
245 class CompletionToken = asio::default_completion_token_t<executor_type>
251 CompletionToken&& token = CompletionToken{})
253 return impl_.async_exec(req, resp, std::forward<CompletionToken>(token));
306 template <
class Response>
308 { impl_.set_receive_response(
response); }
312 {
return impl_.get_usage(); }
316 asio::basic_waitable_timer<
317 std::chrono::steady_clock,
318 asio::wait_traits<std::chrono::steady_clock>,
321 template <
class,
class>
friend struct detail::reconnection_op;
346 asio::ssl::context::method method = asio::ssl::context::tls_client,
347 std::size_t max_read_size = (std::numeric_limits<std::size_t>::max)());
352 asio::io_context& ioc,
353 asio::ssl::context::method method = asio::ssl::context::tls_client,
354 std::size_t max_read_size = (std::numeric_limits<std::size_t>::max)());
361 template <
class CompletionToken>
364 return asio::async_initiate<
365 CompletionToken, void(boost::system::error_code)>(
368 self->async_run_impl(*cfg, l, std::move(handler));
369 }, token,
this, &cfg, l);
373 template <
class Response,
class CompletionToken>
374 [[deprecated(
"Set the response with set_receive_response and use the other overload.")]]
381 template <
class CompletionToken>
392 template <
class Response,
class CompletionToken>
395 return impl_.
async_exec(req, resp, std::move(token));
418 template <
class Response>
431 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.
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.
usage get_usage() const noexcept
Returns connection usage information.
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.
void set_receive_response(Response &response)
Sets the response object of async_receive operations.
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.
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.
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.
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.
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.
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.