shelly-liveview 0.3.0

Core runtime primitives for Shelly LiveView.
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
use crate::ServerMessage;
use std::{
    collections::{BTreeMap, HashMap, HashSet},
    future::Future,
    pin::Pin,
    sync::{Arc, Mutex},
};
use tokio::sync::broadcast;

const DEFAULT_TOPIC_CAPACITY: usize = 1024;

/// Delivery topology for one PubSub backend.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PubSubDeliveryScope {
    /// Fanout stays inside one process/runtime instance.
    LocalProcess,
    /// Fanout can be shared across multiple runtime instances.
    Cluster,
}

/// Ordering contract for one PubSub backend.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PubSubOrdering {
    /// Messages are delivered in topic order.
    PerTopicOrdered,
    /// Ordering is best effort and may be reordered by backend behavior.
    BestEffort,
}

/// Session-affinity contract for one PubSub backend.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SessionAffinityRequirement {
    /// Session affinity is not required for backend fanout.
    None,
    /// Session affinity is required for stateful reconnect/session continuity.
    StatefulSessionRequired,
}

/// Cluster capabilities advertised by a PubSub backend.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PubSubCapabilities {
    pub backend: String,
    pub delivery_scope: PubSubDeliveryScope,
    pub ordering: PubSubOrdering,
    pub session_affinity: SessionAffinityRequirement,
    pub presence_tracking: bool,
}

impl PubSubCapabilities {
    fn in_process() -> Self {
        Self {
            backend: "in_process".to_string(),
            delivery_scope: PubSubDeliveryScope::LocalProcess,
            ordering: PubSubOrdering::PerTopicOrdered,
            session_affinity: SessionAffinityRequirement::StatefulSessionRequired,
            presence_tracking: true,
        }
    }
}

/// Presence snapshot for one topic across nodes.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PubSubPresenceSnapshot {
    pub topic: String,
    pub total_sessions: usize,
    pub by_node: BTreeMap<String, usize>,
}

impl PubSubPresenceSnapshot {
    fn empty(topic: &str) -> Self {
        Self {
            topic: topic.to_string(),
            total_sessions: 0,
            by_node: BTreeMap::new(),
        }
    }
}

/// Errors produced while receiving subscription fanout.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PubSubReceiveError {
    Closed,
    Lagged(u64),
}

type PubSubRecvFuture<'a> =
    Pin<Box<dyn Future<Output = Result<PubSubMessage, PubSubReceiveError>> + Send + 'a>>;

/// Backend-owned receiver trait for one subscription.
pub trait PubSubSubscriptionHandle: Send {
    fn recv(&mut self) -> PubSubRecvFuture<'_>;
}

struct BroadcastSubscriptionHandle {
    receiver: broadcast::Receiver<PubSubMessage>,
}

impl PubSubSubscriptionHandle for BroadcastSubscriptionHandle {
    fn recv(&mut self) -> PubSubRecvFuture<'_> {
        Box::pin(async move {
            self.receiver.recv().await.map_err(|err| match err {
                broadcast::error::RecvError::Closed => PubSubReceiveError::Closed,
                broadcast::error::RecvError::Lagged(skipped) => PubSubReceiveError::Lagged(skipped),
            })
        })
    }
}

/// Backend interface for adapter-executed PubSub commands.
pub trait PubSubBackend: Send + Sync {
    fn subscribe(&self, topic: &str) -> PubSubSubscription;
    fn broadcast(&self, topic: &str, messages: Vec<ServerMessage>) -> usize;
    fn capabilities(&self) -> PubSubCapabilities;

    fn register_presence(&self, _topic: &str, _session_id: &str, _node_id: &str) {}

    fn unregister_presence(&self, _topic: &str, _session_id: &str, _node_id: &str) {}

    fn presence_snapshot(&self, topic: &str) -> PubSubPresenceSnapshot {
        PubSubPresenceSnapshot::empty(topic)
    }
}

