gent-daemon 0.2.4

Local daemon, storage and synchronization for Gent chat clusters.
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
//! Every request is scoped to one negotiated watch. Losing that watch drops
//! its queued sends and receipt waiters. The durable MLS outbox owns retries.
use crate::sync::{
    SharedStore, Stream, direct::session, health::Health, local, transfer::Prepared,
};
use anyhow::{Context, Result, ensure};
use gent_protocol::transport_wss::{Frame, Kind, Uuid};
use std::{
    collections::BTreeMap,
    sync::{
        Arc, Mutex,
        atomic::{AtomicU64, Ordering},
    },
    time::Duration,
};
use tokio::{
    sync::{mpsc, oneshot},
    task::JoinSet,
};

const CONCURRENT: usize = 4;
const COOLDOWN: Duration = Duration::from_secs(30);
const MAX_PEERS: usize = 64;
const REPLY_TIMEOUT: Duration = Duration::from_secs(4);
const _: () = assert!(session::MAX_FRAME_BYTES <= gent_protocol::transport_wss::MAX_SESSION);
const _: () = assert!(session::VERSION == 2);

#[derive(Default)]
pub(in crate::sync) struct Metrics {
    sent_bytes: AtomicU64,
    received_bytes: AtomicU64,
    stats_sent_frames: AtomicU64,
    stats_sent_bytes: AtomicU64,
    stats_received_frames: AtomicU64,
    stats_received_bytes: AtomicU64,
    confirmed_batches: AtomicU64,
    fallbacks: AtomicU64,
}
impl Metrics {
    pub(in crate::sync) fn snapshot(&self) -> serde_json::Value {
        serde_json::json!({
            "sent_frame_bytes": self.sent_bytes.load(Ordering::Relaxed),
            "received_frame_bytes": self.received_bytes.load(Ordering::Relaxed),
            "stats_sent_frames": self.stats_sent_frames.load(Ordering::Relaxed),
            "stats_sent_frame_bytes": self.stats_sent_bytes.load(Ordering::Relaxed),
            "stats_received_frames": self.stats_received_frames.load(Ordering::Relaxed),
            "stats_received_frame_bytes": self.stats_received_bytes.load(Ordering::Relaxed),
            "confirmed_batches": self.confirmed_batches.load(Ordering::Relaxed),
            "fallbacks": self.fallbacks.load(Ordering::Relaxed),
        })
    }
}

struct Request {
    peer: Uuid,
    payload: Vec<u8>,
    reply: oneshot::Sender<Vec<u8>>,
}

struct StatsRequest {
    request: Request,
    dispatched: oneshot::Sender<()>,
}

struct Route {
    channel: mpsc::Sender<Request>,
    stats: mpsc::Sender<StatsRequest>,
    failed: Mutex<BTreeMap<Uuid, tokio::time::Instant>>,
}
impl Route {
    fn available(&self, peer: Uuid, now: tokio::time::Instant) -> bool {
        let mut failed = self.failed.lock().expect("peer cooldown lock poisoned");
        failed.retain(|_, until| *until > now);
        !failed.contains_key(&peer)
    }
    fn failed(&self, peer: Uuid, now: tokio::time::Instant) {
        let mut failed = self.failed.lock().expect("peer cooldown lock poisoned");
        failed.retain(|_, until| *until > now);
        if failed.len() == MAX_PEERS
            && !failed.contains_key(&peer)
            && let Some((&oldest, _)) = failed.iter().min_by_key(|(_, until)| **until)
        {
            failed.remove(&oldest);
        }
        failed.insert(peer, now + COOLDOWN);
    }
}

#[derive(Clone, Default)]
pub(in crate::sync) struct Wss {
    channel: Arc<Mutex<Option<Arc<Route>>>>,
    pub(super) metrics: Arc<Metrics>,
    stats: Arc<crate::sync::health::StatsWatch>,
}

