link-cli 3.1.1

A CLI tool and reusable library for links manipulation backed by a LiNo-notation doublet storage engine.
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
//! The reliable datagram session of `udp`, `mq`, `sysvmq`, `mailslot`,
//! `copydata` and `alpc` (see the "Reliable datagram session" section of
//! `docs/protocol/ipc-transports-v1.md`).

use super::{broken_pipe, hung_up, timed_out, trace, Transport};
use std::collections::VecDeque;
use std::fmt;
use std::io;
use std::sync::{Arc, Condvar, Mutex, MutexGuard};
use std::thread;
use std::time::Duration;
use web_time::Instant;

pub(crate) const HEADER: usize = 8;
pub(crate) const DATA: u8 = 1;
pub(crate) const ACK: u8 = 2;
pub(crate) const EOF: u8 = 3;
pub(crate) const PING: u8 = 4;
pub(crate) const ABORT: u8 = 5;
pub(crate) const HELLO: u8 = 6;
pub(crate) const WELCOME: u8 = 7;

/// At most this many received bytes wait to be read.
const WINDOW: usize = 1 << 20;
const MAX_RTO: Duration = Duration::from_millis(1000);
const TICK: Duration = Duration::from_millis(100);
/// How long hanging up waits for the end-of-stream frame to be acknowledged.
const LINGER: Duration = Duration::from_millis(1000);

/// Builds one frame.
pub(crate) fn frame(kind: u8, seq: u32, payload: &[u8]) -> Vec<u8> {
    let mut frame = Vec::with_capacity(HEADER + payload.len());
    frame.extend_from_slice(&[kind, 0, 0, 0]);
    frame.extend_from_slice(&seq.to_le_bytes());
    frame.extend_from_slice(payload);
    frame
}

/// Splits a frame into its type, sequence number and payload.
pub(crate) fn parse(frame: &[u8]) -> Option<(u8, u32, &[u8])> {
    if frame.len() < HEADER {
        return None;
    }
    let seq = u32::from_le_bytes(frame[4..8].try_into().expect("four bytes"));
    Some((frame[0], seq, &frame[HEADER..]))
}

/// A mechanism that moves whole frames, possibly losing some.
pub(crate) trait Datagrams: Send + Sync + fmt::Debug {
    /// Sends one frame; `Ok(false)` when it was dropped (a full queue).
    fn send(&self, frame: &[u8]) -> io::Result<bool>;
    /// Receives one frame into `buf`, waiting up to `timeout`; `Ok(false)`
    /// when none arrived in time.
    fn recv(&self, buf: &mut Vec<u8>, timeout: Duration) -> io::Result<bool>;
    /// Whether the peer may still answer (process liveness for local kinds).
    fn peer_alive(&self) -> bool {
        true
    }
    /// Releases the mechanism; called once, after the session stopped.
    fn close(&self) {}
}

/// Session settings.
#[derive(Clone, Copy, Debug)]
pub(crate) struct Settings {
    pub(crate) chunk: usize,
    pub(crate) rto: Duration,
    /// Send `PING` after this much silence (`udp`).
    pub(crate) heartbeat: Option<Duration>,
    /// The peer is gone after this much silence (`udp`).
    pub(crate) dead: Option<Duration>,
}

/// The settings of a local datagram kind (`mq`, `sysvmq`, `mailslot`,
/// `copydata`, `alpc`): no heartbeat, the peer's liveness is its process.
#[cfg_attr(not(any(unix, windows)), allow(dead_code))] // used by the OS-specific kinds
pub(crate) fn local_settings(endpoint: &super::Endpoint, chunk: usize) -> io::Result<Settings> {
    Ok(Settings {
        chunk,
        rto: endpoint.millis("rto", 20)?,
        heartbeat: None,
        dead: None,
    })
}

#[derive(Debug)]
struct State {
    inbound: VecDeque<u8>,
    /// The last accepted incoming sequence number.
    received: u32,
    /// The incoming direction ended.
    ended: bool,
    /// The peer hung up or is gone.
    aborted: bool,
    /// The last outgoing sequence number used.
    sent: u32,
    /// The highest acknowledged outgoing sequence number.
    acknowledged: u32,
    /// The outgoing direction ended.
    write_closed: bool,
    /// This side hung up.
    closed: bool,
    /// The receiver thread should stop.
    stopping: bool,
    last_heard: Instant,
    last_sent: Instant,
}

