infinity-bridge-host 0.1.2

Host-side WebSocket server for infinity-bridge — async tokio, no framework dependency
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
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;

use infinity_bridge_wire::{
    AckPayload, BridgeError, CmdPayload, EventPayload, HelloPayload, WireMsg,
};
use serde_json::Value;
use tokio::sync::{Mutex, broadcast, mpsc, oneshot, watch};
use uuid::Uuid;

use crate::client::Client;

#[derive(Debug, Clone)]
pub struct ClientInfo {
    pub id: u64,
    pub hello: Option<HelloPayload>,
}

pub(crate) struct Hub {
    next_id: AtomicU64,
    clients: Mutex<HashMap<u64, Client>>,
    pending: Mutex<HashMap<String, oneshot::Sender<AckPayload>>>,

    event_tx: broadcast::Sender<EventPayload>,

    connection_tx: watch::Sender<bool>,
    connection_rx: watch::Receiver<bool>,

    connect_notify: tokio::sync::Notify,
}

impl Hub {
    pub fn new(event_capacity: usize) -> Arc<Self> {
        let (event_tx, _) = broadcast::channel(event_capacity);
        let (connection_tx, connection_rx) = watch::channel(false);

        Arc::new(Self {
            next_id: AtomicU64::new(1),
            clients: Mutex::new(HashMap::new()),
            pending: Mutex::new(HashMap::new()),
            event_tx,
            connection_tx,
            connection_rx,
            connect_notify: tokio::sync::Notify::new(),
        })
    }

    pub async fn register_client(&self, tx: mpsc::UnboundedSender<String>) -> u64 {
        let id = self.next_id.fetch_add(1, Ordering::Relaxed);
        let mut clients = self.clients.lock().await;
        clients.insert(
            id,
            Client {
                tx,
                hello: None,
                last_seen: tokio::time::Instant::now(),
                ready: false,
            },
        );
        self.connection_tx.send_replace(true);
        self.connect_notify.notify_waiters();
        id
    }

    pub async fn set_client_hello(&self, id: u64, hello: HelloPayload) {
        let mut clients = self.clients.lock().await;
        if let Some(c) = clients.get_mut(&id) {
            c.hello = Some(hello);
        }
    }

    pub async fn unregister_client(&self, id: u64) {
        let mut clients = self.clients.lock().await;
        clients.remove(&id);
        let connected = !clients.is_empty();
        self.connection_tx.send_replace(connected);
    }

    pub async fn touch_client(&self, id: u64) {
        let mut clients = self.clients.lock().await;
        if let Some(c) = clients.get_mut(&id) {
            c.last_seen = tokio::time::Instant::now();
        }
    }

    pub async fn reap_dead_clients(&self, timeout: Duration) -> Vec<u64> {
        let now = tokio::time::Instant::now();
        let mut clients = self.clients.lock().await;
        let dead: Vec<u64> = clients
            .iter()
            .filter(|(_, c)| now.duration_since(c.last_seen) > timeout)
            .map(|(&id, _)| id)
            .collect();

        for &id in &dead {
            clients.remove(&id);
        }

        if !dead.is_empty() {
            let connected = !clients.is_empty();
            self.connection_tx.send_replace(connected);
        }

        dead
    }

    /// Record a client's downstream readiness (see
    /// [`infinity_bridge_wire::READY_EVENT`]).
    pub async fn set_client_ready(&self, id: u64, ready: bool) {
        let mut clients = self.clients.lock().await;
        if let Some(c) = clients.get_mut(&id) {
            c.ready = ready;
        }
    }

    pub async fn is_connected(&self) -> bool {
        !self.clients.lock().await.is_empty()
    }

    /// `true` when at least one client has reported its downstream link bound.
    ///
    /// Distinct from [`Self::is_connected`]: a relay gauge can hold an open
    /// socket for the whole session while the module it fronts is still
    /// loading, absent, or has lost its IPC binding.
    pub async fn is_ready(&self) -> bool {
        self.clients.lock().await.values().any(|c| c.ready)
    }

    pub async fn wait_connected(&self) {
        loop {
            if self.is_connected().await {
                return;
            }
            self.connect_notify.notified().await;
        }
    }

    pub fn subscribe_connection_status(&self) -> watch::Receiver<bool> {
        self.connection_rx.clone()
    }