#[derive(Debug)]
struct InProcessPubSubBackend {
    topics: Arc<Mutex<HashMap<String, broadcast::Sender<PubSubMessage>>>>,
    presence: Arc<Mutex<HashMap<String, HashMap<String, HashSet<String>>>>>,
    topic_capacity: usize,
}

impl InProcessPubSubBackend {
    fn new(topic_capacity: usize) -> Self {
        Self {
            topics: Arc::new(Mutex::new(HashMap::new())),
            presence: Arc::new(Mutex::new(HashMap::new())),
            topic_capacity,
        }
    }

    fn sender_for(&self, topic: &str) -> broadcast::Sender<PubSubMessage> {
        let mut topics = self.topics.lock().expect("pubsub topic mutex poisoned");
        topics
            .entry(topic.to_string())
            .or_insert_with(|| {
                let (sender, _) = broadcast::channel(self.topic_capacity);
                sender
            })
            .clone()
    }
}

impl PubSubBackend for InProcessPubSubBackend {
    fn subscribe(&self, topic: &str) -> PubSubSubscription {
        let sender = self.sender_for(topic);
        PubSubSubscription::new(BroadcastSubscriptionHandle {
            receiver: sender.subscribe(),
        })
    }

    fn broadcast(&self, topic: &str, messages: Vec<ServerMessage>) -> usize {
        let sender = self.sender_for(topic);
        sender
            .send(PubSubMessage {
                topic: topic.to_string(),
                messages,
            })
            .unwrap_or_default()
    }

    fn capabilities(&self) -> PubSubCapabilities {
        PubSubCapabilities::in_process()
    }

    fn register_presence(&self, topic: &str, session_id: &str, node_id: &str) {
        let mut presence = self
            .presence
            .lock()
            .expect("pubsub presence mutex poisoned");
        presence
            .entry(topic.to_string())
            .or_default()
            .entry(node_id.to_string())
            .or_default()
            .insert(session_id.to_string());
    }

    fn unregister_presence(&self, topic: &str, session_id: &str, node_id: &str) {
        let mut presence = self
            .presence
            .lock()
            .expect("pubsub presence mutex poisoned");
        let mut remove_topic = false;
        if let Some(by_node) = presence.get_mut(topic) {
            if let Some(sessions) = by_node.get_mut(node_id) {
                sessions.remove(session_id);
                if sessions.is_empty() {
                    by_node.remove(node_id);
                }
            }
            remove_topic = by_node.is_empty();
        }
        if remove_topic {
            presence.remove(topic);
        }
    }

    fn presence_snapshot(&self, topic: &str) -> PubSubPresenceSnapshot {
        let presence = self
            .presence
            .lock()
            .expect("pubsub presence mutex poisoned");
        let Some(by_node) = presence.get(topic) else {
            return PubSubPresenceSnapshot::empty(topic);
        };
        let mut snapshot = PubSubPresenceSnapshot {
            topic: topic.to_string(),
            total_sessions: 0,
            by_node: BTreeMap::new(),
        };
        for (node_id, sessions) in by_node {
            snapshot.total_sessions += sessions.len();
            snapshot.by_node.insert(node_id.clone(), sessions.len());
        }
        snapshot
    }
}

/// Adapter-owned PubSub runtime abstraction.
#[derive(Clone)]
pub struct PubSub {
    backend: Arc<dyn PubSubBackend>,
}

impl std::fmt::Debug for PubSub {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PubSub")
            .field("capabilities", &self.capabilities())
            .finish()
    }
}

impl Default for PubSub {
    fn default() -> Self {
        Self::new(DEFAULT_TOPIC_CAPACITY)
    }
}

impl PubSub {
    /// Create the built-in in-process backend.
    pub fn new(topic_capacity: usize) -> Self {
        Self::with_backend(InProcessPubSubBackend::new(topic_capacity))
    }

    /// Wrap a custom backend implementation (for clustered fanout backends).
    pub fn with_backend<B>(backend: B) -> Self
    where
        B: PubSubBackend + 'static,
    {
        Self {
            backend: Arc::new(backend),
        }
    }

