sark-h2 0.28.2

Fiber-free HTTP/2 protocol primitives and manifold server for sark
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
use core::marker;

use o3::{
    alloc,
    depot::{
        arena::{slab, slab::entries},
        map,
        map::flat,
        queue::indexed::keyed,
    },
};

use crate::{
    conn,
    conn::registry::{Active, Changes, History, Past, Peer, Registry, Update, Wake},
    flow,
    frame::numbers,
    stream,
};

enum State {
    Active(Active),
    Reset(ResetState, Option<Closed>),
    Closed(Closed),
}

#[derive(Clone, Copy)]
pub(super) enum Live {
    Initial,
    Trailing,
    Duplex,
    End,
    DuplexEnd,
}

#[derive(Clone, Copy)]
enum Closed {
    Duplex,
    End,
    DuplexEnd,
    Reset,
    DuplexReset,
}

#[derive(Clone, Copy)]
struct ResetState {
    error: numbers::ErrorCode,
    next: Option<stream::Key>,
}

#[derive(Clone, Copy)]
pub(crate) enum Notice {
    End,
    Reset,
    Cancel,
    Duplex,
}

impl Live {
    fn retire(self, reset: bool) -> Option<Closed> {
        Some(match (self, reset) {
            (Self::Initial | Self::Trailing, false) => return None,
            (Self::Duplex, false) => Closed::Duplex,
            (Self::End, false) => Closed::End,
            (Self::DuplexEnd, false) => Closed::DuplexEnd,
            (Self::Initial | Self::Trailing | Self::End, true) => Closed::Reset,
            (Self::Duplex | Self::DuplexEnd, true) => Closed::DuplexReset,
        })
    }
}

impl Closed {
    fn release(&mut self, notice: Notice) -> bool {
        *self = match (*self, notice) {
            (Self::End, Notice::End | Notice::Cancel)
            | (Self::Reset, Notice::Reset | Notice::Cancel)
            | (Self::Duplex, Notice::Duplex) => return true,
            (Self::DuplexEnd, Notice::End | Notice::Cancel)
            | (Self::DuplexReset, Notice::Reset | Notice::Cancel) => Self::Duplex,
            (Self::DuplexEnd, Notice::Duplex) => Self::End,
            (Self::DuplexReset, Notice::Duplex) => Self::Reset,
            (_, Notice::Duplex) => unreachable!("stream"),
            (phase, _) => phase,
        };
        false
    }
}

impl State {
    fn active(&self) -> Option<&Active> {
        match self {
            Self::Active(active) => Some(active),
            _ => None,
        }
    }

    fn active_mut(&mut self) -> Option<&mut Active> {
        match self {
            Self::Active(active) => Some(active),
            _ => None,
        }
    }

    fn reset(&self) -> Option<ResetState> {
        match self {
            Self::Reset(reset, _) => Some(*reset),
            _ => None,
        }
    }

    fn owns_exchange(&self) -> bool {
        !matches!(self, Self::Reset(_, None))
    }

    fn close(&mut self, reset: bool) -> bool {
        let owner = match self {
            Self::Active(active) => active.peer.retire(reset),
            Self::Reset(_, owner) => *owner,
            Self::Closed(_) => unreachable!("stream"),
        };
        match owner {
            Some(owner) => *self = Self::Closed(owner),
            None => return true,
        }
        false
    }

    fn begin_reset(&mut self, reset: ResetState, notify: bool) {
        let Self::Active(active) = self else {
            unreachable!("stream")
        };
        *self = Self::Reset(reset, active.peer.retire(notify));
    }