    pub async fn connected_clients(&self) -> Vec<ClientInfo> {
        let clients = self.clients.lock().await;
        clients
            .iter()
            .map(|(&id, c)| ClientInfo {
                id,
                hello: c.hello.clone(),
            })
            .collect()
    }

    pub fn subscribe_events(&self) -> broadcast::Receiver<EventPayload> {
        self.event_tx.subscribe()
    }

    pub fn dispatch_event(&self, event: EventPayload) {
        let _ = self.event_tx.send(event);
    }

    pub async fn emit(&self, name: impl Into<String>, data: Value) -> Result<(), BridgeError> {
        let msg = WireMsg::Event(EventPayload::new(name, data));
        let json = msg.to_json()?;

        let clients = self.clients.lock().await;
        if clients.is_empty() {
            return Err(BridgeError::no_clients(
                "no gauges connected — event dropped",
            ));
        }

        let mut send_failures = 0u32;
        for client in clients.values() {
            if client.tx.send(json.clone()).is_err() {
                send_failures += 1;
            }
        }

        if send_failures > 0 && send_failures as usize == clients.len() {
            return Err(BridgeError::transport(
                "all gauge connections failed to accept event",
            ));
        }

        Ok(())
    }

    pub async fn command(
        &self,
        name: Option<&str>,
        payload: Value,
        timeout: Duration,
    ) -> Result<Value, BridgeError> {
        let id = Uuid::new_v4().to_string();

        let cmd = match name {
            Some(n) => CmdPayload::named(id.clone(), n, payload),
            None => CmdPayload::new(id.clone(), payload),
        };
        let msg = WireMsg::Cmd(cmd);
        let json = msg.to_json()?;

        let (ack_tx, ack_rx) = oneshot::channel();
        self.pending.lock().await.insert(id.clone(), ack_tx);

        {
            let clients = self.clients.lock().await;
            if clients.is_empty() {
                self.pending.lock().await.remove(&id);
                return Err(BridgeError::no_clients(
                    "no gauges connected — cannot send command",
                ));
            }

            // Prefer clients that have reported their downstream link bound.
            // Broadcasting to a relay whose CommBus isn't attached burns the
            // full timeout on a socket that was never going to answer. When
            // nothing has ever reported ready the flag carries no information
            // (older relays don't send it), so fall back to every client.
            let any_ready = clients.values().any(|c| c.ready);
            let mut delivered = 0usize;
            for client in clients.values() {
                if any_ready && !client.ready {
                    continue;
                }
                if client.tx.send(json.clone()).is_ok() {
                    delivered += 1;
                }
            }

            // Every target's writer half is gone: the sockets are dead but not
            // yet unregistered. Fail now instead of waiting out the timeout —
            // the caller can retry into a fresh connection immediately.
            if delivered == 0 {
                self.pending.lock().await.remove(&id);
                return Err(BridgeError::transport(
                    "no gauge connection accepted the command",
                ));
            }
        }

        let ack_result = tokio::time::timeout(timeout, ack_rx).await;

        self.pending.lock().await.remove(&id);

        match ack_result {
            Ok(Ok(ack)) => {
                if ack.ok {
                    Ok(ack.response.unwrap_or(Value::Null))
                } else {
                    Err(BridgeError::application(
                        ack.error.unwrap_or_else(|| "unknown error".into()),
                    ))
                }
            }
            Ok(Err(_)) => Err(BridgeError::transport(
                "all gauge connections dropped before ack",
            )),
            Err(_) => Err(BridgeError::timeout(format!(
                "no ack received within {timeout:?}"
            ))),
        }
    }

    pub async fn dispatch_ack(&self, ack: AckPayload) {
        let tx = self.pending.lock().await.remove(&ack.id);
        if let Some(tx) = tx {
            let _ = tx.send(ack);
        }
    }

    pub async fn send_to(&self, client_id: u64, json: String) -> Result<(), BridgeError> {
        let clients = self.clients.lock().await;
        let client = clients
            .get(&client_id)
            .ok_or_else(|| BridgeError::transport(format!("client {client_id} not found")))?;
        client
            .tx
            .send(json)
            .map_err(|_| BridgeError::transport(format!("client {client_id} channel closed")))
    }

