rust-pubsub 0.1.0

A thread-safe, in-memory publish-subscribe library for Rust with flexible subscription modes.
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
//! # Rust PubSub
//!
//! A thread-safe, in-memory publish-subscribe library for Rust, designed for efficient and flexible
//! inter-thread communication. It supports both manual message receiving and callback-based subscriptions,
//! with configurable queue depth and overwrite behavior.
//!
//! ## Features
//!
//! - **Thread-Safe**: Uses `crossbeam-channel` for safe message passing between threads.
//! - **Flexible Subscriptions**: Supports manual message receiving and callback-based subscriptions.
//! - **Configurable Channels**: Allows customization of queue depth and overwrite behavior per subscription.
//! - **Type-Safe Messaging**: Supports any `Send + Sync + Clone + 'static` type for messages.
//! - **Timeout Support**: Provides blocking, non-blocking, and timeout-based message receiving and publishing.
//!
//! ## 功能(中文)
//!
//! - **线程安全**:使用 `crossbeam-channel` 实现线程间安全消息传递。
//! - **灵活订阅**:支持手动消息接收和基于回调的订阅模式。
//! - **可配置通道**:允许为每个订阅配置队列深度和覆盖行为。
//! - **类型安全消息**:支持任何满足 `Send + Sync + Clone + 'static` 的消息类型。
//! - **超时支持**:提供阻塞、非阻塞和带超时的消息接收与发布。
//!
//! ## Usage Examples
//!
//! ### 1. Manual Subscription with Non-Blocking Receive
//!
//! ```rust
//! use rust_pubsub::{PubSub, TopicConfig};
//!
//! let pubsub = PubSub::instance();
//! let topic = "test_topic";
//! let config = TopicConfig::new(10, false); // Queue depth 10, no overwrite
//!
//! // Create publisher
//! let topic_id = pubsub.create_publisher(topic);
//!
//! // Subscribe manually
//! let receiver = pubsub.subscribe_manual::<String>(topic, config);
//!
//! // Publish a message
//! pubsub.publish(topic_id, "Hello, World!".to_string());
//!
//! // Try to receive the message
//! if let Some(msg) = receiver.try_recv() {
//!     println!("Received: {}", msg);
//! }
//! ```
//!
//! ### 2. Callback-Based Subscription
//!
//! ```rust
//! use rust_pubsub::{PubSub, TopicConfig};
//!
//! let pubsub = PubSub::instance();
//! let topic = "callback_topic";
//! let config = TopicConfig::new(5, true); // Queue depth 5, overwrite enabled
//!
//! // Create publisher
//! let topic_id = pubsub.create_publisher(topic);
//!
//! // Subscribe with a callback
//! let subscriber_id = pubsub.subscribe::<String, _>(topic, config, |msg: &String| {
//!     println!("Callback received: {}", msg);
//! });
//!
//! // Publish a message
//! pubsub.publish(topic_id, "Callback message".to_string());
//!
//! // Wait briefly to ensure callback executes
//! std::thread::sleep(std::time::Duration::from_millis(100));
//!
//! // Unsubscribe
//! pubsub.unsubscribe(&subscriber_id);
//! ```
//!
//! ### 3. Publishing with Timeout
//!
//! ```rust
//! use rust_pubsub::{PubSub, TopicConfig};
//!
//! let pubsub = PubSub::instance();
//! let topic = "timeout_topic";
//! let config = TopicConfig::new(1, false); // Queue depth 1, no overwrite
//!
//! // Create publisher
//! let topic_id = pubsub.create_publisher(topic);
//!
//! // Subscribe manually
//! let receiver = pubsub.subscribe_manual::<i32>(topic, config);
//!
//! // Publish with timeout (100ms)
//! pubsub.publish_with_timeout(topic_id, 42, Some(100));
//!
//! // Receive with timeout (100ms)
//! if let Some(msg) = receiver.recv_timeout(Some(100)) {
//!     println!("Received: {}", msg);
//! }
//! ```
//!
//! ### 4. Overwrite Mode with Full Queue
//!
//! ```rust
//! use rust_pubsub::{PubSub, TopicConfig};
//!
//! let pubsub = PubSub::instance();
//! let topic = "overwrite_topic";
//! let config = TopicConfig::new(2, true); // Queue depth 2, overwrite enabled
//!
//! // Create publisher
//! let topic_id = pubsub.create_publisher(topic);
//!
//! // Subscribe manually
//! let receiver = pubsub.subscribe_manual::<String>( کمپل, config);
//!
//! // Publish multiple messages to fill queue
//! pubsub.publish(topic_id, "Message 1".to_string());
//! pubsub.publish(topic_id, "Message 2".to_string());
//! pubsub.publish(topic_id, "Message 3".to_string()); // Overwrites oldest
//!
//! // Receive messages
//! while let Some(msg) = receiver.try_recv() {
//!     println!("Received: {}", msg);
//! }
//! ```
//!
//! ### 5. Multiple Subscribers
//!
//! ```rust
//! use rust_pubsub::{PubSub, TopicConfig};
//!
//! let pubsub = PubSub::instance();
//! let topic = "multi_subscriber_topic";
//! let config = TopicConfig::new(10, false);
//!
//! // Create publisher
//! let topic_id = pubsub.create_publisher(topic);
//!
//! // Subscribe multiple times
//! let receiver1 = pubsub.subscribe_manual::<String>(topic, config.clone());
//! let receiver2 = pubsub.subscribe_manual::<String>(topic, config.clone());
//!
//! // Publish a message
//! pubsub.publish(topic_id, "Broadcast message".to_string());
//!
//! // Receive from both subscribers
//! println!("Receiver 1: {:?}", receiver1.try_recv());
//! println!("Receiver 2: {:?}", receiver2.try_recv());
//! ```
//!
//! ## Installation
//!
//! Add the following to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! rust-pubsub = "0.1.0"
//! ```
//!
//! ## 安装(中文)
//!
//! 在您的 `Cargo.toml` 中添加以下内容:
//!
//! ```toml
//! [dependencies]
//! rust-pubsub = "0.1.0"
//! ```
//!
//! ## License
//!
//! Licensed under either of Apache License, Version 2.0 or MIT license at your option.