    fn release(&mut self, notice: Notice) -> bool {
        match self {
            Self::Active(active) => {
                active.peer = match (active.peer, notice) {
                    (Live::End, Notice::End | Notice::Cancel) => Live::Trailing,
                    (Live::DuplexEnd, Notice::End | Notice::Cancel) => Live::Duplex,
                    (Live::Duplex, Notice::Duplex) => Live::Trailing,
                    (Live::DuplexEnd, Notice::Duplex) => Live::End,
                    (_, Notice::Duplex) => unreachable!("stream"),
                    (phase, _) => phase,
                };
            }
            Self::Reset(_, owner) => {
                if let Some(phase) = owner {
                    if phase.release(notice) {
                        *owner = None;
                    }
                } else if matches!(notice, Notice::Duplex) {
                    unreachable!("stream");
                }
            }
            Self::Closed(owner) => return owner.release(notice),
        }
        false
    }

    fn hold_duplex(&mut self) {
        let Self::Active(active) = self else {
            unreachable!("stream")
        };
        active.peer = match active.peer {
            Live::Trailing => Live::Duplex,
            Live::End => Live::DuplexEnd,
            _ => unreachable!("stream"),
        };
    }
}

pub(super) struct Record {
    pub(super) key: stream::Key,
    pub(super) send: keyed::Link<stream::Key, Send>,
    state: State,
}

impl Wake<'_> {
    pub(super) fn update<E>(
        mut self,
        edit: impl FnOnce(&mut Active) -> Result<(), E>,
    ) -> Result<Update, E> {
        let Some(active) = self.entry.get_mut().state.active_mut() else {
            return Ok(Update::Past(Past::Closed));
        };
        let blocked = active.send_window.is_stalled();
        edit(active)?;
        if blocked
            && !active.send_window.is_stalled()
            && let Ok(vacant) = keyed::Slot::try_vacant(self)
        {
            vacant.push::<false>();
        }
        Ok(Update::Applied)
    }
}

pub(super) enum Send {}

pub(crate) enum Lookup<'a> {
    Active(stream::Key, &'a mut Active),
    Class(Past),
}

pub(crate) enum Admission<'s, 'h, R: crate::Role, T> {
    Active(T),
    Past(Past),
    Open(Vacant<'s>, Peer<'h, R>),
    Full(Peer<'h, R>),
    Exhausted(stream::Id),
}

/// The caller decides while borrowing the original active record; the store
/// applies the transition through the same occupied entry after that borrow ends.
pub(crate) enum Action<T> {
    Keep(T),
    Remove(T),
    Reset(numbers::ErrorCode, bool, T),
}

type Occupied<'a> = slab::Occupied<
    entries::Busy<'a, Record, { u32::MAX }, entries::Exclusive<false>, 1, ()>,
    (),
    true,
>;

pub(crate) struct Vacant<'a> {
    id: stream::Nonzero,
    index: map::Vacancy<&'a mut flat::Table<stream::Key>, u64>,
    slot: slab::Vacant<
        entries::Pending<'a, Record, { u32::MAX }, entries::Exclusive<false>, 1, ()>,
        (),
        true,
    >,
    count: &'a mut usize,
}

pub(super) struct Transition<'a> {
    index: map::Found<&'a mut flat::Table<stream::Key>>,
    slot: keyed::Slot<Wake<'a>, Send, false>,
    count: &'a mut usize,
}

pub(super) struct Reset<'a> {
    index: map::Found<&'a mut flat::Table<stream::Key>>,
    slot: Occupied<'a>,
    error: numbers::ErrorCode,
    next: Option<stream::Key>,
}

pub(crate) struct Store<R: crate::Role> {
    role: marker::PhantomData<R>,
    pub(super) by_id: flat::Table<stream::Key>,
    pub(super) streams: slab::Store<Record>,
    local_count: usize,
    peer_count: usize,
}

impl<R: crate::Role> Store<R> {
    pub(crate) fn try_new(plan: map::Plan) -> Result<Self, alloc::Error> {
        let capacity = plan.capacity();
        Ok(Self {
            role: marker::PhantomData,
            by_id: flat::Table::try_new(plan)?,
            streams: slab::Store::<Record>::try_new(capacity)?,
            local_count: 0,
            peer_count: 0,
        })
    }