    /// Subscribe to a topic.
    pub fn subscribe(&self, topic: impl Into<String>) -> PubSubSubscription {
        let topic = topic.into();
        self.backend.subscribe(&topic)
    }

    /// Broadcast one payload to all subscribers on a topic.
    pub fn broadcast(&self, topic: impl Into<String>, messages: Vec<ServerMessage>) -> usize {
        let topic = topic.into();
        self.backend.broadcast(&topic, messages)
    }

    /// Backend cluster/delivery capability contract.
    pub fn capabilities(&self) -> PubSubCapabilities {
        self.backend.capabilities()
    }

    /// Register one session as present for a topic on one cluster node.
    pub fn register_presence(
        &self,
        topic: impl Into<String>,
        session_id: impl Into<String>,
        node_id: impl Into<String>,
    ) {
        let topic = topic.into();
        let session_id = session_id.into();
        let node_id = node_id.into();
        self.backend
            .register_presence(&topic, &session_id, &node_id);
    }

    /// Unregister one session presence from a topic on one cluster node.
    pub fn unregister_presence(
        &self,
        topic: impl Into<String>,
        session_id: impl Into<String>,
        node_id: impl Into<String>,
    ) {
        let topic = topic.into();
        let session_id = session_id.into();
        let node_id = node_id.into();
        self.backend
            .unregister_presence(&topic, &session_id, &node_id);
    }

    /// Return presence counts for one topic.
    pub fn presence_snapshot(&self, topic: impl Into<String>) -> PubSubPresenceSnapshot {
        let topic = topic.into();
        self.backend.presence_snapshot(&topic)
    }
}

/// Message delivered by one PubSub backend.
#[derive(Debug, Clone, PartialEq)]
pub struct PubSubMessage {
    pub topic: String,
    pub messages: Vec<ServerMessage>,
}

/// Live subscription receiver for one topic.
pub struct PubSubSubscription {
    inner: Box<dyn PubSubSubscriptionHandle>,
}

impl PubSubSubscription {
    /// Create one subscription from a custom backend receiver handle.
    pub fn new<H>(handle: H) -> Self
    where
        H: PubSubSubscriptionHandle + 'static,
    {
        Self {
            inner: Box::new(handle),
        }
    }

    pub async fn recv(&mut self) -> Result<PubSubMessage, PubSubReceiveError> {
        self.inner.recv().await
    }
}

/// Internal commands collected from `Context` and executed by the adapter.
#[derive(Debug, Clone, PartialEq)]
pub enum PubSubCommand {
    Subscribe {
        topic: String,
    },
    Broadcast {
        topic: String,
        messages: Vec<ServerMessage>,
    },
}

#[cfg(test)]
mod tests {
    use super::{
        BroadcastSubscriptionHandle, PubSub, PubSubBackend, PubSubCapabilities,
        PubSubDeliveryScope, PubSubMessage, PubSubOrdering, PubSubSubscription,
        SessionAffinityRequirement,
    };
    use crate::ServerMessage;
    use std::{
        collections::HashMap,
        sync::{Arc, Mutex},
    };
    use tokio::sync::broadcast;

    #[tokio::test]
    async fn in_process_pubsub_broadcasts_to_subscribers() {
        let pubsub = PubSub::default();
        let mut first = pubsub.subscribe("chat:lobby");
        let mut second = pubsub.subscribe("chat:lobby");

        assert_eq!(
            pubsub.broadcast(
                "chat:lobby",
                vec![ServerMessage::Redirect {
                    to: "/ok".to_string()
                }]
            ),
            2
        );

        assert_eq!(first.recv().await.unwrap().topic, "chat:lobby");
        assert_eq!(
            second.recv().await.unwrap().messages,
            vec![ServerMessage::Redirect {
                to: "/ok".to_string()
            }]
        );
    }

