weida 0.1.0-alpha.11

QUIC-native messaging framework: runtime, native QUIC transport, Req/Rep, Push/Pull, Pub/Sub, PAIR, SURVEY and BUS
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
//! Per-write chunk framing for the local stream transports.
//!
//! A QUIC stream ends two ways and says which: a FIN the reader sees as the
//! end of the payload, and a `RESET_STREAM` carrying a code the reader sees as
//! a failure. Neither local transport gets both from the kernel:
//!
//! * a **named pipe** has no half-close at all — closing the handle would take
//!   the reply direction with it;
//! * an **`AF_UNIX` socket** has a half-close, and that is the whole problem:
//!   a shutdown and an abort are byte-for-byte the same thing at the reader,
//!   because Linux has no kernel-level abort for a stream socket (`SO_LINGER`
//!   with a zero timeout plus `close` is a plain close at the reader).
//!
//! So the distinction lives in the payload, for both of them, in one framing:
//!
//! ```text
//! 0x00 + u32 length + bytes    payload, as much as one write produced
//! 0x01                          FIN: the payload is complete
//! 0x02 + u64 code               RESET: the payload was abandoned, with why
//! ```
//!
//! Five bytes per write and nothing per byte, and it carries what the socket
//! could not: **a cancelled transfer is `Canceled` at the reader rather than a
//! short payload that looks complete**. A transport that cannot signal an
//! abort would otherwise let an application read half a message as a whole
//! one, with nothing anywhere naming it.
//! [0012 §4.7(e)](../../../docs/decisions/0012-local-connection-grouping.md)
//! priced the alternative and this is the shape it chose; both socket
//! transports share it.
//!
//! What the framing still cannot carry is the **reader's** refusal: there is
//! no `STOP_SENDING`, because on a pipe the writer's direction is the only one
//! the reader could signal on and the reply already owns it, and on a socket
//! closing the read half gives the writer an `EPIPE` with no code. A reader
//! that stops therefore **drains** — the rest of the payload is read and
//! discarded in the background, so the writer finishes normally and learns of
//! the refusal from the reply. A drain gives up on a peer that goes quiet for
//! `DRAIN_IDLE`, which releases the stream; a payload that keeps flowing is
//! drained to its FIN however long it runs.
//!
//! A drain, and an end marker the stream did not take at once, run on a task
//! that owns the OS handle, outliving the half that started it. Each is given
//! what the caller counts the connection against (`hold`) and drops it after
//! the handle, so a binding's `max_connections` bounds the descriptors it
//! holds and not only the halves still in hand.

use std::pin::Pin;
use std::task::{Context, Poll, Waker, ready};
use std::time::Duration;

use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf};
use weida_runtime::Exec;

/// A payload chunk: `u32` little-endian length, then the bytes.
const CHUNK_DATA: u8 = 0x00;
/// The end of the payload.
const CHUNK_FIN: u8 = 0x01;
/// The payload abandoned, with a `u64` little-endian code.
const CHUNK_RESET: u8 = 0x02;
/// The longest chunk header: kind plus a reset code.
const HEADER_MAX: usize = 9;
/// The header of a payload chunk: kind plus a `u32` length.
const HEADER_DATA: usize = 5;
/// Read-ahead of a chunk reader. One `read` serves a short message's header,
/// body and FIN, and a bounded buffer keeps a flood from pinning memory.
const READ_AHEAD: usize = 4 * 1024;
/// Scratch for a draining reader.
const DRAIN_BUF: usize = 8 * 1024;
/// How long a draining reader waits for the peer's next bytes before it
/// gives the stream up. The bound is per read, so a flowing payload drains
/// to its FIN however long it is.
const DRAIN_IDLE: Duration = Duration::from_secs(30);
/// Errors for a stream that ends inside a header and inside a chunk.
const HEADER_EOF: &str = "the local stream closed before the end of the payload";
const BODY_EOF: &str = "the local stream closed in the middle of a chunk";

/// The peer abandoned its payload, with the code it gave.
///
/// Carried inside an `io::Error` so that a transport's `read_error` can
/// recover the code and report `Canceled` rather than a generic failure.
#[derive(Debug)]
pub(crate) struct PeerReset(pub(crate) u64);

impl std::fmt::Display for PeerReset {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "stream reset by the peer with code {}", self.0)
    }
}

impl std::error::Error for PeerReset {}

/// How a stream ended.
pub(crate) enum Marker {
    Fin,
    Reset(u64),
}