use crossbeam_channel::{Receiver, Sender, bounded};
use lazy_static::lazy_static;
use std::any::Any;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use uuid::Uuid;

// Your original code follows here, unchanged
lazy_static! {
    static ref PUBSUB: Arc<PubSub> = Arc::new(PubSub::new());
}

#[derive(Clone)]
pub struct TopicConfig {
    queue_depth: usize,
    overwrite: bool,
}

impl TopicConfig {
    pub fn new(queue_depth: usize, overwrite: bool) -> Self {
        TopicConfig {
            queue_depth,
            overwrite,
        }
    }
}

#[derive(Clone)]
struct MessageWrapper {
    data: Arc<dyn Any + Send + Sync>,
}

#[derive(Clone)]
struct ChannelPair {
    sender: Sender<MessageWrapper>,
    receiver: Receiver<MessageWrapper>,
    config: TopicConfig,
    subscriber_id: String,
}

impl ChannelPair {
    fn new(
        sender: Sender<MessageWrapper>,
        receiver: Receiver<MessageWrapper>,
        config: TopicConfig,
        subscriber_id: String,
    ) -> Self {
        ChannelPair {
            sender,
            receiver,
            config,
            subscriber_id,
        }
    }
}

struct TopicData {
    #[allow(dead_code)]
    name: String,
    channel_pairs: Vec<ChannelPair>,
}

struct SubscriberData {
    topic: String,
    #[allow(dead_code)]
    receiver: Receiver<MessageWrapper>,
    #[allow(dead_code)]
    callback: Option<Arc<dyn Fn(&dyn Any) + Send + Sync>>,
}

