redis-oxide 0.2.3

High-performance async Redis client for Rust with automatic cluster support, multiplexing, and advanced features
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
//! Pub/Sub support for Redis
//!
//! This module provides functionality for Redis publish/subscribe messaging.
//! Redis Pub/Sub allows you to send messages between different parts of your
//! application or between different applications.
//!
//! # Examples
//!
//! ## Publisher
//!
//! ```no_run
//! use redis_oxide::{Client, ConnectionConfig};
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let config = ConnectionConfig::new("redis://localhost:6379");
//! let client = Client::connect(config).await?;
//!
//! // Publish a message to a channel
//! let subscribers = client.publish("news", "Breaking news!").await?;
//! println!("Message sent to {} subscribers", subscribers);
//! # Ok(())
//! # }
//! ```
//!
//! ## Subscriber
//!
//! ```no_run
//! use redis_oxide::{Client, ConnectionConfig};
//! use futures::StreamExt;
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let config = ConnectionConfig::new("redis://localhost:6379");
//! let client = Client::connect(config).await?;
//!
//! // Subscribe to channels
//! let mut subscriber = client.subscriber().await?;
//! subscriber.subscribe(vec!["news".to_string(), "updates".to_string()]).await?;
//!
//! // Listen for messages
//! while let Some(message) = subscriber.next_message().await? {
//!     println!("Received: {} on channel {}", message.payload, message.channel);
//! }
//! # Ok(())
//! # }
//! ```

use crate::core::{
    error::{RedisError, RedisResult},
    value::RespValue,
};
use futures_util::Stream;
use std::collections::HashMap;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use tokio::sync::{mpsc, Mutex};
use tokio::time::{timeout, Duration};

/// A message received from a Redis channel
#[derive(Debug, Clone)]
pub struct PubSubMessage {
    /// The channel the message was received on
    pub channel: String,
    /// The message payload
    pub payload: String,
    /// The pattern that matched (for pattern subscriptions)
    pub pattern: Option<String>,
}

/// Redis Pub/Sub subscriber
pub struct Subscriber {
    connection: Arc<Mutex<dyn PubSubConnection + Send + Sync>>,
    message_rx: mpsc::UnboundedReceiver<PubSubMessage>,
    subscribed_channels: HashMap<String, bool>,
    subscribed_patterns: HashMap<String, bool>,
}

/// Trait for Pub/Sub connections
#[async_trait::async_trait]
pub trait PubSubConnection {
    /// Subscribe to channels
    async fn subscribe(&mut self, channels: Vec<String>) -> RedisResult<()>;

    /// Unsubscribe from channels
    async fn unsubscribe(&mut self, channels: Vec<String>) -> RedisResult<()>;

    /// Subscribe to patterns
    async fn psubscribe(&mut self, patterns: Vec<String>) -> RedisResult<()>;

    /// Unsubscribe from patterns
    async fn punsubscribe(&mut self, patterns: Vec<String>) -> RedisResult<()>;

    /// Start listening for messages
    async fn listen(&mut self, message_tx: mpsc::UnboundedSender<PubSubMessage>)
        -> RedisResult<()>;

    /// Publish a message to a channel
    async fn publish(&mut self, channel: String, message: String) -> RedisResult<i64>;
}

impl Subscriber {
    /// Create a new subscriber
    pub fn new(connection: Arc<Mutex<dyn PubSubConnection + Send + Sync>>) -> Self {
        let (message_tx, message_rx) = mpsc::unbounded_channel();

        // Start listening for messages in the background
        let conn_clone = connection.clone();
        tokio::spawn(async move {
            let mut conn = conn_clone.lock().await;
            if let Err(e) = conn.listen(message_tx).await {
                eprintln!("Pub/Sub listener error: {}", e);
            }
        });

        Self {
            connection,
            message_rx,
            subscribed_channels: HashMap::new(),
            subscribed_patterns: HashMap::new(),
        }
    }

    /// Subscribe to one or more channels
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use redis_oxide::{Client, ConnectionConfig};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let config = ConnectionConfig::new("redis://localhost:6379");
    /// # let client = Client::connect(config).await?;
    /// let mut subscriber = client.subscriber().await?;
    ///
    /// // Subscribe to multiple channels
    /// subscriber.subscribe(vec![
    ///     "news".to_string(),
    ///     "updates".to_string(),
    ///     "alerts".to_string()
    /// ]).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn subscribe(&mut self, channels: Vec<String>) -> RedisResult<()> {
        let mut connection = self.connection.lock().await;
        connection.subscribe(channels.clone()).await?;

        for channel in channels {
            self.subscribed_channels.insert(channel, true);
        }