    fn edit<'a, I, T, E>(
        index: map::Found<&'a mut flat::Table<stream::Key>>,
        mut slot: Occupied<'a>,
        count: &'a mut usize,
        changes: Changes<'a>,
        input: I,
        edit: impl FnOnce(stream::Key, &mut Active, I) -> Result<Action<T>, E>,
    ) -> Result<Result<T, I>, E> {
        let key = *index.get();
        let record = slot.get_mut();
        let Some(active) = record.state.active_mut() else {
            return Ok(Err(input));
        };
        let value = match edit(key, active, input)? {
            Action::Keep(value) => value,
            Action::Remove(value) => {
                Transition {
                    index,
                    slot: Wake::clear(slot, changes.wakes),
                    count,
                }
                .remove(false);
                value
            }
            Action::Reset(error, notify, value) => {
                Transition {
                    index,
                    slot: Wake::clear(slot, changes.wakes),
                    count,
                }
                .reset(error, *changes.head, notify);
                *changes.head = Some(key);
                *changes.count += 1;
                value
            }
        };
        Ok(Ok(value))
    }

    pub(crate) fn vacant(
        &mut self,
        nonzero: stream::Nonzero,
    ) -> Result<Vacant<'_>, conn::Admission> {
        use flat::Entry;

        let id = nonzero.get();
        let Self {
            by_id,
            streams,
            local_count: local,
            peer_count: peer,
            ..
        } = self;
        let full = conn::Admission::Full {
            stream: id,
            used: streams.len(),
            limit: streams.capacity(),
        };
        let index = match by_id.entry(u64::from(id.into_u32()), |key| key.id() == id) {
            Some(Entry::Vacant(entry)) => entry,
            Some(Entry::Occupied(_)) => return Err(conn::Admission::Duplicate(id)),
            None => return Err(full),
        };
        let slot = streams.try_vacant().map_err(|error| match error {
            slab::Vacancy::Full => full,
            slab::Vacancy::IdentityExhausted => conn::Admission::IdentityExhausted(id),
        })?;
        let count = if History::<R>::is_local(id) {
            local
        } else {
            peer
        };
        Ok(Vacant {
            id: nonzero,
            index,
            slot,
            count,
        })
    }

    pub(crate) fn lookup<'a>(
        &'a mut self,
        history: &History<R>,
        proof: stream::Nonzero,
    ) -> Lookup<'a> {
        let id = proof.get();
        let key = match self
            .by_id
            .get(u64::from(id.into_u32()), |key| key.id() == id)
            .copied()
        {
            Some(key) => key,
            None => return Lookup::Class(history.classify(proof)),
        };
        match self.slot_mut(key) {
            Some(record) => match record.state.active_mut() {
                Some(active) => Lookup::Active(key, active),
                None => Lookup::Class(Past::Closed),
            },
            None => Lookup::Class(history.classify(proof)),
        }
    }

    pub(crate) fn key(&self, id: stream::Nonzero) -> Option<stream::Key> {
        let key = self
            .by_id
            .get(u64::from(id.get().into_u32()), |key| key.nonzero() == id)
            .copied()?;
        self.record_at(key)?;
        Some(key)
    }

    pub(crate) fn record_at(&self, key: stream::Key) -> Option<&Active> {
        self.slot(key)?.state.active()
    }

    pub(crate) fn active_mut(&mut self, key: stream::Key) -> Option<&mut Active> {
        self.slot_mut(key)?.state.active_mut()
    }

    pub(super) fn entry<'a>(
        &'a mut self,
        key: stream::Key,
        wakes: &'a mut keyed::Fifo<stream::Key, Send>,
    ) -> Option<Transition<'a>> {
        let Self {
            by_id,
            streams,
            local_count: local,
            peer_count: peer,
            ..
        } = self;
        let index = by_id.occupied(u64::from(key.id().into_u32()), |entry| *entry == key)?;
        let slot = streams.entry_at(key.index() as u32)?;
        if slot.get().state.active().is_none() {
            return None;
        }
        let count = if History::<R>::is_local(key.id()) {
            local
        } else {
            peer
        };
        Some(Transition {
            index,
            slot: Wake::clear(slot, wakes),
            count,
        })
    }

    pub(super) fn reset(&mut self, key: stream::Key) -> Option<Reset<'_>> {
        let Self { by_id, streams, .. } = self;
        let index = by_id.occupied(u64::from(key.id().into_u32()), |entry| *entry == key)?;
        let slot = streams.entry_at(key.index() as u32)?;
        let ResetState { error, next } = slot.get().state.reset()?;
        Some(Reset {
            index,
            slot,
            error,
            next,
        })
    }

    pub(crate) fn capacity(&self) -> usize {
        self.streams.capacity()
    }

    pub(crate) fn active_len(&self) -> usize {
        self.local_count + self.peer_count
    }

    pub(crate) fn local_len(&self) -> usize {
        self.local_count
    }

    pub(crate) fn owns_exchange(&self, key: stream::Key) -> bool {
        self.slot(key)
            .is_some_and(|record| record.state.owns_exchange())
    }

    pub(crate) fn hold_duplex(&mut self, key: stream::Key) {
        if let Some(record) = self.slot_mut(key) {
            record.state.hold_duplex();
        }
    }

    pub(crate) fn release(&mut self, key: stream::Key, notice: Notice) {
        let Some(mut slot) = self.streams.entry_at(key.slot()) else {
            return;
        };
        if slot.get().key != key {
            return;
        }
        if slot.get_mut().state.release(notice) {
            slot.discard();
        }
    }

    fn slot(&self, key: stream::Key) -> Option<&Record> {
        let record = self.streams.value_at(key.slot())?;
        (record.key == key).then_some(record)
    }

    fn slot_mut(&mut self, key: stream::Key) -> Option<&mut Record> {
        let record = self.streams.entry_at(key.slot())?.into_mut();
        (record.key == key).then_some(record)
    }
}