#[derive(Clone)]
pub struct ManualReceiver<T: 'static> {
    receiver: Receiver<MessageWrapper>,
    subscriber_id: String,
    pubsub: Arc<PubSub>,
    _marker: std::marker::PhantomData<T>,
}

impl<T: Clone + 'static> ManualReceiver<T> {
    pub fn try_recv(&self) -> Option<T> {
        let msg = self.receiver.try_recv().ok();

        match msg {
            Some(msg) => {
                if let Some(data) = msg.downcast::<T>() {
                    return Some(data.to_owned());
                }
                None
            }
            None => None,
        }
    }

    pub fn recv(&self) -> Option<T> {
        self.recv_timeout(None)
    }

    pub fn recv_timeout(&self, timeout_ms: Option<u64>) -> Option<T> {
        let msg = match timeout_ms {
            Some(ms) => self.receiver.recv_timeout(Duration::from_millis(ms)).ok(),
            None => self.receiver.recv().ok(),
        };

        match msg {
            Some(msg) => {
                if let Some(data) = msg.downcast::<T>() {
                    return Some(data.to_owned());
                }
                None
            }
            None => None,
        }
    }

    pub fn unsubscribe(self) {
        self.pubsub.unsubscribe(&self.subscriber_id);
    }
}

impl MessageWrapper {
    fn new<T: Send + Sync + Clone + 'static>(data: T) -> Self {
        MessageWrapper {
            data: Arc::new(data),
        }
    }

    fn downcast<T: 'static>(&self) -> Option<&T> {
        self.data.downcast_ref::<T>()
    }
}

pub struct PubSub {
    topics: Mutex<Vec<TopicData>>,
    topic_map: Mutex<HashMap<String, usize>>,
    subscribers: Mutex<HashMap<String, SubscriberData>>,
}

impl PubSub {
    fn new() -> Self {
        PubSub {
            topics: Mutex::new(Vec::new()),
            topic_map: Mutex::new(HashMap::new()),
            subscribers: Mutex::new(HashMap::new()),
        }
    }

    pub fn instance() -> Arc<PubSub> {
        PUBSUB.clone()
    }

    pub fn create_publisher(&self, topic: &str) -> usize {
        let mut topic_map = self.topic_map.lock().unwrap();

        if let Some(&index) = topic_map.get(topic) {
            return index;
        }

        let mut topics = self.topics.lock().unwrap();
        let new_index = topics.len();

        topics.push(TopicData {
            name: topic.to_string(),
            channel_pairs: Vec::new(),
        });

        topic_map.insert(topic.to_string(), new_index);

        new_index
    }