#[derive(Debug)]
struct Shared {
    kind: &'static str,
    link: Arc<dyn Datagrams>,
    settings: Settings,
    state: Mutex<State>,
    changed: Condvar,
}

impl Shared {
    fn lock(&self) -> MutexGuard<'_, State> {
        self.state
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
    }

    fn send(&self, frame: &[u8]) -> io::Result<bool> {
        let sent = self.link.send(frame);
        self.lock().last_sent = Instant::now();
        sent
    }

    fn receive_loop(self: Arc<Self>) {
        let mut buffer = Vec::with_capacity(self.settings.chunk + HEADER);
        let tick = match self.settings.heartbeat {
            Some(heartbeat) => TICK.min(heartbeat / 2).max(Duration::from_millis(1)),
            None => TICK,
        };
        let mut last_check = Instant::now();
        loop {
            if self.lock().stopping {
                break;
            }
            match self.link.recv(&mut buffer, tick) {
                Ok(true) => self.handle(&buffer),
                Ok(false) => {}
                Err(error) => {
                    trace!("{} receive failed: {error}", self.kind);
                    self.hang_up();
                    thread::sleep(tick);
                }
            }
            let now = Instant::now();
            if now.duration_since(last_check) >= tick {
                last_check = now;
                self.housekeeping(now);
            }
        }
        self.link.close();
        trace!("{} session stopped", self.kind);
    }

    fn handle(&self, frame: &[u8]) {
        let Some((kind, seq, payload)) = parse(frame) else {
            return;
        };
        let mut deliver = false;
        let reply = {
            let mut state = self.lock();
            state.last_heard = Instant::now();
            match kind {
                DATA | EOF => {
                    if seq == state.received.wrapping_add(1)
                        && !state.ended
                        && state.inbound.len() <= WINDOW
                        && payload.len() <= self.settings.chunk
                    {
                        // Only this thread handles frames, so the frame is delivered below, after its
                        // acknowledgement.
                        state.received = seq;
                        deliver = true;
                        Some(state.received)
                    } else if seq.wrapping_sub(state.received) > u32::MAX / 2
                        || seq == state.received
                    {
                        // A retransmission of something already accepted.
                        Some(state.received)
                    } else {
                        None
                    }
                }
                ACK => {
                    if seq.wrapping_sub(state.acknowledged) <= u32::MAX / 2 {
                        state.acknowledged = seq;
                        self.changed.notify_all();
                    }
                    None
                }
                ABORT => {
                    if !state.aborted {
                        trace!("{} peer hung up", self.kind);
                    }
                    state.aborted = true;
                    self.changed.notify_all();
                    None
                }
                _ => None,
            }
        };
        if let Some(seq) = reply {
            let _ = self.send(&frame_ack(seq));
        }
        if deliver {
            // The reader sees the frame only once it is acknowledged: a reader that hangs up as soon as it sees the
            // end must not abort the peer before the peer learns its end arrived.
            let mut state = self.lock();
            if kind == EOF {
                state.ended = true;
            } else {
                state.inbound.extend(payload);
            }
            self.changed.notify_all();
        }
    }

    fn housekeeping(&self, now: Instant) {
        let (ping, silent) = {
            let state = self.lock();
            if state.aborted || state.stopping {
                return;
            }
            let ping = self
                .settings
                .heartbeat
                .is_some_and(|heartbeat| now.duration_since(state.last_sent) >= heartbeat);
            let silent = self
                .settings
                .dead
                .is_some_and(|dead| now.duration_since(state.last_heard) >= dead);
            (ping, silent)
        };
        if silent || !self.link.peer_alive() {
            trace!("{} peer is gone", self.kind);
            self.hang_up();
        } else if ping {
            let _ = self.send(&frame(PING, 0, &[]));
        }
    }

    fn hang_up(&self) {
        let mut state = self.lock();
        state.aborted = true;
        self.changed.notify_all();
    }

    /// Sends a DATA or EOF frame and waits for its acknowledgement.
    fn send_reliably(&self, kind: u8, payload: &[u8], linger: Option<Instant>) -> io::Result<()> {
        let seq = {
            let mut state = self.lock();
            state.sent = state.sent.wrapping_add(1);
            state.sent
        };
        let frame = frame(kind, seq, payload);
        let mut rto = self.settings.rto.max(Duration::from_millis(1));
        loop {
            match self.send(&frame) {
                // The link found the peer gone while (re)sending the end: a System V
                // peer that read the end and hung up removes both queues, its ACK and
                // ABORT included. That is a hang-up, which ends the stream below.
                Err(error) if kind == EOF && error.kind() == io::ErrorKind::ConnectionReset => {
                    trace!(
                        "{} link found the peer gone while sending the end",
                        self.kind
                    );
                    self.hang_up();
                }
                result => {
                    result?;
                }
            }
            let deadline = Instant::now() + rto;
            let mut state = self.lock();
            loop {
                if state.acknowledged.wrapping_sub(seq) <= u32::MAX / 2 {
                    return Ok(());
                }
                if state.aborted {
                    if kind == EOF {
                        // Every DATA frame before the end was acknowledged, so nothing is lost: a peer that
                        // hung up no longer reads, and its acknowledgement of the end may be the frame lost.
                        trace!("{} peer hung up before acknowledging the end", self.kind);
                        return Ok(());
                    }
                    return Err(broken_pipe());
                }
                if state.closed && linger.is_none() {
                    return Err(io::Error::new(
                        io::ErrorKind::BrokenPipe,
                        "the connection was closed",
                    ));
                }
                let now = Instant::now();
                if linger.is_some_and(|linger| now >= linger) {
                    return Err(timed_out());
                }
                if now >= deadline {
                    break;
                }
                state = self
                    .changed
                    .wait_timeout(state, deadline - now)
                    .unwrap_or_else(|poisoned| poisoned.into_inner())
                    .0;
            }
            drop(state);
            rto = (rto * 2).min(MAX_RTO);
        }
    }
}