impl<R: crate::Role> Registry<R> {
    pub(crate) fn admit<'s, 'h, T, E>(
        &'s mut self,
        history: &'h mut History<R>,
        proof: stream::Nonzero,
        limit: usize,
        edit: impl FnOnce(stream::Key, &mut Active) -> Result<Action<T>, E>,
    ) -> Result<Admission<'s, 'h, R, T>, E> {
        use flat::Entry;

        let id = proof.get();
        let Store {
            by_id,
            streams,
            peer_count: count,
            local_count,
            ..
        } = &mut self.store;
        let index = match by_id.entry(u64::from(id.into_u32()), |key| key.id() == id) {
            Some(Entry::Occupied(entry)) => {
                let key = *entry.get();
                let Some(slot) = streams.entry_at(key.slot()) else {
                    return Ok(Admission::Past(history.classify(proof)));
                };
                let count = if History::<R>::is_local(id) {
                    local_count
                } else {
                    count
                };
                return Store::<R>::edit(
                    entry,
                    slot,
                    count,
                    Changes {
                        wakes: &mut self.send_wakes,
                        head: &mut self.pending_reset,
                        count: &mut self.reset_count,
                    },
                    (),
                    |key, active, ()| edit(key, active),
                )
                .map(|result| match result {
                    Ok(value) => Admission::Active(value),
                    Err(()) => Admission::Past(Past::Closed),
                });
            }
            Some(Entry::Vacant(entry)) => Some(entry),
            None => None,
        };
        let past = history.classify(proof);
        if past != Past::Idle {
            return Ok(Admission::Past(past));
        }
        let peer = match history.peer(proof) {
            Some(peer) => peer,
            None => return Ok(Admission::Past(Past::Idle)),
        };
        let Some(index) = index else {
            return Ok(Admission::Full(peer));
        };
        if *count >= limit {
            return Ok(Admission::Full(peer));
        }
        let slot = match streams.try_vacant() {
            Ok(slot) => slot,
            Err(slab::Vacancy::Full) => return Ok(Admission::Full(peer)),
            Err(slab::Vacancy::IdentityExhausted) => return Ok(Admission::Exhausted(id)),
        };
        Ok(Admission::Open(
            Vacant {
                id: proof,
                index,
                slot,
                count,
            },
            peer,
        ))
    }

