aimdb-core 3.0.0

Type-safe async data pipelines — one Rust codebase from MCU to cloud
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
//! The reactive **server** engine of the session substrate.
//!
//! - [`run_session`] drives one accepted [`Connection`]: a biased `select_biased!`
//!   loop interleaving inbound requests (RPC + subscribe + write) with outbound
//!   subscription events.
//! - [`serve`] is the accept loop over a [`Listener`], honoring
//!   [`SessionLimits::max_connections`].
//!
//! Spawn-free (every per-connection/-subscription task lives in a
//! [`FuturesUnordered`] the runner drives) and runtime-neutral (purely reactive,
//! so no timer and no `tokio`/`embassy-*`).
//!
//! The loops use an **extract-then-act** shape: `select_biased!` only computes a
//! small [`Step`], then the loop acts on it once the arm futures (and their
//! borrows of `conn`/`subs`) drop — `futures`' macro, unlike `tokio::select!`,
//! keeps non-selected futures alive across the handler.

use crate::session::events::{report, SessionEvent};

use alloc::boxed::Box;
use alloc::string::{String, ToString};
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::future::Future;
use core::pin::Pin;
use core::task::{Context, Poll};

use async_channel::Sender;
use futures_channel::oneshot;
use futures_util::stream::{FuturesUnordered, StreamExt};
use futures_util::{select_biased, FutureExt};
use hashbrown::HashMap;

use super::{
    BoxFut, BoxStream, Connection, Dispatch, EnvelopeCodec, Inbound, Listener, Outbound, Payload,
    RpcError, SessionLimits, SubUpdate, TransportError,
};

/// Per-session engine knobs.
#[derive(Debug, Clone, Default)]
pub struct SessionConfig {
    /// Connection cap (consulted by [`serve`]) and per-connection subscription
    /// cap (by [`run_session`]).
    pub limits: SessionLimits,
    /// If `true`, read one frame before authenticating and pass it to
    /// [`Dispatch::authenticate`] as an in-band Hello. If `false` (default),
    /// authenticate from [`PeerInfo`](super::PeerInfo) alone.
    pub reads_hello: bool,
    /// If `true`, emit an explicit [`Outbound::Subscribed`] ack before the first
    /// event. If `false` (default) the ack is implicit (events carry the
    /// subscription id back).
    pub acks_subscribe: bool,
}

/// Bound for the per-connection event funnel: pending outbound updates a
/// connection may buffer before pumps start dropping.
const EVENT_BUFFER: usize = 256;

/// One subscription update on its way back to the connection's send half.
struct SubEvent {
    sub: String,
    seq: u64,
    /// Concrete record topic, when the subscription tags its updates
    /// (see [`SubUpdate::topic`]).
    topic: Option<Arc<str>>,
    ts: crate::clock::Stamp,
    data: Payload,
}

/// What [`run_session`]'s `select_biased!` decided this iteration — extracted so
/// the work runs after the arm futures' borrows of `conn`/`subs` release.
enum Step {
    /// A logical frame arrived from the peer (decode + dispatch).
    Frame(Vec<u8>),
    /// Peer closed or the transport errored — end the session.
    Closed,
    /// A subscription update to encode and forward to the peer.
    Event(SubEvent),
    /// A subscription pump finished — reap it and prune its `cancels` entry,
    /// but only while that entry is still the pump's own (same generation).
    SubDrained(PumpEnd),
}

/// How a subscription pump ended, returned to [`run_session`].
struct PumpEnd {
    sub_id: String,
    /// Which `cancels` entry was the pump's own.
    generation: u64,
    /// The stream ran out on its own, not through a cancel or a closed funnel.
    exhausted: bool,
}