impl Wss {
    pub(super) fn new(stats: Arc<crate::sync::health::StatsWatch>) -> Self {
        Self {
            stats,
            ..Default::default()
        }
    }
    // One side request at a time. Stats never marks a chat route failed or
    // waits for remote I/O on the ordinary transfer loop.
    pub(super) async fn stats_worker(self, store: SharedStore) {
        let tick = &self.stats.tick;
        let mut delay = COOLDOWN;
        loop {
            tokio::select! { _=tick.notified()=>{}, _=tokio::time::sleep(delay)=>{} }
            delay = COOLDOWN;
            for batch in 0..16 {
                let route = self
                    .channel
                    .lock()
                    .expect("peer watch lock poisoned")
                    .clone();
                let Some(route) = route else {
                    break;
                };
                let Ok(Some(sent)) = local(store.clone(), |s| s.next_stats_send()).await else {
                    delay = local(store.clone(), |s| s.stats_worker_delay())
                        .await
                        .unwrap_or(COOLDOWN);
                    break;
                };
                let (reply, result) = oneshot::channel();
                let (dispatch, dispatched) = oneshot::channel();
                let queued = route
                    .stats
                    .try_send(StatsRequest {
                        request: Request {
                            peer: sent.peer,
                            payload: sent.wire.clone(),
                            reply,
                        },
                        dispatched: dispatch,
                    })
                    .is_ok();
                // A busy local writer says nothing about a peer's reachability.
                // Start the remote receipt timer only when the writer admits
                // this frame. A cancelled admission cannot be sent later.
                if queued
                    && matches!(
                        tokio::time::timeout(REPLY_TIMEOUT, dispatched).await,
                        Ok(Ok(()))
                    )
                {
                    let response = tokio::time::timeout(REPLY_TIMEOUT, result)
                        .await
                        .ok()
                        .and_then(Result::ok);
                    let _ = local(store.clone(), move |s| {
                        s.finish_stats_send(&sent, response.as_deref())
                    })
                    .await;
                } else {
                    drop(result);
                    let _ = local(store.clone(), move |s| s.defer_stats_send(&sent)).await;
                    delay = Duration::from_millis(100);
                    break;
                }
                if batch == 15 {
                    tick.notify_one();
                }
                tokio::time::sleep(Duration::from_millis(10)).await;
            }
        }
    }

    pub(in crate::sync) async fn transfer(
        &self,
        store: &SharedStore,
        entry: &Stream,
        health: &Health,
        prepared: &mut Prepared,
    ) -> Option<Result<Option<session::Confirmed>>> {
        if !entry.outgoing || entry.configuration {
            return None;
        }
        let route = self
            .channel
            .lock()
            .expect("peer watch lock poisoned")
            .clone()?;
        let group = entry.group.clone();
        let peer = local(store.clone(), move |s| {
            Ok(Uuid::parse_str(&s.session_peer(&group, true)?)?)
        })
        .await
        .ok()?;
        if !route.available(peer, tokio::time::Instant::now()) {
            return None;
        }
        let channel = route.channel.clone();
        let result = session::push(
            store.clone(),
            &entry.group,
            prepared,
            |payload| async move {
                let (reply, result) = oneshot::channel();
                channel
                    .try_send(Request {
                        peer,
                        payload,
                        reply,
                    })
                    .map_err(|_| anyhow::anyhow!("peer watch busy or disconnected"))?;
                Ok(tokio::time::timeout(REPLY_TIMEOUT, result).await??)
            },
        )
        .await;
        match result {
            Ok(confirmed) => {
                if let Some(batch) = &confirmed {
                    health.record_transport(
                        &entry.group,
                        crate::sync::health::TransportPath::RelayWss,
                        crate::sync::health::TransportEvidence::SignedReceipt,
                        batch.last,
                    );
                    self.metrics
                        .confirmed_batches
                        .fetch_add(1, Ordering::Relaxed);
                } else {
                    health.idle(&entry.group);
                }
                Some(Ok(confirmed))
            }
            Err(_) => {
                route.failed(peer, tokio::time::Instant::now());
                self.metrics.fallbacks.fetch_add(1, Ordering::Relaxed);
                None
            }
        }
    }
}

struct PendingRequest {
    peer: Uuid,
    reply: oneshot::Sender<Vec<u8>>,
    stats: bool,
}
type Pending = BTreeMap<Uuid, PendingRequest>;
struct Reply {
    peer: Uuid,
    id: Uuid,
    receipt: Option<Vec<u8>>,
}
impl Reply {
    fn encode(self) -> Result<Vec<u8>> {
        let (kind, payload) = match &self.receipt {
            Some(receipt) => (Kind::Receipt, receipt.as_slice()),
            None => (Kind::Unavailable, &[][..]),
        };
        Ok(Frame {
            kind,
            peer: self.peer,
            id: self.id,
            payload,
        }
        .encode()?)
    }
}

pub(super) struct Connection {
    route: Wss,
    incoming: mpsc::Receiver<Request>,
    stats_incoming: mpsc::Receiver<StatsRequest>,
    pending: Pending,
    outgoing_stats: bool,
    receivers: JoinSet<Reply>,
    stats_receivers: JoinSet<Reply>,
}

impl Connection {
    pub(super) fn open(route: &Wss) -> Self {
        let (tx, incoming) = mpsc::channel(CONCURRENT);
        let (stats, stats_incoming) = mpsc::channel(1);
        *route.channel.lock().expect("peer watch lock poisoned") = Some(Arc::new(Route {
            channel: tx,
            stats,
            failed: Mutex::default(),
        }));
        route.stats.routes.send_modify(|n| *n += 1);
        route.stats.tick.notify_one();
        Self {
            route: route.clone(),
            incoming,
            stats_incoming,
            pending: Pending::new(),
            outgoing_stats: false,
            receivers: JoinSet::new(),
            stats_receivers: JoinSet::new(),
        }
    }