fn frame_ack(seq: u32) -> Vec<u8> {
    frame(ACK, seq, &[])
}

/// A reliable byte stream over [`Datagrams`].
pub(crate) struct Session {
    shared: Arc<Shared>,
    sending: Mutex<()>,
}

impl fmt::Debug for Session {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("Session")
            .field("kind", &self.shared.kind)
            .field("link", &self.shared.link)
            .field("settings", &self.shared.settings)
            .finish()
    }
}

impl Session {
    pub(crate) fn start(
        kind: &'static str,
        link: Arc<dyn Datagrams>,
        settings: Settings,
    ) -> io::Result<Self> {
        let now = Instant::now();
        let shared = Arc::new(Shared {
            kind,
            link,
            settings,
            state: Mutex::new(State {
                inbound: VecDeque::new(),
                received: 0,
                ended: false,
                aborted: false,
                sent: 0,
                acknowledged: 0,
                write_closed: false,
                closed: false,
                stopping: false,
                last_heard: now,
                last_sent: now,
            }),
            changed: Condvar::new(),
        });
        let receiver = Arc::clone(&shared);
        thread::Builder::new()
            .name(format!("clink-{kind}-session"))
            .spawn(move || receiver.receive_loop())?;
        trace!("{kind} session started, chunk {}", settings.chunk);
        Ok(Self {
            shared,
            sending: Mutex::new(()),
        })
    }

    fn sending(&self) -> MutexGuard<'_, ()> {
        self.sending
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
    }
}

impl Transport for Session {
    fn read(&self, buf: &mut [u8], timeout: Option<Duration>) -> io::Result<usize> {
        let deadline = timeout.map(|timeout| Instant::now() + timeout);
        let mut state = self.shared.lock();
        loop {
            if !state.inbound.is_empty() {
                let count = buf.len().min(state.inbound.len());
                for (target, byte) in buf.iter_mut().zip(state.inbound.drain(..count)) {
                    *target = byte;
                }
                return Ok(count);
            }
            if state.ended || state.closed {
                return Ok(0);
            }
            if state.aborted {
                return Err(hung_up());
            }
            let wait = match deadline {
                Some(deadline) => {
                    let now = Instant::now();
                    if now >= deadline {
                        return Err(timed_out());
                    }
                    deadline - now
                }
                None => TICK * 10,
            };
            state = self
                .shared
                .changed
                .wait_timeout(state, wait)
                .unwrap_or_else(|poisoned| poisoned.into_inner())
                .0;
        }
    }