    pub(crate) fn visit<I, T, E>(
        &mut self,
        history: &History<R>,
        proof: stream::Nonzero,
        input: I,
        edit: impl FnOnce(stream::Key, &mut Active, I) -> Result<Action<T>, E>,
    ) -> Result<Result<T, (Past, I)>, E> {
        let id = proof.get();
        let Some(index) = self
            .store
            .by_id
            .occupied(u64::from(id.into_u32()), |key| key.id() == id)
        else {
            return Ok(Err((history.classify(proof), input)));
        };
        let key = *index.get();
        let Some(slot) = self.store.streams.entry_at(key.slot()) else {
            return Ok(Err((history.classify(proof), input)));
        };
        let count = if History::<R>::is_local(id) {
            &mut self.store.local_count
        } else {
            &mut self.store.peer_count
        };
        Store::<R>::edit(
            index,
            slot,
            count,
            Changes {
                wakes: &mut self.send_wakes,
                head: &mut self.pending_reset,
                count: &mut self.reset_count,
            },
            input,
            edit,
        )
        .map(|value| value.map_err(|input| (Past::Closed, input)))
    }
}

impl Vacant<'_> {
    pub(crate) fn insert(
        self,
        stream: stream::Lifecycle,
        send: u32,
        recv: u32,
        remaining: u64,
        end: bool,
    ) -> stream::Key {
        use flow::Window;

        let Self {
            id,
            index,
            slot,
            count,
        } = self;
        let slot = slot
            .stage_with(|index| {
                let key = stream::Key::from_parts(id, index);
                let active = Active {
                    state: stream.state,
                    method: stream.method,
                    content: stream.content,
                    peer: if end {
                        Live::End
                    } else {
                        match stream.peer_section {
                            sark_core::manifold::stream::Section::Initial => Live::Initial,
                            sark_core::manifold::stream::Section::Trailing => Live::Trailing,
                        }
                    },
                    send_window: Window::with(send as i32),
                    recv_window: Window::with(recv as i32),
                    pending_release: 0,
                    remaining,
                };
                Record {
                    key,
                    send: keyed::Link::from(index),
                    state: State::Active(active),
                }
            })
            .commit();
        let key = slot.get().key;
        index.insert(key);
        *count += 1;
        key
    }
}

impl Transition<'_> {
    pub(super) fn remove(self, reset: bool) {
        let Self { index, slot, count } = self;
        finish(index, slot.release(), reset);
        *count -= 1;
    }

    pub(super) fn reset(self, error: numbers::ErrorCode, next: Option<stream::Key>, notify: bool) {
        let mut slot = self.slot.release();
        slot.get_mut()
            .state
            .begin_reset(ResetState { error, next }, notify);
        *self.count -= 1;
    }
}

impl Reset<'_> {
    pub(super) fn key(&self) -> stream::Key {
        self.slot.get().key
    }

    pub(super) const fn error(&self) -> numbers::ErrorCode {
        self.error
    }

    pub(super) const fn next(&self) -> Option<stream::Key> {
        self.next
    }

    pub(super) fn finish(self) {
        finish(self.index, self.slot, false);
    }
}

fn finish(index: map::Found<&mut flat::Table<stream::Key>>, mut slot: Occupied<'_>, reset: bool) {
    index.remove();
    if slot.get_mut().state.close(reset) {
        slot.discard();
    }
}