-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathreconnecting_connection.rs
366 lines (335 loc) · 12.7 KB
/
reconnecting_connection.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
use super::{NodeAddress, TlsMode};
use crate::retry_strategies::RetryStrategy;
use async_trait::async_trait;
use futures_intrusive::sync::ManualResetEvent;
use logger_core::{log_debug, log_error, log_trace, log_warn};
use redis::aio::{DisconnectNotifier, MultiplexedConnection};
use redis::{GlideConnectionOptions, PushInfo, RedisConnectionInfo, RedisError, RedisResult};
use std::fmt;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::sync::Mutex;
use std::time::Duration;
use telemetrylib::Telemetry;
use tokio::sync::{mpsc, Notify};
use tokio::task;
use tokio::time::timeout;
use tokio_retry2::{Retry, RetryError};
use super::{run_with_timeout, DEFAULT_CONNECTION_TIMEOUT};
/// The reason behind the call to `reconnect()`
#[derive(PartialEq, Eq, Debug, Clone)]
pub enum ReconnectReason {
/// A connection was dropped (for any reason)
ConnectionDropped,
/// Connection creation error
CreateError,
}
/// The object that is used in order to recreate a connection after a disconnect.
struct ConnectionBackend {
/// This signal is reset when a connection disconnects, and set when a new `ConnectionState` has been set with a `Connected` state.
connection_available_signal: ManualResetEvent,
/// Information needed in order to create a new connection.
connection_info: redis::Client,
/// Once this flag is set, the internal connection needs no longer try to reconnect to the server, because all the outer clients were dropped.
client_dropped_flagged: AtomicBool,
}
/// State of the current connection. Allows the user to use a connection only when a reconnect isn't in progress or has failed.
enum ConnectionState {
/// A connection has been established.
Connected(MultiplexedConnection),
/// There's a reconnection effort on the way, no need to try reconnecting again.
Reconnecting,
/// Initial state of connection when no connection was created during initialization.
InitializedDisconnected,
}
struct InnerReconnectingConnection {
state: Mutex<ConnectionState>,
backend: ConnectionBackend,
}
#[derive(Clone)]
pub(super) struct ReconnectingConnection {
inner: Arc<InnerReconnectingConnection>,
connection_options: GlideConnectionOptions,
}
impl fmt::Debug for ReconnectingConnection {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.node_address())
}
}
async fn get_multiplexed_connection(
client: &redis::Client,
connection_options: &GlideConnectionOptions,
) -> RedisResult<MultiplexedConnection> {
run_with_timeout(
Some(
connection_options
.connection_timeout
.unwrap_or(DEFAULT_CONNECTION_TIMEOUT),
),
client.get_multiplexed_async_connection(connection_options.clone()),
)
.await
}
#[derive(Clone)]
struct TokioDisconnectNotifier {
disconnect_notifier: Arc<Notify>,
}
#[async_trait]
impl DisconnectNotifier for TokioDisconnectNotifier {
fn notify_disconnect(&mut self) {
self.disconnect_notifier.notify_one();
}
async fn wait_for_disconnect_with_timeout(&self, max_wait: &Duration) {
let _ = timeout(*max_wait, async {
self.disconnect_notifier.notified().await;
})
.await;
}
fn clone_box(&self) -> Box<dyn DisconnectNotifier> {
Box::new(self.clone())
}
}
impl TokioDisconnectNotifier {
fn new() -> TokioDisconnectNotifier {
TokioDisconnectNotifier {
disconnect_notifier: Arc::new(Notify::new()),
}
}
}
async fn create_connection(
connection_backend: ConnectionBackend,
retry_strategy: RetryStrategy,
push_sender: Option<mpsc::UnboundedSender<PushInfo>>,
discover_az: bool,
connection_timeout: Duration,
) -> Result<ReconnectingConnection, (ReconnectingConnection, RedisError)> {
let client = &connection_backend.connection_info;
let connection_options = GlideConnectionOptions {
push_sender,
disconnect_notifier: Some::<Box<dyn DisconnectNotifier>>(Box::new(
TokioDisconnectNotifier::new(),
)),
discover_az,
connection_timeout: Some(connection_timeout),
};
let action = || async {
get_multiplexed_connection(client, &connection_options)
.await
.map_err(RetryError::transient)
};
match Retry::spawn(retry_strategy.get_iterator(), action).await {
Ok(connection) => {
log_debug(
"connection creation",
format!(
"Connection to {} created",
connection_backend
.connection_info
.get_connection_info()
.addr
),
);
Telemetry::incr_total_connections(1);
Ok(ReconnectingConnection {
inner: Arc::new(InnerReconnectingConnection {
state: Mutex::new(ConnectionState::Connected(connection)),
backend: connection_backend,
}),
connection_options,
})
}
Err(err) => {
log_warn(
"connection creation",
format!(
"Failed connecting to {}, due to {err}",
connection_backend
.connection_info
.get_connection_info()
.addr
),
);
let connection = ReconnectingConnection {
inner: Arc::new(InnerReconnectingConnection {
state: Mutex::new(ConnectionState::InitializedDisconnected),
backend: connection_backend,
}),
connection_options,
};
connection.reconnect(ReconnectReason::CreateError);
Err((connection, err))
}
}
}
fn get_client(
address: &NodeAddress,
tls_mode: TlsMode,
redis_connection_info: redis::RedisConnectionInfo,
) -> redis::Client {
redis::Client::open(super::get_connection_info(
address,
tls_mode,
redis_connection_info,
))
.unwrap() // can unwrap, because [open] fails only on trying to convert input to ConnectionInfo, and we pass ConnectionInfo.
}
/// This iterator isn't exposed to users, and can't be configured.
fn internal_retry_iterator() -> impl Iterator<Item = Duration> {
const MAX_DURATION: Duration = Duration::from_secs(5);
crate::retry_strategies::get_exponential_backoff(
crate::retry_strategies::EXPONENT_BASE,
crate::retry_strategies::FACTOR,
crate::retry_strategies::NUMBER_OF_RETRIES,
)
.get_iterator()
.chain(std::iter::repeat(MAX_DURATION))
}
impl ReconnectingConnection {
pub(super) async fn new(
address: &NodeAddress,
connection_retry_strategy: RetryStrategy,
redis_connection_info: RedisConnectionInfo,
tls_mode: TlsMode,
push_sender: Option<mpsc::UnboundedSender<PushInfo>>,
discover_az: bool,
connection_timeout: Duration,
) -> Result<ReconnectingConnection, (ReconnectingConnection, RedisError)> {
log_debug(
"connection creation",
format!("Attempting connection to {address}"),
);
let connection_info = get_client(address, tls_mode, redis_connection_info);
let backend = ConnectionBackend {
connection_info,
connection_available_signal: ManualResetEvent::new(true),
client_dropped_flagged: AtomicBool::new(false),
};
create_connection(
backend,
connection_retry_strategy,
push_sender,
discover_az,
connection_timeout,
)
.await
}
pub(crate) fn node_address(&self) -> String {
self.inner
.backend
.connection_info
.get_connection_info()
.addr
.to_string()
}
pub(super) fn is_dropped(&self) -> bool {
self.inner
.backend
.client_dropped_flagged
.load(Ordering::Relaxed)
}
pub(super) fn mark_as_dropped(&self) {
// Update the telemetry for each connection that is dropped. A dropped connection
// will not be re-connected, so update the telemetry here
Telemetry::decr_total_connections(1);
self.inner
.backend
.client_dropped_flagged
.store(true, Ordering::Relaxed)
}
pub(super) async fn try_get_connection(&self) -> Option<MultiplexedConnection> {
let guard = self.inner.state.lock().unwrap();
if let ConnectionState::Connected(connection) = &*guard {
Some(connection.clone())
} else {
None
}
}
pub(super) async fn get_connection(&self) -> Result<MultiplexedConnection, RedisError> {
loop {
self.inner.backend.connection_available_signal.wait().await;
if let Some(connection) = self.try_get_connection().await {
return Ok(connection);
}
}
}
/// Attempt to re-connect the connection.
///
/// This function spawns a task to perform the reconnection in the background
pub(super) fn reconnect(&self, reason: ReconnectReason) {
{
let mut guard = self.inner.state.lock().unwrap();
if matches!(*guard, ConnectionState::Reconnecting) {
log_trace("reconnect", "already started");
// exit early - if reconnection already started or failed, there's nothing else to do.
return;
}
self.inner.backend.connection_available_signal.reset();
*guard = ConnectionState::Reconnecting;
};
log_debug("reconnect", "starting");
let connection_clone = self.clone();
if reason.eq(&ReconnectReason::ConnectionDropped) {
// Attempting to reconnect a connection that was dropped (for any reason) - update the telemetry by reducing
// the number of opened connections by 1, it will be incremented by 1 after a successful re-connect
Telemetry::decr_total_connections(1);
}
// The reconnect task is spawned instead of awaited here, so that the reconnect attempt will continue in the
// background, regardless of whether the calling task is dropped or not.
task::spawn(async move {
let client = &connection_clone.inner.backend.connection_info;
for sleep_duration in internal_retry_iterator() {
if connection_clone.is_dropped() {
log_debug(
"ReconnectingConnection",
"reconnect stopped after client was dropped",
);
// Client was dropped, reconnection attempts can stop
return;
}
match get_multiplexed_connection(client, &connection_clone.connection_options).await
{
Ok(mut connection) => {
if connection
.send_packed_command(&redis::cmd("PING"))
.await
.is_err()
{
tokio::time::sleep(sleep_duration).await;
continue;
}
{
let mut guard = connection_clone.inner.state.lock().unwrap();
log_debug("reconnect", "completed successfully");
connection_clone
.inner
.backend
.connection_available_signal
.set();
*guard = ConnectionState::Connected(connection);
}
Telemetry::incr_total_connections(1);
return;
}
Err(_) => tokio::time::sleep(sleep_duration).await,
}
}
});
}
pub fn is_connected(&self) -> bool {
!matches!(
*self.inner.state.lock().unwrap(),
ConnectionState::Reconnecting
)
}
pub async fn wait_for_disconnect_with_timeout(&self, max_wait: &Duration) {
// disconnect_notifier should always exists
if let Some(disconnect_notifier) = &self.connection_options.disconnect_notifier {
disconnect_notifier
.wait_for_disconnect_with_timeout(max_wait)
.await;
} else {
log_error("disconnect notifier", "BUG! Disconnect notifier is not set");
}
}
}