    // The watch loop owns the socket writer. Completed receipts get the first
    // available slot, including while unrelated peers are pushing new batches.
    pub(super) async fn next_send(&mut self) -> Result<Vec<u8>> {
        loop {
            self.pending.retain(|_, request| !request.reply.is_closed());
            let (request, stats) = tokio::select! {
                biased;
                reply = self.receivers.join_next(), if !self.receivers.is_empty() => {
                    self.outgoing_stats = false;
                    return reply.context("peer receiver stopped")??.encode();
                }
                reply = self.stats_receivers.join_next(), if !self.stats_receivers.is_empty() => {
                    self.outgoing_stats = true;
                    return reply.context("stats receiver stopped")??.encode();
                }
                request = self.incoming.recv() => {
                    let request = request.context("peer watch closed")?;
                    if request.reply.is_closed() || self.pending.len() >= CONCURRENT {
                        continue;
                    }
                    (request, false)
                }
                request = self.stats_incoming.recv(), if self.pending.is_empty() => {
                    let stats = request.context("statistics watch closed")?;
                    if stats.dispatched.send(()).is_err() {
                        continue;
                    }
                    (stats.request, true)
                }
                // A caller may abandon an outstanding ordinary transfer without
                // a receipt. Recheck the queue as soon as that slot is released.
                _ = async {
                    futures_util::future::select_all(self.pending.values_mut().map(|request| Box::pin(request.reply.closed()))).await;
                }, if !self.pending.is_empty() => continue,
            };
            let id = Uuid::new_v4();
            let frame = Frame {
                kind: Kind::Push,
                peer: request.peer,
                id,
                payload: &request.payload,
            }
            .encode()?;
            self.pending.insert(
                id,
                PendingRequest {
                    peer: request.peer,
                    reply: request.reply,
                    stats,
                },
            );
            self.outgoing_stats = stats;
            return Ok(frame);
        }
    }

    pub(super) fn received(&mut self, bytes: &[u8], store: &SharedStore) -> Result<()> {
        let frame = Frame::decode(bytes)?;
        // Unavailable responses have no payload. Attribute those by request ID
        // and authenticated route, including responses generated by the relay.
        let stats = frame.payload.starts_with(crate::encrypted::stats::MAGIC)
            || (frame.kind == Kind::Unavailable
                && self
                    .pending
                    .get(&frame.id)
                    .is_some_and(|r| r.stats && r.peer == frame.peer));
        if stats {
            self.route
                .metrics
                .stats_received_frames
                .fetch_add(1, Ordering::Relaxed);
            self.route
                .metrics
                .stats_received_bytes
                .fetch_add(bytes.len() as u64, Ordering::Relaxed);
        }
        self.route
            .metrics
            .received_bytes
            .fetch_add(bytes.len() as u64, Ordering::Relaxed);
        match frame.kind {
            Kind::Push => {
                let store = store.clone();
                let peer = frame.peer;
                let id = frame.id;
                if frame.payload.starts_with(crate::encrypted::stats::MAGIC) {
                    // An optional or oversized push cannot consume messaging's
                    // receiver slots or tear down the shared watch.
                    if !self.stats_receivers.is_empty()
                        || frame.payload.len() > crate::encrypted::stats::MAX_WIRE_BYTES
                    {
                        return Ok(());
                    }
                    let payload = frame.payload.to_vec();
                    self.stats_receivers.spawn(async move {
                        let receipt = local(store, move |s| s.accept_stats_send(peer, &payload))
                            .await
                            .ok();
                        Reply { peer, id, receipt }
                    });
                } else {
                    ensure!(
                        self.receivers.len() < CONCURRENT,
                        "peer watch receiver busy"
                    );
                    let payload = frame.payload.to_vec();
                    self.receivers.spawn(async move {
                        let receipt =
                            session::accept(store, session::Peer::Node(peer.to_string()), &payload)
                                .await
                                .ok();
                        Reply { peer, id, receipt }
                    });
                }
            }
            Kind::Receipt | Kind::Unavailable => {
                if self
                    .pending
                    .get(&frame.id)
                    .is_some_and(|r| r.peer == frame.peer)
                    && let Some(request) = self.pending.remove(&frame.id)
                    && frame.kind == Kind::Receipt
                {
                    // The session layer verifies the signed stream, reader,
                    // history and sequence before it can retire any ciphertext.
                    let _ = request.reply.send(frame.payload.to_vec());
                }
            }
        }
        Ok(())
    }

    pub(super) fn sent(&self, bytes: usize) {
        if self.outgoing_stats {
            self.route
                .metrics
                .stats_sent_frames
                .fetch_add(1, Ordering::Relaxed);
            self.route
                .metrics
                .stats_sent_bytes
                .fetch_add(bytes as u64, Ordering::Relaxed);
        }
        self.route
            .metrics
            .sent_bytes
            .fetch_add(bytes as u64, Ordering::Relaxed);
    }
}

impl Drop for Connection {
    fn drop(&mut self) {
        self.route
            .channel
            .lock()
            .expect("peer watch lock poisoned")
            .take();
        self.route.stats.routes.send_modify(|n| *n -= 1);
    }
}

#[cfg(test)]
pub(in crate::sync) mod tests;