impl Marker {
    fn encode(&self) -> ([u8; HEADER_MAX], usize) {
        let mut bytes = [0u8; HEADER_MAX];
        match self {
            Marker::Fin => {
                bytes[0] = CHUNK_FIN;
                (bytes, 1)
            }
            Marker::Reset(code) => {
                bytes[0] = CHUNK_RESET;
                bytes[1..].copy_from_slice(&code.to_le_bytes());
                (bytes, HEADER_MAX)
            }
        }
    }
}

fn closed() -> std::io::Error {
    std::io::Error::new(std::io::ErrorKind::BrokenPipe, "stream already closed")
}

/// The writing half: frames every write as a chunk.
pub(crate) struct ChunkWriter<W> {
    io: Option<W>,
    exec: Exec,
    /// The chunk header being written, and how much of it is still owed.
    header: [u8; HEADER_MAX],
    header_len: usize,
    header_written: usize,
    /// Payload bytes the current chunk's header promised and that have not
    /// been written yet.
    body_left: usize,
    /// A FIN went out through `poll_shutdown`; `end` has nothing to add.
    finished: bool,
}

impl<W: AsyncWrite + Unpin + Send + 'static> ChunkWriter<W> {
    pub(crate) fn new(io: W, exec: Exec) -> ChunkWriter<W> {
        ChunkWriter {
            io: Some(io),
            exec,
            header: [0; HEADER_MAX],
            header_len: 0,
            header_written: 0,
            body_left: 0,
            finished: false,
        }
    }

    /// Writes the end-of-stream marker: in place when the stream takes all of
    /// it at once, and on the runtime for whatever it did not take.
    ///
    /// The callers of `finish` and `reset` are synchronous, so the task is the
    /// fallback, not the path. It owns the stream until the peer has read the
    /// rest of the marker, which a peer that never reads makes for ever, and
    /// it is why `hold` is here: it is dropped after the stream, so what it
    /// counts is not given back while the descriptor is still open.
    ///
    /// A chunk left half-written cannot be ended cleanly — the reader is owed
    /// bytes this side no longer has — so nothing is written and the reader
    /// sees the connection close when the last half goes.
    pub(crate) fn end<H: Send + 'static>(mut self, marker: Marker, hold: H) {
        let Some(mut io) = self.io.take() else {
            return;
        };
        if self.finished || self.body_left > 0 || self.header_written != self.header_len {
            return;
        }
        let (bytes, len) = marker.encode();
        // A waker that is never woken: a stream that answers `Pending` here is
        // finished by the task below, which polls with a real waker.
        let mut cx = Context::from_waker(Waker::noop());
        let mut written = 0;
        while written < len {
            match Pin::new(&mut io).poll_write(&mut cx, &bytes[written..len]) {
                Poll::Ready(Ok(n)) if n > 0 => written += n,
                Poll::Ready(Err(e)) => {
                    tracing::debug!(error = %e, "local stream end marker not written");
                    return;
                }
                // Pending, or a write that took nothing: the task reports it.
                Poll::Ready(Ok(_)) | Poll::Pending => break,
            }
        }
        if written == len {
            match Pin::new(&mut io).poll_flush(&mut cx) {
                Poll::Ready(Ok(())) => return,
                Poll::Ready(Err(e)) => {
                    tracing::debug!(error = %e, "local stream end marker not flushed");
                    return;
                }
                Poll::Pending => {}
            }
        }
        self.exec.spawn(async move {
            if let Err(e) = io.write_all(&bytes[written..len]).await {
                tracing::debug!(error = %e, "local stream end marker not written");
            } else if let Err(e) = io.flush().await {
                tracing::debug!(error = %e, "local stream end marker not flushed");
            }
            drop(io);
            drop(hold);
        });
    }

    /// Writes what is owed of the current header; `Ready(Ok(()))` when none
    /// is.
    fn poll_header(&mut self, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
        let Some(io) = self.io.as_mut() else {
            return Poll::Ready(Err(closed()));
        };
        while self.header_written < self.header_len {
            let n = ready!(
                Pin::new(&mut *io)
                    .poll_write(cx, &self.header[self.header_written..self.header_len])
            )?;
            if n == 0 {
                return Poll::Ready(Err(std::io::Error::new(
                    std::io::ErrorKind::WriteZero,
                    "the local stream accepted no bytes",
                )));
            }
            self.header_written += n;
        }
        Poll::Ready(Ok(()))
    }

    fn poll_flush_inner(&mut self, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
        match self.io.as_mut() {
            Some(io) => Pin::new(io).poll_flush(cx),
            None => Poll::Ready(Ok(())),
        }
    }
}