    fn write(&self, buf: &[u8]) -> io::Result<usize> {
        let _sending = self.sending();
        {
            let state = self.shared.lock();
            if state.write_closed || state.closed {
                return Err(io::Error::new(
                    io::ErrorKind::BrokenPipe,
                    "the outgoing direction was closed",
                ));
            }
            if state.aborted {
                return Err(broken_pipe());
            }
        }
        let count = buf.len().min(self.shared.settings.chunk);
        self.shared.send_reliably(DATA, &buf[..count], None)?;
        Ok(count)
    }

    fn close_write(&self) -> io::Result<()> {
        let _sending = self.sending();
        {
            let mut state = self.shared.lock();
            if state.write_closed || state.aborted || state.closed {
                return Ok(());
            }
            state.write_closed = true;
        }
        self.shared.send_reliably(EOF, &[], None)
    }

    fn shutdown(&self) {
        {
            let mut state = self.shared.lock();
            if state.closed {
                return;
            }
            state.closed = true;
            self.shared.changed.notify_all();
        }
        trace!("{} session hanging up", self.shared.kind);
        // Waits for a write in progress, which now fails quickly.
        let _sending = self.sending();
        let end = {
            let mut state = self.shared.lock();
            let end = !state.write_closed && !state.aborted;
            state.write_closed = true;
            end
        };
        if end {
            let _ = self
                .shared
                .send_reliably(EOF, &[], Some(Instant::now() + LINGER));
        }
        let aborted = self.shared.lock().aborted;
        if !aborted {
            for _ in 0..3 {
                let _ = self.shared.send(&frame(ABORT, 0, &[]));
            }
        }
        let mut state = self.shared.lock();
        state.stopping = true;
        self.shared.changed.notify_all();
    }

    fn kind(&self) -> &'static str {
        self.shared.kind
    }
}

impl Drop for Session {
    fn drop(&mut self) {
        self.shutdown();
    }
}

/// The chunk both sides use: the smaller proposal.
pub(crate) fn agree_chunk(own: u64, peer: Option<u64>) -> usize {
    peer.map_or(own, |peer| own.min(peer)).clamp(16, 65000) as usize
}

/// A pair of in-process queues, for tests of the session itself.
#[cfg(test)]
pub(crate) mod memory {
    use super::*;
    use std::sync::mpsc;

    #[derive(Debug)]
    pub(crate) struct MemoryLink {
        outgoing: Mutex<mpsc::Sender<Vec<u8>>>,
        incoming: Mutex<mpsc::Receiver<Vec<u8>>>,
        /// Drops every n-th outgoing frame when non-zero.
        pub(crate) lose_every: u32,
        counter: std::sync::atomic::AtomicU32,
    }

    pub(crate) fn pair(lose_every: u32) -> (MemoryLink, MemoryLink) {
        let (a_out, b_in) = mpsc::channel();
        let (b_out, a_in) = mpsc::channel();
        let link = |outgoing, incoming| MemoryLink {
            outgoing: Mutex::new(outgoing),
            incoming: Mutex::new(incoming),
            lose_every,
            counter: Default::default(),
        };
        (link(a_out, a_in), link(b_out, b_in))
    }

    impl Datagrams for MemoryLink {
        fn send(&self, frame: &[u8]) -> io::Result<bool> {
            let count = self
                .counter
                .fetch_add(1, std::sync::atomic::Ordering::Relaxed)
                + 1;
            if self.lose_every != 0 && count.is_multiple_of(self.lose_every) {
                return Ok(false);
            }
            Ok(self.outgoing.lock().unwrap().send(frame.to_vec()).is_ok())
        }

        fn recv(&self, buf: &mut Vec<u8>, timeout: Duration) -> io::Result<bool> {
            match self.incoming.lock().unwrap().recv_timeout(timeout) {
                Ok(frame) => {
                    buf.clear();
                    buf.extend_from_slice(&frame);
                    Ok(true)
                }
                Err(_) => Ok(false),
            }
        }
    }
}