/// Drive one accepted [`Connection`] until it closes.
///
/// Authenticates once, then interleaves (biased toward inbound reads, so a chatty
/// subscription cannot starve the RPC path) incoming requests, outgoing
/// subscription events, and reaping of finished subscription pumps. Dropping the
/// engine cancels every live subscription.
pub async fn run_session<C, D>(
    mut conn: Box<dyn Connection>,
    codec: &C,
    dispatch: &D,
    config: &SessionConfig,
) where
    C: EnvelopeCodec + ?Sized,
    D: Dispatch + ?Sized,
{
    // Resolve the session context (Hello-frame or peer-only — see `reads_hello`).
    let first = if config.reads_hello {
        match conn.recv().await {
            Ok(Some(frame)) => Some(frame),
            // Peer closed or errored before sending the Hello — nothing to serve.
            _ => return,
        }
    } else {
        None
    };
    let ctx = match dispatch.authenticate(conn.peer(), first.as_deref()).await {
        Ok(ctx) => ctx,
        Err(_e) => {
            report(SessionEvent::AuthenticationRejected { error: &_e });
            return;
        }
    };

    // Open the per-connection session once; the loop threads `&mut` into it.
    let mut session = dispatch.open(&ctx);

    // Event funnel: every per-subscription pump sends updates here; the main loop
    // is the sole writer to the connection. Bounded, so a slow client cannot grow
    // it without limit — pumps drop on overflow (events carry a monotonic `seq`).
    let (mut event_tx, mut event_rx) = async_channel::bounded::<SubEvent>(EVENT_BUFFER);
    // Per-connection subscription pumps; the engine future is their sole owner.
    // Each resolves to its own sub id and generation, so the loop can prune the
    // matching `cancels` entry (a no-op if Unsubscribe already removed it).
    let mut subs: FuturesUnordered<BoxFut<'static, PumpEnd>> = FuturesUnordered::new();
    // sub-id → (generation, cancel handle); dropping/firing the oneshot cancels
    // the pump. The generation tells a pump's own entry from a later
    // subscription that reuses its id after an Unsubscribe: a pump that ends
    // late must not remove that one.
    let mut cancels: HashMap<String, (u64, oneshot::Sender<()>)> = HashMap::new();
    let mut next_generation: u64 = 0;
    // Whether this connection has seen a handshake; a second one starts a new
    // session (see `Session::starts_session`).
    let mut greeted = false;
    // Reused encode scratch buffer.
    let mut out = Vec::new();

    loop {
        // Biased toward inbound reads. The select only decides the next step; the
        // work happens after, in the `match`, once the arm borrows release.
        let step = {
            // Per-iteration futures, fused for the select. `event_rx.recv()` is
            // `!Unpin`, so pin it; `subs` (a `FusedStream`) parks on the empty set
            // while the always-active `recv` arm keeps the select alive.
            let mut recv = conn.recv().fuse();
            let mut event = core::pin::pin!(event_rx.recv().fuse());
            select_biased! {
                // ---- inbound: one logical frame from the peer --------------
                r = recv => match r {
                    Ok(Some(frame)) => Step::Frame(frame),
                    Ok(None) | Err(_) => Step::Closed, // peer closed / transport error
                },
                // ---- outbound: a subscription update to forward ------------
                ev = event => match ev {
                    Ok(ev) => Step::Event(ev),
                    // Funnel closed — only if every sender dropped, which can't
                    // happen while the loop holds `event_tx`, so this is
                    // unreachable; end the session defensively if it ever does.
                    Err(_) => Step::Closed,
                },
                // ---- drain finished subscription pumps ---------------------
                end = subs.select_next_some() => Step::SubDrained(end),
            }
        };

        match step {
            Step::Closed => break,
            // A pump finished; drop its cancel handle so the ended subscription
            // neither leaks nor keeps counting against
            // `max_subs_per_connection` — unless the id now belongs to a newer
            // subscription (Unsubscribe, then Subscribe with the same id).
            Step::SubDrained(end) => {
                let own = cancels
                    .get(&end.sub_id)
                    .is_some_and(|(g, _)| *g == end.generation);
                if !own {
                    continue;
                }
                cancels.remove(&end.sub_id);
                // A subscription the client still holds ended on its own: its
                // source closed, or never opened (a buffer with no subscriber
                // slot left). Tell the client, which turns the error reply to
                // its subscribe id into a terminal error, instead of leaving an
                // acknowledged subscription that never delivers.
                if end.exhausted {
                    // Its last events may still sit in the funnel: the pump
                    // runs inside the reap arm, so a stream that is ready at
                    // once fills the funnel and ends in one poll. Send them
                    // first, so the error is the subscription's last word.
                    let mut flushed = true;
                    while let Ok(ev) = event_rx.try_recv() {
                        if !send_event(&mut conn, codec, &mut out, ev).await {
                            flushed = false;
                            break;
                        }
                    }
                    if !flushed {
                        break;
                    }
                    report(SessionEvent::SubscriptionEnded { id: &end.sub_id });
                    if let Ok(id) = end.sub_id.parse::<u64>() {
                        send_reply_err(&mut conn, codec, &mut out, id, RpcError::Internal).await;
                    }
                }
            }

            Step::Event(ev) => {
                if !send_event(&mut conn, codec, &mut out, ev).await {
                    break;
                }
            }

            Step::Frame(frame) => {
                let msg = match codec.decode(&frame) {
                    Ok(msg) => msg,
                    Err(_e) => continue, // skip a malformed frame, keep the session
                };
                match msg {
                    Inbound::Request { id, method, params } => {
                        if session.starts_session(&method) {
                            if greeted {
                                // A new client on a connection that never closed
                                // (a serial line): nothing of the old session
                                // may reach it or outlive it. Dropping the pumps
                                // releases their buffer subscribers; a fresh
                                // funnel discards their queued events.
                                report(SessionEvent::HandshakeReset {
                                    ended_subscriptions: cancels.len(),
                                });
                                subs = FuturesUnordered::new();
                                cancels.clear();
                                (event_tx, event_rx) =
                                    async_channel::bounded::<SubEvent>(EVENT_BUFFER);
                                session = dispatch.open(&ctx);
                            }
                            greeted = true;
                        }
                        let result = session.call(&method, params).await;
                        out.clear();
                        if codec
                            .encode(Outbound::Reply { id, result }, &mut out)
                            .is_err()
                        {
                            continue;
                        }
                        if conn.send(&out).await.is_err() {
                            break;
                        }
                    }
                    Inbound::Subscribe { id, topic } => {
                        // The opening request id is the subscription's routing key;
                        // events carry it back as `Outbound::Event.sub`.
                        let sub_id = id.to_string();
                        // An id that still names a live subscription is refused:
                        // replacing it would cancel the live one without a word.
                        if cancels.contains_key(&sub_id) {
                            report(SessionEvent::SubscribeRefused { id: &sub_id });
                            send_reply_err(&mut conn, codec, &mut out, id, RpcError::Denied).await;
                            continue;
                        }
                        if cancels.len() >= config.limits.max_subs_per_connection {
                            send_reply_err(&mut conn, codec, &mut out, id, RpcError::Denied).await;
                            continue;
                        }
                        match session.subscribe(&topic).await {
                            Ok(stream) => {
                                // Optional explicit ack (see `acks_subscribe`).
                                if config.acks_subscribe {
                                    out.clear();
                                    if codec
                                        .encode(Outbound::Subscribed { sub: &sub_id }, &mut out)
                                        .is_ok()
                                        && conn.send(&out).await.is_err()
                                    {
                                        break;
                                    }
                                }
                                // Optional late-join snapshots (one per covered
                                // record), before the first event. They share
                                // the subscription's `seq` space — numbered
                                // `1..=N` here, events continue at `N + 1` — so
                                // every snapshot is accounted for end-to-end:
                                // one dropped by a slow client sink (or skipped
                                // by the encode failure below) leaves a hole the
                                // client reports as `skipped`, and a loss at the
                                // tail of the burst surfaces on the first event.
                                let snaps = session.snapshots(&topic);
                                let total = snaps.len();
                                let mut seq: u64 = 0;
                                let mut send_failed = false;
                                for (i, (snap_topic, data, ts)) in snaps.into_iter().enumerate() {
                                    seq += 1;
                                    out.clear();
                                    let encoded = codec.encode(
                                        Outbound::Snapshot {
                                            sub: &sub_id,
                                            seq,
                                            // The client reserves a sink slot
                                            // for this one, so the burst's
                                            // loss total always lands.
                                            last: i + 1 == total,
                                            topic: &snap_topic,
                                            ts,
                                            data,
                                        },
                                        &mut out,
                                    );
                                    if let Err(_e) = encoded {
                                        // A record whose serialized bytes aren't
                                        // valid for this envelope — broken for
                                        // every read of it, not just here, so say
                                        // so rather than dropping it in silence.
                                        // `seq` already advanced, so the client
                                        // still counts it as loss; if this was the
                                        // burst's last snapshot the `last` flag
                                        // goes with it and no `snapshot_end`
                                        // reaches the subscriber.
                                        report(SessionEvent::SnapshotEncodeFailed {
                                            id: &sub_id,
                                            topic: &snap_topic,
                                            error: &_e,
                                        });
                                        continue;
                                    }
                                    if conn.send(&out).await.is_err() {
                                        send_failed = true;
                                        break;
                                    }
                                }
                                if send_failed {
                                    break;
                                }
                                let (cancel_tx, cancel_rx) = oneshot::channel();
                                let generation = next_generation;
                                next_generation += 1;
                                cancels.insert(sub_id.clone(), (generation, cancel_tx));
                                subs.push(Box::pin(pump_subscription(
                                    sub_id,
                                    generation,
                                    stream,
                                    event_tx.clone(),
                                    cancel_rx,
                                    seq,
                                )));
                            }
                            Err(e) => {
                                send_reply_err(&mut conn, codec, &mut out, id, e).await;
                            }
                        }
                    }
                    Inbound::Unsubscribe { sub } => {
                        // Dropping the sender resolves the pump's cancel future.
                        cancels.remove(&sub);
                    }
                    Inbound::Write { topic, payload } => {
                        // Fire-and-forget; single-writer-per-key stays intact.
                        let _ = session.write(&topic, payload).await;
                    }
                    Inbound::Ping => {
                        out.clear();
                        if codec.encode(Outbound::Pong, &mut out).is_ok()
                            && conn.send(&out).await.is_err()
                        {
                            break;
                        }
                    }
                }
            }
        }
    }

    // Dropping `subs` here cancels every live subscription pump.
    drop(subs);
}