    pub fn subscribe_manual<T: Send + Sync + Clone + 'static>(
        &self,
        topic: &str,
        config: TopicConfig,
    ) -> ManualReceiver<T>
    where
        T: 'static,
    {
        let subscriber_id = Uuid::new_v4().to_string();
        let (tx, rx) = bounded(config.queue_depth);
        let topic_str = topic.to_string();

        let topic_index = self.create_publisher(topic);

        {
            let mut topics = self.topics.lock().unwrap();
            topics[topic_index].channel_pairs.push(ChannelPair::new(
                tx,
                rx.clone(),
                config.clone(),
                subscriber_id.clone(),
            ));
        }

        {
            self.subscribers.lock().unwrap().insert(
                subscriber_id.clone(),
                SubscriberData {
                    topic: topic_str.clone(),
                    receiver: rx.clone(),
                    callback: None,
                },
            );
        }

        ManualReceiver {
            receiver: rx,
            subscriber_id,
            pubsub: PubSub::instance(),
            _marker: std::marker::PhantomData,
        }
    }

    pub fn subscribe<T, F>(&self, topic: &str, config: TopicConfig, callback: F) -> String
    where
        T: Send + Sync + Clone + 'static,
        F: Fn(&T) + Send + Sync + 'static,
    {
        let subscriber_id = Uuid::new_v4().to_string();
        let (tx, rx) = bounded(config.queue_depth);
        let topic_str = topic.to_string();

        let topic_index = self.create_publisher(topic);

        {
            let mut topics = self.topics.lock().unwrap();
            topics[topic_index].channel_pairs.push(ChannelPair::new(
                tx,
                rx.clone(),
                config.clone(),
                subscriber_id.clone(),
            ));
        }

        let callback_wrapper: Arc<dyn Fn(&dyn Any) + Send + Sync> =
            Arc::new(move |data: &dyn Any| {
                if let Some(t) = data.downcast_ref::<T>() {
                    callback(t);
                }
            });

        {
            self.subscribers.lock().unwrap().insert(
                subscriber_id.clone(),
                SubscriberData {
                    topic: topic_str.clone(),
                    receiver: rx.clone(),
                    callback: Some(callback_wrapper.clone()),
                },
            );
        }

        let rx_clone = rx.clone();
        let callback_for_thread = callback_wrapper.clone();
        std::thread::spawn(move || {
            while let Ok(msg) = rx_clone.recv() {
                if let Some(data) = msg.downcast::<T>() {
                    callback_for_thread(data);
                }
            }
        });

        subscriber_id
    }

    pub fn try_publish<T: Send + Sync + Clone + 'static>(&self, topic_id: usize, message: T) {
        let msg = MessageWrapper::new(message);

        let channel_pairs = {
            let topics = self.topics.lock().unwrap();

            if topic_id >= topics.len() {
                return;
            }

            if topics[topic_id].channel_pairs.is_empty() {
                return;
            }

            topics[topic_id].channel_pairs.clone()
        };

        for pair in channel_pairs.iter() {
            if pair.config.overwrite {
                while pair.sender.is_full() {
                    let _ = pair.receiver.try_recv();
                }
            }

            let _ = pair.sender.try_send(msg.clone());
        }
    }

    pub fn publish<T: Send + Sync + Clone + 'static>(&self, topic_id: usize, message: T) {
        self.publish_with_timeout(topic_id, message, None);
    }

    pub fn publish_with_timeout<T: Send + Sync + Clone + 'static>(
        &self,
        topic_id: usize,
        message: T,
        max_wait_ms: Option<u64>,
    ) {
        let msg = MessageWrapper::new(message);

        let channel_pairs = {
            let topics = self.topics.lock().unwrap();

            if topic_id >= topics.len() {
                return;
            }

            if topics[topic_id].channel_pairs.is_empty() {
                return;
            }

            topics[topic_id].channel_pairs.clone()
        };

        for pair in channel_pairs.iter() {
            if pair.config.overwrite {
                while pair.sender.is_full() {
                    let _ = pair.receiver.try_recv();
                }
                let _ = pair.sender.try_send(msg.clone());
            } else {
                match max_wait_ms {
                    Some(ms) => {
                        let _ = pair
                            .sender
                            .send_timeout(msg.clone(), Duration::from_millis(ms));
                    }
                    None => {
                        let _ = pair.sender.send(msg.clone());
                    }
                }
            }
        }
    }

    pub fn unsubscribe(&self, subscriber_id: &str) {
        let topic_opt = {
            let mut subscribers = self.subscribers.lock().unwrap();
            if let Some(data) = subscribers.remove(subscriber_id) {
                Some(data.topic)
            } else {
                None
            }
        };

        if let Some(topic) = topic_opt {
            let topic_index_opt = {
                let topic_map = self.topic_map.lock().unwrap();
                topic_map.get(&topic).cloned()
            };

            if let Some(topic_index) = topic_index_opt {
                let mut topics = self.topics.lock().unwrap();
                if let Some(topic_data) = topics.get_mut(topic_index) {
                    topic_data
                        .channel_pairs
                        .retain(|pair| pair.subscriber_id != subscriber_id);

                    if topic_data.channel_pairs.is_empty() {
                        let mut topic_map = self.topic_map.lock().unwrap();
                        topic_map.remove(&topic);
                    }
                }
            }
        }
    }
}