redis-tower 0.1.2

A Tower-based Redis client with strong typing, composable middleware, and resilience
Documentation
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Redis Smart Client Handoff maintenance notifications.
//!
//! Maintenance handling is deliberately opt-in and currently targets one
//! factory-backed [`MultiplexedClient`](crate::MultiplexedClient) connection.
//! The existing auto-pipeline worker remains the sole socket reader. Construct
//! the client with
//! [`MultiplexedClient::from_factory_with_maintenance`](crate::MultiplexedClient::from_factory_with_maintenance)
//! and retain the returned [`MaintenanceListenerHandle`] to keep it enabled.
//!
//! # Example
//!
//! ```no_run
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! use redis_tower::{AutoPipelineConfig, MultiplexedClient};
//! use redis_tower::auto_pipeline::AutoPipelineReconnectConfig;
//! use redis_tower::reconnect::Resp3AddrConnectionFactory;
//!
//! let factory = Resp3AddrConnectionFactory::new("127.0.0.1:6379");
//! let (client, maintenance) = MultiplexedClient::from_factory_with_maintenance(
//!     factory,
//!     AutoPipelineConfig::default(),
//!     AutoPipelineReconnectConfig::default(),
//! )
//! .await?;
//!
//! // Keep the handle alive for as long as maintenance handoff is desired.
//! # let _ = client;
//! maintenance.shutdown().await;
//! # Ok(())
//! # }
//! ```

use std::collections::VecDeque;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;

use redis_tower_core::{Frame, ReceivedPushFrame, RedisConnection, RedisError};
use redis_tower_protocol::helpers::{array, bulk};
use tokio::sync::{mpsc, oneshot};

static NEXT_LISTENER_ID: AtomicU64 = AtomicU64::new(1);

/// The supported Redis maintenance-notification kinds.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum MaintenanceNotificationKind {
    /// Redis is preparing this connection for migration.
    ///
    /// This is observational in the current implementation; it does not
    /// trigger a reconnect.
    Migrating,
    /// Redis asked this connection to move before the supplied TTL expires.
    ///
    /// With endpoint type `none`, redis-tower reconnects through the original
    /// factory halfway through the TTL.
    Moving,
}

/// Owned capability that keeps maintenance-notification handling enabled.
///
/// Dropping the handle disables future handling and cancels a handoff whose
/// half-TTL boundary has not yet been reached. It does not shut down the
/// client, keep the client worker alive, or perform network I/O. Call
/// [`shutdown`](Self::shutdown) to wait until the worker acknowledges the
/// disabled state.
#[must_use = "dropping the handle immediately disables maintenance handling"]
pub struct MaintenanceListenerHandle {
    control: mpsc::UnboundedSender<MaintenanceControl>,
    listener_id: u64,
    armed: bool,
}

impl MaintenanceListenerHandle {
    pub(crate) fn new(
        control: mpsc::UnboundedSender<MaintenanceControl>,
        listener_id: u64,
    ) -> Self {
        Self {
            control,
            listener_id,
            armed: true,
        }
    }

    /// Disable maintenance handling and wait for worker acknowledgement.
    ///
    /// If a handoff already crossed its safe batch boundary, shutdown waits
    /// for that replacement attempt to reach a connected or terminal state.
    /// This method does not shut down the multiplexed client itself.
    pub async fn shutdown(mut self) {
        self.armed = false;
        let (ack_tx, ack_rx) = oneshot::channel();
        if self
            .control
            .send(MaintenanceControl::Disable {
                listener_id: self.listener_id,
                ack: Some(ack_tx),
            })
            .is_ok()
        {
            let _ = ack_rx.await;
        }
    }
}

impl Drop for MaintenanceListenerHandle {
    fn drop(&mut self) {
        if self.armed {
            let _ = self.control.send(MaintenanceControl::Disable {
                listener_id: self.listener_id,
                ack: None,
            });
        }
    }
}

pub(crate) enum MaintenanceControl {
    Disable {
        listener_id: u64,
        ack: Option<oneshot::Sender<()>>,
    },
}