/// Encode + send one subscription event. `false` only when the connection is
/// gone; an event that does not encode is skipped.
async fn send_event<C: EnvelopeCodec + ?Sized>(
    conn: &mut Box<dyn Connection>,
    codec: &C,
    out: &mut Vec<u8>,
    ev: SubEvent,
) -> bool {
    out.clear();
    let encoded = codec
        .encode(
            Outbound::Event {
                sub: &ev.sub,
                seq: ev.seq,
                topic: ev.topic.as_deref(),
                ts: ev.ts,
                data: ev.data,
            },
            out,
        )
        .is_ok();
    !encoded || conn.send(out).await.is_ok()
}

/// Encode + send a `Reply` carrying an [`RpcError`]; best-effort (a send/encode
/// failure just ends this attempt — the caller's loop handles a dead connection
/// on its next `send`).
async fn send_reply_err<C: EnvelopeCodec + ?Sized>(
    conn: &mut Box<dyn Connection>,
    codec: &C,
    out: &mut Vec<u8>,
    id: u64,
    err: RpcError,
) {
    out.clear();
    if codec
        .encode(
            Outbound::Reply {
                id,
                result: Err(err),
            },
            out,
        )
        .is_ok()
    {
        let _ = conn.send(out).await;
    }
}

/// Pump one `Session::subscribe` stream into the connection's event funnel,
/// tagging each update with a monotonic `seq`. Ends when the stream finishes or
/// the cancel handle is dropped/fired (Unsubscribe or connection teardown).
///
/// `start_seq` is the last sequence number the late-join snapshot burst used
/// (`0` when there were none), so events continue the *same* counter and a
/// snapshot lost at the tail of the burst still shows up as a gap on the first
/// event.
///
/// Returns its `sub_id` and `generation` so [`run_session`] can prune the
/// `cancels` entry for a pump that ended on its own; the Unsubscribe path
/// already removed it, and a newer subscription under the same id has another
/// generation, so that later prune leaves both alone. `exhausted` tells a
/// stream that ran out from a cancel or a closed funnel.
async fn pump_subscription(
    sub_id: String,
    generation: u64,
    mut stream: BoxStream<'static, SubUpdate>,
    tx: Sender<SubEvent>,
    cancel: oneshot::Receiver<()>,
    start_seq: u64,
) -> PumpEnd {
    // Fuse the cancel receiver: a bare `oneshot::Receiver` reports
    // `is_terminated()` once its sender drops, and `select_biased!` skips
    // terminated arms — so the cancel would never fire. `Fuse` keeps the arm
    // polled until it actually resolves.
    let mut cancel = cancel.fuse();
    let mut seq: u64 = start_seq;
    let mut exhausted = false;
    loop {
        // Independent arms, so a direct `select_biased!` is fine here.
        let update = select_biased! {
            // Resolves on explicit Unsubscribe (send) or on sender drop.
            _ = cancel => break,
            // `BoxStream` is not `FusedStream`, so fuse the per-iteration `next`.
            next = stream.next().fuse() => match next {
                Some(update) => update,
                None => {
                    exhausted = true;
                    break;
                }
            },
        };
        seq += update.skipped + 1;
        // Non-blocking: drop on a full funnel (slow-client protection); only a
        // disconnected funnel ends the pump.
        match tx.try_send(SubEvent {
            sub: sub_id.clone(),
            seq,
            topic: update.topic,
            ts: update.ts,
            data: update.data,
        }) {
            Ok(()) => {}
            Err(e) if e.is_full() => {} // drop on overflow
            Err(_) => break,            // funnel disconnected — connection gone
        }
    }
    PumpEnd {
        sub_id,
        generation,
        exhausted,
    }
}