impl<W: AsyncWrite + Unpin + Send + 'static> AsyncWrite for ChunkWriter<W> {
    fn poll_write(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<std::io::Result<usize>> {
        let this = self.get_mut();
        if this.io.is_none() || this.finished {
            return Poll::Ready(Err(closed()));
        }
        if buf.is_empty() {
            return Poll::Ready(Ok(0));
        }
        if this.body_left == 0 && this.header_written == this.header_len {
            // A new chunk: as long as this write, capped by the length field.
            let len = buf.len().min(u32::MAX as usize);
            this.header[0] = CHUNK_DATA;
            this.header[1..HEADER_DATA].copy_from_slice(&(len as u32).to_le_bytes());
            this.header_len = HEADER_DATA;
            this.header_written = 0;
            this.body_left = len;
        }
        ready!(this.poll_header(cx))?;
        let want = buf.len().min(this.body_left);
        let io = this.io.as_mut().expect("checked above");
        let n = ready!(Pin::new(io).poll_write(cx, &buf[..want]))?;
        this.body_left -= n;
        Poll::Ready(Ok(n))
    }

    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
        self.get_mut().poll_flush_inner(cx)
    }

    /// A shutdown is a FIN, as it is on every other transport, written in
    /// place through the polls rather than on the runtime.
    fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
        let this = self.get_mut();
        if this.io.is_none() {
            return Poll::Ready(Ok(()));
        }
        if this.finished {
            return this.poll_flush_inner(cx);
        }
        if this.body_left > 0 {
            return Poll::Ready(Err(std::io::Error::other(
                "shutdown in the middle of a write",
            )));
        }
        if this.header_written == this.header_len {
            let (bytes, len) = Marker::Fin.encode();
            this.header = bytes;
            this.header_len = len;
            this.header_written = 0;
        }
        ready!(this.poll_header(cx))?;
        this.finished = true;
        this.poll_flush_inner(cx)
    }
}

/// Where the reader is in the framing.
enum ReadState {
    /// At a chunk header; its first byte says how long the header is.
    Header,
    /// Inside a payload chunk, with this many bytes still to come.
    Body { left: usize },
    /// FIN seen, RESET reported, or the connection failed: nothing more to
    /// read.
    Ended,
}

/// What a completed header means.
enum Chunk {
    Data(usize),
    Fin,
    Reset(u64),
}

/// The reading half: unframes chunks, ends at FIN, fails at RESET.
///
/// Reads from the stream go through a read-ahead buffer, so one `read` serves
/// a short chunk's header, body and FIN together. Bytes read ahead are part of
/// the payload: every read, and `drain`, consumes them before the stream.
pub(crate) struct ChunkReader<R> {
    io: Option<R>,
    exec: Exec,
    state: ReadState,
    /// Bytes read from the stream and not consumed yet: `rbuf[rpos..rfill]`.
    rbuf: Box<[u8]>,
    rpos: usize,
    rfill: usize,
}