        Ok(())
    }

    /// Unsubscribe from one or more channels
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use redis_oxide::{Client, ConnectionConfig};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let config = ConnectionConfig::new("redis://localhost:6379");
    /// # let client = Client::connect(config).await?;
    /// let mut subscriber = client.subscriber().await?;
    /// subscriber.subscribe(vec!["news".to_string()]).await?;
    ///
    /// // Later, unsubscribe
    /// subscriber.unsubscribe(vec!["news".to_string()]).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn unsubscribe(&mut self, channels: Vec<String>) -> RedisResult<()> {
        let mut connection = self.connection.lock().await;
        connection.unsubscribe(channels.clone()).await?;

        for channel in channels {
            self.subscribed_channels.remove(&channel);
        }

        Ok(())
    }

    /// Subscribe to one or more patterns
    ///
    /// Patterns support glob-style matching:
    /// - `*` matches any sequence of characters
    /// - `?` matches any single character
    /// - `[abc]` matches any character in the set
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use redis_oxide::{Client, ConnectionConfig};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let config = ConnectionConfig::new("redis://localhost:6379");
    /// # let client = Client::connect(config).await?;
    /// let mut subscriber = client.subscriber().await?;
    ///
    /// // Subscribe to all channels starting with "news"
    /// subscriber.psubscribe(vec!["news*".to_string()]).await?;
    ///
    /// // Subscribe to all channels ending with "log"
    /// subscriber.psubscribe(vec!["*log".to_string()]).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn psubscribe(&mut self, patterns: Vec<String>) -> RedisResult<()> {
        let mut connection = self.connection.lock().await;
        connection.psubscribe(patterns.clone()).await?;

        for pattern in patterns {
            self.subscribed_patterns.insert(pattern, true);
        }

        Ok(())
    }

    /// Unsubscribe from one or more patterns
    pub async fn punsubscribe(&mut self, patterns: Vec<String>) -> RedisResult<()> {
        let mut connection = self.connection.lock().await;
        connection.punsubscribe(patterns.clone()).await?;

        for pattern in patterns {
            self.subscribed_patterns.remove(&pattern);
        }

        Ok(())
    }

    /// Get the next message from subscribed channels
    ///
    /// This method will block until a message is received or an error occurs.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use redis_oxide::{Client, ConnectionConfig};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let config = ConnectionConfig::new("redis://localhost:6379");
    /// # let client = Client::connect(config).await?;
    /// let mut subscriber = client.subscriber().await?;
    /// subscriber.subscribe(vec!["news".to_string()]).await?;
    ///
    /// // Wait for the next message
    /// if let Some(message) = subscriber.next_message().await? {
    ///     println!("Received: {} on {}", message.payload, message.channel);
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn next_message(&mut self) -> RedisResult<Option<PubSubMessage>> {
        match self.message_rx.recv().await {
            Some(message) => Ok(Some(message)),
            None => Ok(None), // Channel closed
        }
    }

    /// Get the next message with a timeout
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use redis_oxide::{Client, ConnectionConfig};
    /// # use std::time::Duration;
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let config = ConnectionConfig::new("redis://localhost:6379");
    /// # let client = Client::connect(config).await?;
    /// let mut subscriber = client.subscriber().await?;
    /// subscriber.subscribe(vec!["news".to_string()]).await?;
    ///
    /// // Wait for a message with 5 second timeout
    /// match subscriber.next_message_timeout(Duration::from_secs(5)).await? {
    ///     Some(message) => println!("Received: {}", message.payload),
    ///     None => println!("No message received within timeout"),
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn next_message_timeout(
        &mut self,
        duration: Duration,
    ) -> RedisResult<Option<PubSubMessage>> {
        match timeout(duration, self.message_rx.recv()).await {
            Ok(Some(message)) => Ok(Some(message)),
            Ok(None) => Ok(None), // Channel closed
            Err(_) => Ok(None),   // Timeout
        }
    }

    /// Get a list of currently subscribed channels
    #[must_use]
    pub fn subscribed_channels(&self) -> Vec<String> {
        self.subscribed_channels.keys().cloned().collect()
    }

    /// Get a list of currently subscribed patterns
    #[must_use]
    pub fn subscribed_patterns(&self) -> Vec<String> {
        self.subscribed_patterns.keys().cloned().collect()
    }

    /// Check if subscribed to a specific channel
    #[must_use]
    pub fn is_subscribed_to_channel(&self, channel: &str) -> bool {
        self.subscribed_channels.contains_key(channel)
    }

    /// Check if subscribed to a specific pattern
    #[must_use]
    pub fn is_subscribed_to_pattern(&self, pattern: &str) -> bool {
        self.subscribed_patterns.contains_key(pattern)
    }
}