/// Accept connections from `listener` and serve each with [`run_session`],
/// bounded by [`SessionLimits::max_connections`]. The accept loop and all
/// per-connection futures share one [`FuturesUnordered`] — spawn-free.
///
/// Runs until the listener reports [`TransportError::Closed`] (no more
/// connections will come) and every open connection has ended, then returns.
/// A listener that never closes keeps it running.
pub async fn serve<L, C, D>(mut listener: L, codec: Arc<C>, dispatch: Arc<D>, config: SessionConfig)
where
    L: Listener,
    C: EnvelopeCodec + 'static,
    // `?Sized` so a caller can serve an `Arc<dyn Dispatch>`.
    D: Dispatch + 'static + ?Sized,
{
    let mut conns: FuturesUnordered<BoxFut<'static, ()>> = FuturesUnordered::new();
    // Set by a failed accept: the next one yields once before it starts.
    let mut after_error = false;
    // Consecutive failed accepts, so a listener that keeps failing logs a
    // summary now and then instead of one line per attempt.
    let mut failures = AcceptFailures::default();

    loop {
        // Extract the accept result first, then act (keeps the `listener` and
        // `conns` borrows apart).
        let accept = {
            // After an error, the next accept first returns `Pending` once, so
            // this select falls through to the connections: a listener that
            // fails at once, every time, cannot starve them, and the task
            // stays cancellable.
            let mut accept = PendingOnce::new(listener.accept(), after_error).fuse();
            select_biased! {
                a = accept => a,
                // Parks on the empty set, so the accept arm keeps the select alive.
                () = conns.select_next_some() => continue,
            }
        };

        after_error = accept.is_err();
        match accept {
            Ok(conn) => {
                if let Some(event) = failures.on_success() {
                    report(event);
                }
                // Soft cap; `len()` counts finished-but-not-yet-reaped futures, so
                // under an accept flood (the biased `accept` arm starves the reap
                // arm) it may read high transiently — acceptable for a soft cap.
                if conns.len() >= config.limits.max_connections {
                    report(SessionEvent::ConnectionLimitReached {
                        limit: config.limits.max_connections,
                    });
                    drop(conn);
                    continue;
                }
                let codec = codec.clone();
                let dispatch = dispatch.clone();
                let cfg = config.clone();
                conns.push(Box::pin(async move {
                    run_session(conn, codec.as_ref(), dispatch.as_ref(), &cfg).await;
                }));
            }
            // The listener hands out no more connections: stop accepting, and
            // serve the open ones to their end. Calling `accept` again would
            // only fail again, at once — a spin that starves those connections.
            Err(TransportError::Closed) => {
                report(SessionEvent::ListenerClosed {
                    open_connections: conns.len(),
                });
                while conns.next().await.is_some() {}
                return;
            }
            Err(_e) => {
                if let Some(event) = failures.on_error(_e) {
                    report(event);
                }
                // Keep serving existing connections despite a transient accept
                // error; the next accept yields first (`after_error`).
            }
        }
    }
}

