@@ -43,37 +43,30 @@ class connection :
43
43
44
44
/* * @brief Connection configuration parameters.
45
45
*/
46
- struct config {
46
+ struct timeouts {
47
47
// / Timeout of the resolve operation.
48
- std::chrono::milliseconds resolve_timeout = std::chrono::seconds{10 };
48
+ std::chrono::steady_clock::duration resolve_timeout = std::chrono::seconds{10 };
49
49
50
50
// / Timeout of the connect operation.
51
- std::chrono::milliseconds connect_timeout = std::chrono::seconds{10 };
51
+ std::chrono::steady_clock::duration connect_timeout = std::chrono::seconds{10 };
52
52
53
53
// / Timeout of the resp3 handshake operation.
54
- std::chrono::milliseconds resp3_handshake_timeout = std::chrono::seconds{2 };
54
+ std::chrono::steady_clock::duration resp3_handshake_timeout = std::chrono::seconds{2 };
55
55
56
56
// / Time interval of ping operations.
57
- std::chrono::milliseconds ping_interval = std::chrono::seconds{1 };
57
+ std::chrono::steady_clock::duration ping_interval = std::chrono::seconds{1 };
58
58
};
59
59
60
60
// / Constructor
61
- explicit connection (executor_type ex, config cfg = {} )
61
+ explicit connection (executor_type ex)
62
62
: base_type{ex}
63
- , cfg_{cfg}
64
63
, stream_{ex}
65
64
{}
66
65
67
- explicit connection (boost::asio::io_context& ioc, config cfg = config{} )
68
- : connection(ioc.get_executor(), std::move(cfg) )
66
+ explicit connection (boost::asio::io_context& ioc)
67
+ : connection(ioc.get_executor())
69
68
{ }
70
69
71
- // / Returns a reference to the configuration parameters.
72
- auto get_config () noexcept -> config& { return cfg_;}
73
-
74
- // / Returns a const reference to the configuration parameters.
75
- auto get_config () const noexcept -> config const & { return cfg_;}
76
-
77
70
// / Resets the underlying stream.
78
71
void reset_stream ()
79
72
{
@@ -90,6 +83,97 @@ class connection :
90
83
// / Returns a const reference to the next layer.
91
84
auto next_layer () const noexcept -> auto const & { return stream_; }
92
85
86
+ /* * @brief Starts communication with the Redis server asynchronously.
87
+ *
88
+ * This function performs the following steps
89
+ *
90
+ * @li Resolves the Redis host as of `async_resolve` with the
91
+ * timeout passed in the base class `connection::timeouts::resolve_timeout`.
92
+ *
93
+ * @li Connects to one of the endpoints returned by the resolve
94
+ * operation with the timeout passed in the base class
95
+ * `connection::timeouts::connect_timeout`.
96
+ *
97
+ * @li Performs a RESP3 handshake by sending a
98
+ * [HELLO](https://redis.io/commands/hello/) command with protocol
99
+ * version 3 and the credentials contained in the
100
+ * `aedis::endpoint` object. The timeout used is the one specified
101
+ * in `connection::timeouts::resp3_handshake_timeout`.
102
+ *
103
+ * @li Erases any password that may be contained in
104
+ * `endpoint::password`.
105
+ *
106
+ * @li Checks whether the server role corresponds to the one
107
+ * specifed in the `endpoint`. If `endpoint::role` is left empty,
108
+ * no check is performed. If the role role is different than the
109
+ * expected `async_run` will complete with
110
+ * `error::unexpected_server_role`.
111
+ *
112
+ * @li Starts healthy checks with a timeout twice the value of
113
+ * `connection::timeouts::ping_interval`. If no data is received during that
114
+ * time interval `connection::async_run` completes with
115
+ * `error::idle_timeout`.
116
+ *
117
+ * @li Starts the healthy check operation that sends the
118
+ * [PING](https://redis.io/commands/ping/) to Redis with a
119
+ * frequency equal to `connection::timeouts::ping_interval`.
120
+ *
121
+ * @li Starts reading from the socket and executes all requests
122
+ * that have been started prior to this function call.
123
+ *
124
+ * @param ep Redis endpoint.
125
+ * @param ts Timeouts used by the operations.
126
+ * @param token Completion token.
127
+ *
128
+ * The completion token must have the following signature
129
+ *
130
+ * @code
131
+ * void f(boost::system::error_code);
132
+ * @endcode
133
+ */
134
+ template <class CompletionToken = boost::asio::default_completion_token_t <executor_type>>
135
+ auto
136
+ async_run (
137
+ endpoint ep,
138
+ timeouts ts = timeouts{},
139
+ CompletionToken token = CompletionToken{})
140
+ {
141
+ return base_type::async_run (ep, ts, std::move (token));
142
+ }
143
+
144
+ /* * @brief Connects and executes a request asynchronously.
145
+ *
146
+ * Combines the other `async_run` overload with `async_exec` in a
147
+ * single function. This function is useful for users that want to
148
+ * send a single request to the server and close it.
149
+ *
150
+ * @param ep Redis endpoint.
151
+ * @param req Request object.
152
+ * @param adapter Response adapter.
153
+ * @param ts Timeouts used by the operation.
154
+ * @param token Asio completion token.
155
+ *
156
+ * The completion token must have the following signature
157
+ *
158
+ * @code
159
+ * void f(boost::system::error_code, std::size_t);
160
+ * @endcode
161
+ *
162
+ * Where the second parameter is the size of the response in bytes.
163
+ */
164
+ template <
165
+ class Adapter = detail::response_traits<void >::adapter_type,
166
+ class CompletionToken = boost::asio::default_completion_token_t <executor_type>>
167
+ auto async_run (
168
+ endpoint ep,
169
+ resp3::request const & req,
170
+ Adapter adapter,
171
+ timeouts ts,
172
+ CompletionToken token = CompletionToken{})
173
+ {
174
+ return base_type::async_run (ep, req, adapter, ts, std::move (token));
175
+ }
176
+
93
177
private:
94
178
using base_type = connection_base<executor_type, connection<AsyncReadWriteStream>>;
95
179
using this_type = connection<next_layer_type>;
@@ -106,19 +190,18 @@ class connection :
106
190
template <class > friend struct detail ::run_op;
107
191
108
192
template <class CompletionToken >
109
- auto async_connect (CompletionToken&& token)
193
+ auto async_connect (timeouts ts, CompletionToken&& token)
110
194
{
111
195
return boost::asio::async_compose
112
196
< CompletionToken
113
197
, void (boost::system ::error_code)
114
- >(detail::connect_with_timeout_op<this_type>{this }, token, stream_);
198
+ >(detail::connect_with_timeout_op<this_type>{this , ts }, token, stream_);
115
199
}
116
200
117
201
void close () { stream_.close (); }
118
202
auto is_open () const noexcept { return stream_.is_open (); }
119
203
auto & lowest_layer () noexcept { return stream_.lowest_layer (); }
120
204
121
- config cfg_;
122
205
AsyncReadWriteStream stream_;
123
206
};
124
207
0 commit comments