/// Stream implementation for Subscriber
impl Stream for Subscriber {
    type Item = RedisResult<PubSubMessage>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        match self.message_rx.poll_recv(cx) {
            Poll::Ready(Some(message)) => Poll::Ready(Some(Ok(message))),
            Poll::Ready(None) => Poll::Ready(None), // Channel closed
            Poll::Pending => Poll::Pending,
        }
    }
}

/// Publisher for sending messages to Redis channels
pub struct Publisher {
    connection: Arc<Mutex<dyn PubSubConnection + Send + Sync>>,
}

impl Publisher {
    /// Create a new publisher
    pub fn new(connection: Arc<Mutex<dyn PubSubConnection + Send + Sync>>) -> Self {
        Self { connection }
    }

    /// Publish a message to a channel
    ///
    /// Returns the number of subscribers that received the message.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use redis_oxide::{Client, ConnectionConfig};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let config = ConnectionConfig::new("redis://localhost:6379");
    /// # let client = Client::connect(config).await?;
    /// let publisher = client.publisher().await?;
    ///
    /// let subscribers = publisher.publish("news", "Breaking news!").await?;
    /// println!("Message delivered to {} subscribers", subscribers);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn publish(
        &self,
        channel: impl Into<String>,
        message: impl Into<String>,
    ) -> RedisResult<i64> {
        let mut connection = self.connection.lock().await;
        connection.publish(channel.into(), message.into()).await
    }

    /// Publish multiple messages to different channels
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use redis_oxide::{Client, ConnectionConfig};
    /// # use std::collections::HashMap;
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let config = ConnectionConfig::new("redis://localhost:6379");
    /// # let client = Client::connect(config).await?;
    /// let publisher = client.publisher().await?;
    ///
    /// let mut messages = HashMap::new();
    /// messages.insert("news".to_string(), "Breaking news!".to_string());
    /// messages.insert("updates".to_string(), "System update available".to_string());
    ///
    /// let results = publisher.publish_multiple(messages).await?;
    /// for (channel, count) in results {
    ///     println!("Channel {}: {} subscribers", channel, count);
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn publish_multiple(
        &self,
        messages: HashMap<String, String>,
    ) -> RedisResult<HashMap<String, i64>> {
        let mut results = HashMap::new();

        for (channel, message) in messages {
            let count = self.publish(&channel, message).await?;
            results.insert(channel, count);
        }

        Ok(results)
    }
}

/// Pub/Sub message types for internal parsing
#[derive(Debug)]
enum PubSubMessageType {
    Subscribe,
    Unsubscribe,
    Message,
    PSubscribe,
    PUnsubscribe,
    PMessage,
}

impl PubSubMessageType {
    fn from_str(s: &str) -> Option<Self> {
        match s {
            "subscribe" => Some(Self::Subscribe),
            "unsubscribe" => Some(Self::Unsubscribe),
            "message" => Some(Self::Message),
            "psubscribe" => Some(Self::PSubscribe),
            "punsubscribe" => Some(Self::PUnsubscribe),
            "pmessage" => Some(Self::PMessage),
            _ => None,
        }
    }
}