    pub async fn broadcast(&self, json: String) -> Result<(), BridgeError> {
        let clients = self.clients.lock().await;
        if clients.is_empty() {
            return Err(BridgeError::no_clients("no gauges connected"));
        }
        for client in clients.values() {
            let _ = client.tx.send(json.clone());
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use infinity_bridge_wire::ErrorKind;

    /// Long enough to prove nothing was delivered, short enough to keep the
    /// suite quick — no ack is ever sent in these tests.
    const NO_ACK: Duration = Duration::from_millis(50);

    #[tokio::test]
    async fn readiness_is_tracked_per_client() {
        let hub = Hub::new(16);
        let (tx_a, _rx_a) = mpsc::unbounded_channel();
        let (tx_b, _rx_b) = mpsc::unbounded_channel();
        let a = hub.register_client(tx_a).await;
        let _b = hub.register_client(tx_b).await;

        assert!(hub.is_connected().await);
        assert!(!hub.is_ready().await, "a fresh client is not yet ready");

        hub.set_client_ready(a, true).await;
        assert!(hub.is_ready().await);

        hub.set_client_ready(a, false).await;
        assert!(!hub.is_ready().await);
        assert!(hub.is_connected().await, "readiness is not connectedness");
    }

    #[tokio::test]
    async fn a_command_skips_clients_that_are_not_ready() {
        let hub = Hub::new(16);
        let (tx_stale, mut rx_stale) = mpsc::unbounded_channel();
        let (tx_live, mut rx_live) = mpsc::unbounded_channel();
        let _stale = hub.register_client(tx_stale).await;
        let live = hub.register_client(tx_live).await;
        hub.set_client_ready(live, true).await;

        let err = hub
            .command(Some("ping"), Value::Null, NO_ACK)
            .await
            .expect_err("nothing acks in this test");
        assert_eq!(err.kind(), ErrorKind::Timeout);

        assert!(
            rx_live.try_recv().is_ok(),
            "the ready client got the command"
        );
        assert!(
            rx_stale.try_recv().is_err(),
            "a relay that never reported ready must not absorb the timeout budget"
        );
    }

    #[tokio::test]
    async fn a_command_goes_everywhere_while_readiness_is_unknown() {
        // Relays predating the ready event never send one. Their silence must
        // read as "unknown", not "unreachable", or they stop working entirely.
        let hub = Hub::new(16);
        let (tx_a, mut rx_a) = mpsc::unbounded_channel();
        let (tx_b, mut rx_b) = mpsc::unbounded_channel();
        hub.register_client(tx_a).await;
        hub.register_client(tx_b).await;

        let _ = hub.command(Some("ping"), Value::Null, NO_ACK).await;

        assert!(rx_a.try_recv().is_ok());
        assert!(rx_b.try_recv().is_ok());
    }

    #[tokio::test]
    async fn a_command_fails_fast_when_every_connection_is_gone() {
        // Sockets can be dead before the reaper has unregistered them. Waiting
        // out the full timeout for a send that provably went nowhere is time
        // the caller could have spent retrying into a fresh connection.
        let hub = Hub::new(16);
        let (tx, rx) = mpsc::unbounded_channel();
        hub.register_client(tx).await;
        drop(rx);

        let start = tokio::time::Instant::now();
        let err = hub
            .command(Some("ping"), Value::Null, Duration::from_secs(30))
            .await
            .expect_err("the only receiver is gone");
        assert_eq!(err.kind(), ErrorKind::Transport);
        assert!(
            start.elapsed() < Duration::from_secs(1),
            "should not have waited out the timeout"
        );
    }

    #[tokio::test]
    async fn a_timed_out_command_leaves_no_pending_entry() {
        let hub = Hub::new(16);
        let (tx, _rx) = mpsc::unbounded_channel();
        hub.register_client(tx).await;

        let _ = hub.command(Some("ping"), Value::Null, NO_ACK).await;
        assert!(hub.pending.lock().await.is_empty());
    }

    #[tokio::test(start_paused = true)]
    async fn a_silent_client_is_reaped_and_named() {
        let hub = Hub::new(16);
        let (tx, _rx) = mpsc::unbounded_channel();
        let id = hub.register_client(tx).await;

        tokio::time::advance(Duration::from_secs(31)).await;
        let dead = hub.reap_dead_clients(Duration::from_secs(30)).await;

        assert_eq!(dead, vec![id], "the reaper names who it dropped");
        assert!(!hub.is_connected().await);
    }
}