    #[test]
    fn in_process_pubsub_reports_cluster_capabilities_and_presence() {
        let pubsub = PubSub::default();
        let capabilities = pubsub.capabilities();
        assert_eq!(capabilities.backend, "in_process");
        assert_eq!(
            capabilities.delivery_scope,
            PubSubDeliveryScope::LocalProcess
        );
        assert_eq!(capabilities.ordering, PubSubOrdering::PerTopicOrdered);
        assert_eq!(
            capabilities.session_affinity,
            SessionAffinityRequirement::StatefulSessionRequired
        );
        assert!(capabilities.presence_tracking);

        pubsub.register_presence("chat:lobby", "s1", "node-a");
        pubsub.register_presence("chat:lobby", "s2", "node-a");
        pubsub.register_presence("chat:lobby", "s3", "node-b");
        let snapshot = pubsub.presence_snapshot("chat:lobby");
        assert_eq!(snapshot.topic, "chat:lobby");
        assert_eq!(snapshot.total_sessions, 3);
        assert_eq!(snapshot.by_node.get("node-a"), Some(&2));
        assert_eq!(snapshot.by_node.get("node-b"), Some(&1));

        pubsub.unregister_presence("chat:lobby", "s2", "node-a");
        let after = pubsub.presence_snapshot("chat:lobby");
        assert_eq!(after.total_sessions, 2);
        assert_eq!(after.by_node.get("node-a"), Some(&1));
    }

    #[derive(Debug, Clone)]
    struct SharedHub {
        topics: Arc<Mutex<HashMap<String, broadcast::Sender<PubSubMessage>>>>,
    }

    impl SharedHub {
        fn new() -> Self {
            Self {
                topics: Arc::new(Mutex::new(HashMap::new())),
            }
        }

        fn sender_for(&self, topic: &str) -> broadcast::Sender<PubSubMessage> {
            let mut topics = self.topics.lock().expect("hub mutex poisoned");
            topics
                .entry(topic.to_string())
                .or_insert_with(|| {
                    let (tx, _) = broadcast::channel(256);
                    tx
                })
                .clone()
        }
    }

    #[derive(Debug, Clone)]
    struct MockClusterBackend {
        hub: SharedHub,
    }

    impl PubSubBackend for MockClusterBackend {
        fn subscribe(&self, topic: &str) -> PubSubSubscription {
            let receiver = self.hub.sender_for(topic).subscribe();
            PubSubSubscription::new(BroadcastSubscriptionHandle { receiver })
        }

        fn broadcast(&self, topic: &str, messages: Vec<ServerMessage>) -> usize {
            self.hub
                .sender_for(topic)
                .send(PubSubMessage {
                    topic: topic.to_string(),
                    messages,
                })
                .unwrap_or_default()
        }

        fn capabilities(&self) -> PubSubCapabilities {
            PubSubCapabilities {
                backend: "mock_cluster".to_string(),
                delivery_scope: PubSubDeliveryScope::Cluster,
                ordering: PubSubOrdering::BestEffort,
                session_affinity: SessionAffinityRequirement::StatefulSessionRequired,
                presence_tracking: false,
            }
        }
    }

    #[tokio::test]
    async fn custom_backend_can_fanout_across_multiple_pubsub_instances() {
        let hub = SharedHub::new();
        let node_a = PubSub::with_backend(MockClusterBackend { hub: hub.clone() });
        let node_b = PubSub::with_backend(MockClusterBackend { hub });

        let mut subscription = node_a.subscribe("cluster:lobby");
        assert_eq!(
            node_b.broadcast(
                "cluster:lobby",
                vec![ServerMessage::Error {
                    message: "hello".to_string(),
                    code: Some("cluster".to_string()),
                }]
            ),
            1
        );

        let delivered = subscription.recv().await.unwrap();
        assert_eq!(delivered.topic, "cluster:lobby");
        assert_eq!(delivered.messages.len(), 1);
        match &delivered.messages[0] {
            ServerMessage::Error { message, code } => {
                assert_eq!(message, "hello");
                assert_eq!(code.as_deref(), Some("cluster"));
            }
            other => panic!("unexpected payload: {other:?}"),
        }
    }
}