impl<R: AsyncRead + Unpin + Send + 'static> ChunkReader<R> {
    pub(crate) fn new(io: R, exec: Exec) -> ChunkReader<R> {
        ChunkReader {
            io: Some(io),
            exec,
            state: ReadState::Header,
            rbuf: vec![0; READ_AHEAD].into_boxed_slice(),
            rpos: 0,
            rfill: 0,
        }
    }

    /// Reads and discards the rest of the payload on the runtime, so the
    /// peer's writes complete instead of blocking on a stream nobody reads.
    ///
    /// Each read waits at most `DRAIN_IDLE` for the peer's next bytes: a peer
    /// that stalls releases the descriptor, and a payload that keeps flowing
    /// drains to its FIN however long it is. `hold` is dropped after the
    /// descriptor, whenever the drain ends.
    pub(crate) fn drain<H: Send + 'static>(self, hold: H) {
        self.drain_within(DRAIN_IDLE, hold);
    }

    fn drain_within<H: Send + 'static>(mut self, idle: Duration, hold: H) {
        if matches!(self.state, ReadState::Ended) || self.io.is_none() {
            // Nothing left to read: the handle goes first, and `hold` as this
            // returns.
            drop(self);
            return;
        }
        let exec = self.exec.clone();
        let timer = self.exec.clone();
        exec.spawn(async move {
            let mut scratch = vec![0u8; DRAIN_BUF];
            loop {
                match timer.within(idle, self.read(&mut scratch)).await {
                    Some(Ok(n)) if n > 0 => {}
                    // The FIN or the end of the stream, a failure, or a peer
                    // that went quiet: in every case the reader is let go.
                    _ => break,
                }
            }
            drop(self);
            drop(hold);
        });
    }

    /// How many header bytes a chunk of `kind` has, or `None` for a kind this
    /// side does not know.
    fn header_len(kind: u8) -> Option<usize> {
        match kind {
            CHUNK_DATA => Some(HEADER_DATA),
            CHUNK_FIN => Some(1),
            CHUNK_RESET => Some(HEADER_MAX),
            _ => None,
        }
    }

    fn buffered(&self) -> usize {
        self.rfill - self.rpos
    }

    /// Reads more of the stream into the read-ahead buffer. Unconsumed bytes
    /// move to the front first, so the free space is at the back.
    fn poll_fill(&mut self, cx: &mut Context<'_>, eof: &'static str) -> Poll<std::io::Result<()>> {
        let Some(io) = self.io.as_mut() else {
            return Poll::Ready(Err(std::io::Error::new(
                std::io::ErrorKind::UnexpectedEof,
                eof,
            )));
        };
        if self.rpos > 0 {
            self.rbuf.copy_within(self.rpos..self.rfill, 0);
            self.rfill -= self.rpos;
            self.rpos = 0;
        }
        debug_assert!(self.rfill < self.rbuf.len(), "a fill needs free space");
        let mut room = ReadBuf::new(&mut self.rbuf[self.rfill..]);
        ready!(Pin::new(io).poll_read(cx, &mut room))?;
        let n = room.filled().len();
        if n == 0 {
            return Poll::Ready(Err(std::io::Error::new(
                std::io::ErrorKind::UnexpectedEof,
                eof,
            )));
        }
        self.rfill += n;
        Poll::Ready(Ok(()))
    }

    fn set_body_left(&mut self, left: usize) {
        self.state = if left == 0 {
            ReadState::Header
        } else {
            ReadState::Body { left }
        };
    }

    fn poll_read_inner(
        &mut self,
        cx: &mut Context<'_>,
        buf: &mut ReadBuf<'_>,
    ) -> Poll<std::io::Result<()>> {
        if self.io.is_none() {
            return Poll::Ready(Ok(()));
        }
        loop {
            match self.state {
                ReadState::Ended => return Poll::Ready(Ok(())),
                ReadState::Header => {
                    if self.buffered() == 0 {
                        ready!(self.poll_fill(cx, HEADER_EOF))?;
                        continue;
                    }
                    let kind = self.rbuf[self.rpos];
                    let Some(need) = ChunkReader::<R>::header_len(kind) else {
                        return Poll::Ready(Err(std::io::Error::new(
                            std::io::ErrorKind::InvalidData,
                            format!("unknown local chunk kind {kind:#04x}"),
                        )));
                    };
                    if self.buffered() < need {
                        ready!(self.poll_fill(cx, HEADER_EOF))?;
                        continue;
                    }
                    let header = &self.rbuf[self.rpos..self.rpos + need];
                    let chunk = match kind {
                        CHUNK_DATA => Chunk::Data(u32::from_le_bytes([
                            header[1], header[2], header[3], header[4],
                        ]) as usize),
                        CHUNK_FIN => Chunk::Fin,
                        _ => {
                            let mut code = [0u8; 8];
                            code.copy_from_slice(&header[1..HEADER_MAX]);
                            Chunk::Reset(u64::from_le_bytes(code))
                        }
                    };
                    self.rpos += need;
                    match chunk {
                        // A zero-length chunk carries no payload: the next
                        // header follows it directly.
                        Chunk::Data(0) => {}
                        Chunk::Data(len) => self.state = ReadState::Body { left: len },
                        Chunk::Fin => {
                            self.state = ReadState::Ended;
                            return Poll::Ready(Ok(()));
                        }
                        Chunk::Reset(code) => {
                            self.state = ReadState::Ended;
                            return Poll::Ready(Err(std::io::Error::other(PeerReset(code))));
                        }
                    }
                }
                ReadState::Body { left } => {
                    if buf.remaining() == 0 {
                        return Poll::Ready(Ok(()));
                    }
                    if self.buffered() > 0 {
                        let n = buf.remaining().min(left).min(self.buffered());
                        buf.put_slice(&self.rbuf[self.rpos..self.rpos + n]);
                        self.rpos += n;
                        self.set_body_left(left - n);
                        return Poll::Ready(Ok(()));
                    }
                    if buf.remaining() >= left.min(READ_AHEAD) {
                        // The caller's buffer takes the payload as it comes,
                        // with no copy through the read-ahead buffer. A small
                        // caller buffer would only mean more reads, so it is
                        // served from the read-ahead instead.
                        let want = buf.remaining().min(left);
                        let Some(io) = self.io.as_mut() else {
                            return Poll::Ready(Ok(()));
                        };
                        buf.initialize_unfilled_to(want);
                        let mut slice = buf.take(want);
                        ready!(Pin::new(io).poll_read(cx, &mut slice))?;
                        let n = slice.filled().len();
                        if n == 0 {
                            return Poll::Ready(Err(std::io::Error::new(
                                std::io::ErrorKind::UnexpectedEof,
                                BODY_EOF,
                            )));
                        }
                        buf.advance(n);
                        self.set_body_left(left - n);
                        return Poll::Ready(Ok(()));
                    }
                    ready!(self.poll_fill(cx, BODY_EOF))?;
                }
            }
        }
    }
}