/// Parse a Pub/Sub message from Redis response
pub fn parse_pubsub_message(response: RespValue) -> RedisResult<Option<PubSubMessage>> {
    match response {
        RespValue::Array(items) if items.len() >= 3 => {
            let message_type = items[0].as_string()?;
            let msg_type = PubSubMessageType::from_str(&message_type);

            match msg_type {
                Some(PubSubMessageType::Message) => {
                    let channel = items[1].as_string()?;
                    let payload = items[2].as_string()?;

                    Ok(Some(PubSubMessage {
                        channel,
                        payload,
                        pattern: None,
                    }))
                }
                Some(PubSubMessageType::PMessage) if items.len() >= 4 => {
                    let pattern = items[1].as_string()?;
                    let channel = items[2].as_string()?;
                    let payload = items[3].as_string()?;

                    Ok(Some(PubSubMessage {
                        channel,
                        payload,
                        pattern: Some(pattern),
                    }))
                }
                Some(
                    PubSubMessageType::Subscribe
                    | PubSubMessageType::Unsubscribe
                    | PubSubMessageType::PSubscribe
                    | PubSubMessageType::PUnsubscribe,
                ) => {
                    // These are subscription confirmations, not actual messages
                    Ok(None)
                }
                _ => Err(RedisError::Protocol(format!(
                    "Unknown pub/sub message type: {}",
                    message_type
                ))),
            }
        }
        _ => Err(RedisError::Protocol(format!(
            "Invalid pub/sub message format: {:?}",
            response
        ))),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use tokio::sync::Mutex;

    struct MockPubSubConnection {
        published_messages: Vec<(String, String)>,
        subscribed_channels: Vec<String>,
        subscribed_patterns: Vec<String>,
    }

    impl MockPubSubConnection {
        fn new() -> Self {
            Self {
                published_messages: Vec::new(),
                subscribed_channels: Vec::new(),
                subscribed_patterns: Vec::new(),
            }
        }
    }

    #[async_trait::async_trait]
    impl PubSubConnection for MockPubSubConnection {
        async fn subscribe(&mut self, channels: Vec<String>) -> RedisResult<()> {
            self.subscribed_channels.extend(channels);
            Ok(())
        }

        async fn unsubscribe(&mut self, channels: Vec<String>) -> RedisResult<()> {
            for channel in channels {
                self.subscribed_channels.retain(|c| c != &channel);
            }
            Ok(())
        }

        async fn psubscribe(&mut self, patterns: Vec<String>) -> RedisResult<()> {
            self.subscribed_patterns.extend(patterns);
            Ok(())
        }

        async fn punsubscribe(&mut self, patterns: Vec<String>) -> RedisResult<()> {
            for pattern in patterns {
                self.subscribed_patterns.retain(|p| p != &pattern);
            }
            Ok(())
        }

        async fn listen(
            &mut self,
            _message_tx: mpsc::UnboundedSender<PubSubMessage>,
        ) -> RedisResult<()> {
            // Mock implementation - would normally listen for messages
            Ok(())
        }

        async fn publish(&mut self, channel: String, message: String) -> RedisResult<i64> {
            self.published_messages.push((channel, message));
            Ok(1) // Mock: 1 subscriber
        }
    }

    #[tokio::test]
    async fn test_subscriber_creation() {
        let connection = MockPubSubConnection::new();
        let subscriber = Subscriber::new(Arc::new(Mutex::new(connection)));

        assert!(subscriber.subscribed_channels().is_empty());
        assert!(subscriber.subscribed_patterns().is_empty());
    }

    #[tokio::test]
    async fn test_subscriber_subscribe() {
        let connection = MockPubSubConnection::new();
        let mut subscriber = Subscriber::new(Arc::new(Mutex::new(connection)));

        subscriber
            .subscribe(vec!["news".to_string(), "updates".to_string()])
            .await
            .unwrap();

        assert_eq!(subscriber.subscribed_channels().len(), 2);
        assert!(subscriber.is_subscribed_to_channel("news"));
        assert!(subscriber.is_subscribed_to_channel("updates"));
    }

    #[tokio::test]
    async fn test_subscriber_unsubscribe() {
        let connection = MockPubSubConnection::new();
        let mut subscriber = Subscriber::new(Arc::new(Mutex::new(connection)));

        subscriber
            .subscribe(vec!["news".to_string(), "updates".to_string()])
            .await
            .unwrap();
        subscriber
            .unsubscribe(vec!["news".to_string()])
            .await
            .unwrap();

        assert_eq!(subscriber.subscribed_channels().len(), 1);
        assert!(!subscriber.is_subscribed_to_channel("news"));
        assert!(subscriber.is_subscribed_to_channel("updates"));
    }

    #[tokio::test]
    async fn test_publisher_publish() {
        let connection = MockPubSubConnection::new();
        let publisher = Publisher::new(Arc::new(Mutex::new(connection)));

        let count = publisher.publish("news", "Breaking news!").await.unwrap();
        assert_eq!(count, 1);
    }

    #[test]
    fn test_parse_pubsub_message() {
        // Test regular message
        let response = RespValue::Array(vec![
            RespValue::from("message"),
            RespValue::from("news"),
            RespValue::from("Breaking news!"),
        ]);

        let message = parse_pubsub_message(response).unwrap().unwrap();
        assert_eq!(message.channel, "news");
        assert_eq!(message.payload, "Breaking news!");
        assert!(message.pattern.is_none());
    }

    #[test]
    fn test_parse_pubsub_pattern_message() {
        // Test pattern message
        let response = RespValue::Array(vec![
            RespValue::from("pmessage"),
            RespValue::from("news*"),
            RespValue::from("news-tech"),
            RespValue::from("Tech news!"),
        ]);

        let message = parse_pubsub_message(response).unwrap().unwrap();
        assert_eq!(message.channel, "news-tech");
        assert_eq!(message.payload, "Tech news!");
        assert_eq!(message.pattern, Some("news*".to_string()));
    }

    #[test]
    fn test_parse_pubsub_subscribe_confirmation() {
        // Test subscription confirmation (should return None)
        let response = RespValue::Array(vec![
            RespValue::from("subscribe"),
            RespValue::from("news"),
            RespValue::Integer(1),
        ]);

        let message = parse_pubsub_message(response).unwrap();
        assert!(message.is_none());
    }
}