/// Counts consecutive failed accepts and decides which of them to log: the
/// first of a series, then a summary at 10, 100, 1000, … failures, so a
/// listener that keeps failing grows the log by one line per decade instead
/// of one per attempt.
#[derive(Default)]
struct AcceptFailures {
    consecutive: u64,
}

impl AcceptFailures {
    fn on_error(&mut self, error: TransportError) -> Option<SessionEvent<'static>> {
        self.consecutive = self.consecutive.saturating_add(1);
        match self.consecutive {
            1 => Some(SessionEvent::AcceptFailed { error }),
            n if is_power_of_ten(n) => {
                Some(SessionEvent::AcceptStillFailing { attempts: n, error })
            }
            _ => None,
        }
    }

    /// The length of the series a success ends, if there was one.
    fn on_success(&mut self) -> Option<SessionEvent<'static>> {
        let failed = core::mem::take(&mut self.consecutive);
        (failed > 0).then_some(SessionEvent::AcceptRecovered { after: failed })
    }
}

fn is_power_of_ten(mut n: u64) -> bool {
    while n >= 10 && n.is_multiple_of(10) {
        n /= 10;
    }
    n == 1
}

/// `inner`, but when `yield_first` is set, its first poll returns `Pending`
/// (waking itself) without polling `inner`. Runtime-neutral: one turn back to
/// the executor and to the sibling arms of a select.
struct PendingOnce<F> {
    inner: F,
    yield_first: bool,
}