impl<R: AsyncRead + Unpin + Send + 'static> AsyncRead for ChunkReader<R> {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut ReadBuf<'_>,
    ) -> Poll<std::io::Result<()>> {
        let this = self.get_mut();
        let polled = this.poll_read_inner(cx, buf);
        if let Poll::Ready(Err(_)) = &polled {
            // Any failure ends the stream: a drain must not spin on it.
            this.state = ReadState::Ended;
        }
        polled
    }
}

/// A read error as both socket transports report it: a peer's RESET by its
/// code, anything else as a transport failure.
pub(crate) fn read_error(error: std::io::Error) -> weida_core::Error {
    match error
        .get_ref()
        .and_then(|inner| inner.downcast_ref::<PeerReset>())
    {
        Some(reset) => weida_protocol::codes::stop_reason(reset.0).into(),
        None => weida_core::Error::Transport(format!("local stream read failed: {error}")),
    }
}

#[cfg(test)]
mod tests {
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::{Arc, Mutex};
    use std::time::Duration;

    use tokio::sync::oneshot;

    use super::*;

    /// The expected time of every await in these tests: past it a wait
    /// reports, it never fails (a paused clock panics there).
    const WAIT: Duration = Duration::from_secs(10);

    async fn within<F: Future>(what: &str, f: F) -> F::Output {
        weida_testkit::within(what, WAIT, f).await
    }

    const WIRE_CAP: usize = 64 * 1024;

    fn exec() -> Exec {
        Exec::current().expect("a tokio runtime")
    }

    fn data_chunk(body: &[u8]) -> Vec<u8> {
        let mut wire = vec![CHUNK_DATA];
        wire.extend_from_slice(&(body.len() as u32).to_le_bytes());
        wire.extend_from_slice(body);
        wire
    }

    fn payload(len: usize) -> Vec<u8> {
        (0..len).map(|i| (i * 31 % 251) as u8).collect()
    }

    /// Reads to the end of the reader with `size`-byte caller buffers.
    async fn read_all<R>(reader: &mut ChunkReader<R>, size: usize) -> std::io::Result<Vec<u8>>
    where
        R: AsyncRead + Unpin + Send + 'static,
    {
        let mut out = Vec::new();
        let mut buf = vec![0u8; size];
        loop {
            let n = reader.read(&mut buf).await?;
            if n == 0 {
                return Ok(out);
            }
            out.extend_from_slice(&buf[..n]);
        }
    }

    /// Feeds `wire` to a reader and then closes the stream.
    async fn read_wire(wire: &[u8]) -> std::io::Result<Vec<u8>> {
        let exec = exec();
        let (mut tx, rx) = tokio::io::duplex(WIRE_CAP);
        tx.write_all(wire).await.expect("write");
        drop(tx);
        let mut reader = ChunkReader::new(rx, exec.clone());
        within("the wire to be read", read_all(&mut reader, 64 * 1024)).await
    }

    /// Writes `payload` through a `ChunkWriter` in `piece`-byte writes and
    /// reads it back with `size`-byte caller buffers.
    async fn round_trip(payload: &[u8], piece: usize, size: usize) -> Vec<u8> {
        let exec = exec();
        let (tx, rx) = tokio::io::duplex(WIRE_CAP);
        let owned = payload.to_vec();
        let writer_exec = exec.clone();
        let writer = tokio::spawn(async move {
            let mut writer = ChunkWriter::new(tx, writer_exec);
            for part in owned.chunks(piece) {
                writer.write_all(part).await?;
            }
            writer.shutdown().await
        });
        let mut reader = ChunkReader::new(rx, exec.clone());
        let out = within("the round-trip read", read_all(&mut reader, size))
            .await
            .expect("read");
        within("the round-trip writer", writer)
            .await
            .expect("join")
            .expect("write");
        out
    }