pub(crate) fn next_listener_id() -> u64 {
    NEXT_LISTENER_ID.fetch_add(1, Ordering::Relaxed)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ParsedMaintenanceNotification {
    Migrating { sequence: u64, ttl: Duration },
    Moving { sequence: u64, ttl: Duration },
}

impl ParsedMaintenanceNotification {
    pub(crate) fn kind(self) -> MaintenanceNotificationKind {
        match self {
            Self::Migrating { .. } => MaintenanceNotificationKind::Migrating,
            Self::Moving { .. } => MaintenanceNotificationKind::Moving,
        }
    }

    pub(crate) fn sequence(self) -> u64 {
        match self {
            Self::Migrating { sequence, .. } | Self::Moving { sequence, .. } => sequence,
        }
    }

    pub(crate) fn ttl(self) -> Duration {
        match self {
            Self::Migrating { ttl, .. } | Self::Moving { ttl, .. } => ttl,
        }
    }
}

pub(crate) struct ReceivedMaintenanceNotification {
    pub(crate) received_at: std::time::Instant,
    pub(crate) notification: ParsedMaintenanceNotification,
}

pub(crate) struct PendingHandoff {
    pub(crate) sequence: u64,
    pub(crate) deadline: tokio::time::Instant,
}

pub(crate) struct MaintenanceState {
    pub(crate) listener_id: u64,
    pub(crate) enabled: bool,
    seen: VecDeque<(MaintenanceNotificationKind, u64)>,
    pub(crate) pending: Option<PendingHandoff>,
}

impl MaintenanceState {
    pub(crate) fn new(listener_id: u64) -> Self {
        Self {
            listener_id,
            enabled: true,
            seen: VecDeque::with_capacity(64),
            pending: None,
        }
    }

    pub(crate) fn accept(
        &mut self,
        received: ReceivedMaintenanceNotification,
    ) -> Option<ParsedMaintenanceNotification> {
        if !self.enabled {
            return None;
        }
        let notification = received.notification;
        let key = (notification.kind(), notification.sequence());
        if self.seen.contains(&key) {
            return None;
        }

        let moving_deadline = match notification {
            ParsedMaintenanceNotification::Moving { ttl, .. } => {
                let half_ttl = ttl / 2;
                let deadline = received.received_at.checked_add(half_ttl)?;
                Some(tokio::time::Instant::from_std(deadline))
            }
            ParsedMaintenanceNotification::Migrating { .. } => None,
        };
        if self.seen.len() == 64 {
            self.seen.pop_front();
        }
        self.seen.push_back(key);

        if let ParsedMaintenanceNotification::Moving { sequence, .. } = notification {
            let deadline = moving_deadline.expect("MOVING notifications compute a deadline");
            if self
                .pending
                .as_ref()
                .is_none_or(|pending| deadline < pending.deadline)
            {
                self.pending = Some(PendingHandoff { sequence, deadline });
            }
        }
        Some(notification)
    }
}

pub(crate) fn parse_maintenance_push(
    received: ReceivedPushFrame,
) -> Option<ReceivedMaintenanceNotification> {
    let Frame::Push(items) = received.frame else {
        return None;
    };
    let kind = frame_bytes(items.first()?)?;
    let notification = match kind {
        b"MIGRATING" if items.len() == 3 => ParsedMaintenanceNotification::Migrating {
            sequence: frame_u64(&items[1])?,
            ttl: Duration::from_secs(frame_u64(&items[2])?),
        },
        b"MOVING" if items.len() == 4 && is_null(&items[3]) => {
            ParsedMaintenanceNotification::Moving {
                sequence: frame_u64(&items[1])?,
                ttl: Duration::from_secs(frame_u64(&items[2])?),
            }
        }
        _ => return None,
    };
    Some(ReceivedMaintenanceNotification {
        received_at: received.received_at,
        notification,
    })
}

fn frame_bytes(frame: &Frame) -> Option<&[u8]> {
    match frame {
        Frame::SimpleString(value) | Frame::BulkString(Some(value)) => Some(value),
        _ => None,
    }
}

fn frame_u64(frame: &Frame) -> Option<u64> {
    match frame {
        Frame::Integer(value) => u64::try_from(*value).ok(),
        other => std::str::from_utf8(frame_bytes(other)?).ok()?.parse().ok(),
    }
}

fn is_null(frame: &Frame) -> bool {
    matches!(
        frame,
        Frame::Null | Frame::BulkString(None) | Frame::Array(None)
    )
}

pub(crate) async fn register_connection(
    connection: &mut RedisConnection,
) -> Result<tokio::sync::broadcast::Receiver<ReceivedPushFrame>, RedisError> {
    if !connection.is_resp3() {
        return Err(RedisError::UnexpectedResponse {
            expected: "a RESP3 connection for maintenance notifications",
            actual: "RESP2 connection".to_owned(),
        });
    }

    // Install the feed before registration so a push arriving immediately
    // after the OK response cannot race past the listener.
    let pushes = connection.subscribe_received_pushes();
    let mut responses = connection
        .execute_pipeline(vec![array(vec![
            bulk("CLIENT"),
            bulk("MAINT_NOTIFICATIONS"),
            bulk("ON"),
            bulk("moving-endpoint-type"),
            bulk("none"),
        ])])
        .await?;
    let response = responses.pop().ok_or(RedisError::UnexpectedResponse {
        expected: "CLIENT MAINT_NOTIFICATIONS OK response",
        actual: "empty response pipeline".to_owned(),
    })?;
    match response {
        Frame::SimpleString(value) if value.as_ref() == b"OK" => Ok(pushes),
        other => Err(RedisError::UnexpectedResponse {
            expected: "successful CLIENT MAINT_NOTIFICATIONS registration",
            actual: format!("{other:?}"),
        }),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bytes::Bytes;

    fn simple(value: &'static [u8]) -> Frame {
        Frame::SimpleString(Bytes::from_static(value))
    }

    #[test]
    fn parses_integer_and_string_maintenance_fields() {
        let received_at = std::time::Instant::now();
        let moving = parse_maintenance_push(ReceivedPushFrame {
            received_at,
            frame: Frame::Push(vec![
                simple(b"MOVING"),
                Frame::Integer(17),
                Frame::BulkString(Some(Bytes::from_static(b"9"))),
                Frame::Null,
            ]),
        })
        .expect("valid MOVING push");
        assert_eq!(moving.received_at, received_at);
        assert_eq!(
            moving.notification,
            ParsedMaintenanceNotification::Moving {
                sequence: 17,
                ttl: Duration::from_secs(9),
            }
        );

        let migrating = parse_maintenance_push(ReceivedPushFrame {
            received_at,
            frame: Frame::Push(vec![
                simple(b"MIGRATING"),
                Frame::BulkString(Some(Bytes::from_static(b"18"))),
                Frame::Integer(10),
            ]),
        })
        .expect("valid MIGRATING push");
        assert_eq!(
            migrating.notification,
            ParsedMaintenanceNotification::Migrating {
                sequence: 18,
                ttl: Duration::from_secs(10),
            }
        );
    }

    #[test]
    fn rejects_non_null_endpoint_and_malformed_fields() {
        for frame in [
            Frame::Push(vec![
                simple(b"MOVING"),
                Frame::Integer(1),
                Frame::Integer(2),
                simple(b"other:6379"),
            ]),
            Frame::Push(vec![
                simple(b"MOVING"),
                Frame::Integer(-1),
                Frame::Integer(2),
                Frame::Null,
            ]),
            Frame::Push(vec![simple(b"MIGRATING"), Frame::Integer(1)]),
            Frame::Push(vec![simple(b"OTHER"), Frame::Integer(1), Frame::Integer(2)]),
        ] {
            assert!(
                parse_maintenance_push(ReceivedPushFrame {
                    received_at: std::time::Instant::now(),
                    frame,
                })
                .is_none()
            );
        }
    }

    // `std::time::Instant` has a much wider representable range on Windows,
    // so even `Duration::MAX / 2` does not exercise checked-add overflow there.
    #[cfg(not(windows))]
    #[test]
    fn ignores_moving_deadline_overflow_without_poisoning_dedup_state() {
        let mut state = MaintenanceState::new(1);
        let received = ReceivedMaintenanceNotification {
            received_at: std::time::Instant::now(),
            notification: ParsedMaintenanceNotification::Moving {
                sequence: 99,
                ttl: Duration::MAX,
            },
        };
        assert_eq!(state.accept(received), None);
        assert!(state.pending.is_none());

        let retry = ReceivedMaintenanceNotification {
            received_at: std::time::Instant::now(),
            notification: ParsedMaintenanceNotification::Moving {
                sequence: 99,
                ttl: Duration::ZERO,
            },
        };
        assert!(matches!(
            state.accept(retry),
            Some(ParsedMaintenanceNotification::Moving { sequence: 99, .. })
        ));
    }
}