impl<F> PendingOnce<F> {
    fn new(inner: F, yield_first: bool) -> Self {
        Self { inner, yield_first }
    }
}

impl<F: Future + Unpin> Future for PendingOnce<F> {
    type Output = F::Output;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        if self.yield_first {
            self.yield_first = false;
            cx.waker().wake_by_ref();
            return Poll::Pending;
        }
        Pin::new(&mut self.inner).poll(cx)
    }
}

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

    #[test]
    fn a_failing_series_reports_first_then_once_per_decade() {
        let mut failures = AcceptFailures::default();
        let reported: Vec<_> = (1..=1000)
            .filter_map(|n| {
                failures.on_error(TransportError::Io).map(|event| {
                    assert!(matches!(
                        event,
                        SessionEvent::AcceptFailed {
                            error: TransportError::Io
                        } | SessionEvent::AcceptStillFailing {
                            error: TransportError::Io,
                            ..
                        }
                    ));
                    n
                })
            })
            .collect();
        assert_eq!(reported, [1, 10, 100, 1000]);
        assert!(matches!(
            failures.on_success(),
            Some(SessionEvent::AcceptRecovered { after: 1000 })
        ));
        assert!(failures.on_success().is_none());
        assert!(matches!(
            failures.on_error(TransportError::Busy),
            Some(SessionEvent::AcceptFailed {
                error: TransportError::Busy
            })
        ));
    }

    #[test]
    fn failure_count_saturates_without_repeated_summaries() {
        let mut failures = AcceptFailures {
            consecutive: u64::MAX - 1,
        };
        assert!(failures.on_error(TransportError::Io).is_none());
        assert!(failures.on_error(TransportError::Io).is_none());
        assert!(matches!(
            failures.on_success(),
            Some(SessionEvent::AcceptRecovered { after: u64::MAX })
        ));
    }

    #[test]
    fn powers_of_ten() {
        let found: Vec<u64> = (0..=10_000).filter(|&n| is_power_of_ten(n)).collect();
        assert_eq!(found, [1, 10, 100, 1000, 10_000]);
    }
}