    /// Counts the reads that reach the wrapped stream.
    struct Counting<T> {
        inner: T,
        reads: Arc<AtomicUsize>,
    }

    impl<T: AsyncRead + Unpin> AsyncRead for Counting<T> {
        fn poll_read(
            self: Pin<&mut Self>,
            cx: &mut Context<'_>,
            buf: &mut ReadBuf<'_>,
        ) -> Poll<std::io::Result<()>> {
            let this = self.get_mut();
            this.reads.fetch_add(1, Ordering::SeqCst);
            Pin::new(&mut this.inner).poll_read(cx, buf)
        }
    }

    #[tokio::test]
    async fn a_one_chunk_message_costs_at_most_two_reads() {
        let exec = exec();
        let mut wire = data_chunk(b"a short message");
        wire.push(CHUNK_FIN);
        let (mut tx, rx) = tokio::io::duplex(WIRE_CAP);
        tx.write_all(&wire).await.expect("write");
        let reads = Arc::new(AtomicUsize::new(0));
        let counted = Counting {
            inner: rx,
            reads: Arc::clone(&reads),
        };
        let mut reader = ChunkReader::new(counted, exec.clone());
        let out = within("a one-chunk read", read_all(&mut reader, 64 * 1024))
            .await
            .expect("read");
        assert_eq!(out, b"a short message");
        let reads = reads.load(Ordering::SeqCst);
        assert!(reads <= 2, "a one-chunk message took {reads} reads");
    }

    #[tokio::test]
    async fn payloads_decode_byte_identically() {
        for len in [0, 1, 4095, 4096, 4097, 1 << 20] {
            let sent = payload(len);
            let got = round_trip(&sent, 1000, 64 * 1024).await;
            assert!(got == sent, "a payload of {len} bytes changed in transit");
        }
    }

    #[tokio::test]
    async fn short_caller_reads_decode_byte_identically() {
        for (len, size) in [(4097, 3), (4096, 1), (1, 1)] {
            let sent = payload(len);
            let got = round_trip(&sent, 1000, size).await;
            assert!(
                got == sent,
                "a {len}-byte payload changed with {size}-byte reads"
            );
        }
    }

    #[tokio::test]
    async fn zero_length_chunks_are_skipped() {
        let mut wire = data_chunk(b"");
        wire.extend(data_chunk(b"abc"));
        wire.push(CHUNK_FIN);
        assert_eq!(read_wire(&wire).await.expect("read"), b"abc");
    }

    #[tokio::test]
    async fn an_unknown_chunk_kind_is_invalid_data() {
        let error = read_wire(&[0x7f]).await.expect_err("unknown kind");
        assert_eq!(error.kind(), std::io::ErrorKind::InvalidData);
    }

    #[tokio::test]
    async fn a_stream_closed_before_any_chunk_is_unexpected_eof() {
        let error = read_wire(&[]).await.expect_err("no chunk");
        assert_eq!(error.kind(), std::io::ErrorKind::UnexpectedEof);
    }

    #[tokio::test]
    async fn a_stream_closed_inside_a_header_is_unexpected_eof() {
        let error = read_wire(&[CHUNK_DATA, 5]).await.expect_err("short header");
        assert_eq!(error.kind(), std::io::ErrorKind::UnexpectedEof);
    }

    #[tokio::test]
    async fn a_stream_closed_inside_a_chunk_is_unexpected_eof() {
        let wire = data_chunk(b"0123456789");
        let error = read_wire(&wire[..7]).await.expect_err("short body");
        assert_eq!(error.kind(), std::io::ErrorKind::UnexpectedEof);
    }

    #[tokio::test]
    async fn a_reset_surfaces_the_peer_code() {
        let code = 0x0123_4567_89ab_cdef_u64;
        let mut wire = vec![CHUNK_RESET];
        wire.extend_from_slice(&code.to_le_bytes());
        let error = read_wire(&wire).await.expect_err("reset");
        let reset = error
            .get_ref()
            .and_then(|inner| inner.downcast_ref::<PeerReset>())
            .expect("a peer reset");
        assert_eq!(reset.0, code);
    }

    /// A sink that keeps what it accepts. It takes at most `limit` bytes per
    /// call and answers `Pending` once, when `stall_at` bytes are in.
    struct Sink {
        taken: Arc<Mutex<Vec<u8>>>,
        limit: usize,
        stall_at: Option<usize>,
    }

    impl AsyncWrite for Sink {
        fn poll_write(
            self: Pin<&mut Self>,
            cx: &mut Context<'_>,
            buf: &[u8],
        ) -> Poll<std::io::Result<usize>> {
            let this = self.get_mut();
            let mut taken = this.taken.lock().expect("sink");
            let len = taken.len();
            if this.stall_at == Some(len) {
                this.stall_at = None;
                cx.waker().wake_by_ref();
                return Poll::Pending;
            }
            let room = this.stall_at.map_or(usize::MAX, |at| at - len);
            let n = buf.len().min(this.limit).min(room);
            taken.extend_from_slice(&buf[..n]);
            Poll::Ready(Ok(n))
        }

        fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<std::io::Result<()>> {
            Poll::Ready(Ok(()))
        }

        fn poll_shutdown(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<std::io::Result<()>> {
            Poll::Ready(Ok(()))
        }
    }

    #[tokio::test]
    async fn end_writes_the_marker_without_a_task() {
        let taken = Arc::new(Mutex::new(Vec::new()));
        let sink = Sink {
            taken: Arc::clone(&taken),
            limit: usize::MAX,
            stall_at: None,
        };
        let mut writer = ChunkWriter::new(sink, exec());
        writer.write_all(b"abc").await.expect("write");
        writer.end(Marker::Fin, ());
        // Nothing awaits between `end` and this check, so a task spawned by
        // `end` cannot have run yet.
        let mut expected = data_chunk(b"abc");
        expected.push(CHUNK_FIN);
        assert_eq!(*taken.lock().expect("sink"), expected);
    }

    #[tokio::test]
    async fn end_writes_what_the_stream_did_not_take_on_a_task() {
        let taken = Arc::new(Mutex::new(Vec::new()));
        let sink = Sink {
            taken: Arc::clone(&taken),
            limit: 4,
            stall_at: Some(4),
        };
        let exec = exec();
        let writer = ChunkWriter::new(sink, exec.clone());
        let code = 0x0102_0304_0506_0708_u64;
        writer.end(Marker::Reset(code), ());
        assert_eq!(taken.lock().expect("sink").len(), 4);
        within("the remainder of the marker to be written", async {
            while taken.lock().expect("sink").len() < 9 {
                tokio::task::yield_now().await;
            }
        })
        .await;
        let mut expected = vec![CHUNK_RESET];
        expected.extend_from_slice(&code.to_le_bytes());
        assert_eq!(*taken.lock().expect("sink"), expected);
    }

    /// Reports the bytes it hands out and, on drop, how many that was.
    struct Watched<T> {
        inner: T,
        delivered: usize,
        released: Option<oneshot::Sender<usize>>,
    }

    impl<T: AsyncRead + Unpin> AsyncRead for Watched<T> {
        fn poll_read(
            self: Pin<&mut Self>,
            cx: &mut Context<'_>,
            buf: &mut ReadBuf<'_>,
        ) -> Poll<std::io::Result<()>> {
            let this = self.get_mut();
            let before = buf.filled().len();
            let polled = Pin::new(&mut this.inner).poll_read(cx, buf);
            this.delivered += buf.filled().len() - before;
            polled
        }
    }

    impl<T> Drop for Watched<T> {
        fn drop(&mut self) {
            if let Some(released) = self.released.take() {
                let _ = released.send(self.delivered);
            }
        }
    }

    #[tokio::test(start_paused = true)]
    async fn a_stalled_drain_releases_its_reader() {
        let exec = exec();
        let (mut tx, rx) = tokio::io::duplex(WIRE_CAP);
        // A payload that promises ten bytes and delivers three.
        tx.write_all(&[CHUNK_DATA, 10, 0, 0, 0, 1, 2, 3])
            .await
            .expect("write");
        let (released, dropped) = oneshot::channel();
        let reader = ChunkReader::new(
            Watched {
                inner: rx,
                delivered: 0,
                released: Some(released),
            },
            exec.clone(),
        );
        reader.drain_within(Duration::from_millis(50), ());
        let delivered = within("the drain to give up on the stalled peer", dropped)
            .await
            .expect("the reader was dropped");
        assert_eq!(delivered, 8);
        drop(tx);
    }

    #[tokio::test(start_paused = true)]
    async fn a_payload_still_flowing_drains_to_its_fin() {
        const PIECES: usize = 20;
        const PIECE: usize = 100;
        let exec = exec();
        let (mut tx, rx) = tokio::io::duplex(WIRE_CAP);
        let pacing = exec.clone();
        // Each gap is well under the idle bound, and the whole flow is well
        // over it: only a per-read bound may release the reader.
        let writer = tokio::spawn(async move {
            for _ in 0..PIECES {
                tx.write_all(&data_chunk(&[7u8; PIECE])).await?;
                pacing.sleep(Duration::from_millis(25)).await;
            }
            tx.write_all(&[CHUNK_FIN]).await
        });
        let (released, dropped) = oneshot::channel();
        let reader = ChunkReader::new(
            Watched {
                inner: rx,
                delivered: 0,
                released: Some(released),
            },
            exec.clone(),
        );
        reader.drain_within(Duration::from_millis(200), ());
        let delivered = within("the drain to reach the FIN", dropped)
            .await
            .expect("the reader was dropped");
        assert_eq!(delivered, PIECES * (HEADER_DATA + PIECE) + 1);
        within("the paced writer", writer)
            .await
            .expect("join")
            .expect("write");
    }

    /// Appends its name to a shared log when it is dropped: the order in which
    /// a stream and what counts its connection are let go.
    struct Dropped {
        name: &'static str,
        log: Arc<Mutex<Vec<&'static str>>>,
    }

    impl Dropped {
        fn new(name: &'static str, log: &Arc<Mutex<Vec<&'static str>>>) -> Dropped {
            Dropped {
                name,
                log: Arc::clone(log),
            }
        }
    }

    impl Drop for Dropped {
        fn drop(&mut self) {
            self.log.lock().expect("log").push(self.name);
        }
    }

    /// A reader that logs when it is dropped.
    struct Traced<T> {
        inner: T,
        _dropped: Dropped,
    }

    impl<T: AsyncRead + Unpin> AsyncRead for Traced<T> {
        fn poll_read(
            self: Pin<&mut Self>,
            cx: &mut Context<'_>,
            buf: &mut ReadBuf<'_>,
        ) -> Poll<std::io::Result<()>> {
            Pin::new(&mut self.get_mut().inner).poll_read(cx, buf)
        }
    }

    async fn logged(log: &Arc<Mutex<Vec<&'static str>>>, entries: usize) {
        while log.lock().expect("log").len() < entries {
            tokio::task::yield_now().await;
        }
    }

    #[tokio::test]
    async fn an_end_marker_finished_on_a_task_holds_what_counts_the_connection() {
        let exec = exec();
        let log = Arc::new(Mutex::new(Vec::new()));
        let taken = Arc::new(Mutex::new(Vec::new()));
        let sink = Sink {
            taken: Arc::clone(&taken),
            limit: 4,
            stall_at: Some(4),
        };
        let writer = ChunkWriter::new(sink, exec.clone());
        writer.end(Marker::Reset(7), Dropped::new("hold", &log));
        assert_eq!(taken.lock().expect("sink").len(), 4);
        assert!(
            log.lock().expect("log").is_empty(),
            "the task still owns the stream, so it still holds"
        );
        within("the task to finish the marker and let go", logged(&log, 1)).await;
        assert_eq!(taken.lock().expect("sink").len(), 9);
    }

    /// Tests that touch the filesystem, network, processes or real time: the
    /// integration tier (`.config/nextest.toml`).
    mod io {
        use super::*;

        #[tokio::test]
        async fn a_drain_lets_go_of_what_counts_the_connection_after_the_handle() {
            let exec = exec();
            let log = Arc::new(Mutex::new(Vec::new()));
            let (mut tx, rx) = tokio::io::duplex(WIRE_CAP);
            // A payload that promises ten bytes, delivers three and goes quiet.
            tx.write_all(&[CHUNK_DATA, 10, 0, 0, 0, 1, 2, 3])
                .await
                .expect("write");
            let reader = ChunkReader::new(
                Traced {
                    inner: rx,
                    _dropped: Dropped::new("handle", &log),
                },
                exec.clone(),
            );
            reader.drain_within(Duration::from_millis(50), Dropped::new("hold", &log));
            assert!(
                log.lock().expect("log").is_empty(),
                "the drain owns both until it ends"
            );
            within("the drain to give up on the stalled peer", logged(&log, 2)).await;
            assert_eq!(*log.lock().expect("log"), ["handle", "hold"]);
            drop(tx);
        }
    }
}