hyperi-rustlib 2.7.1

Opinionated, drop-in Rust toolkit for production services at scale. The patterns from blog posts as actual code: 8-layer config cascade, structured logging with PII masking, Prometheus + OpenTelemetry, Kafka/gRPC transports, tiered disk-spillover, adaptive worker pools, graceful shutdown.
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
// Project:   hyperi-rustlib
// File:      src/concurrency/sink.rs
// Purpose:   BackgroundSink — generic fire-and-forget durable sink
// Language:  Rust
//
// License:   FSL-1.1-ALv2
// Copyright: (c) 2026 HYPERI PTY LIMITED

//! Generic fire-and-forget durable sink built on bounded `mpsc` +
//! background actor. Consumer hot path is sync-shaped and never blocks
//! the tokio runtime — `try_push` is ~100 ns push to an mpsc queue.
//!
//! See `docs/superpowers/plans/2026-05-08-async-patterns.md` for the
//! design rationale and `hyperi-ai/standards/languages/RUST.md`
//! "Three async primitives" subsection for the canonical usage.
//!
//! # The contract
//!
//! - [`BackgroundSink::try_push`] returns immediately. ~100 ns when the
//!   queue has space; `Err(SinkError::Overflow)` if full (Drop mode).
//! - [`BackgroundSink::push_blocking`] is `async` and awaits queue
//!   space. Use when the caller can yield.
//! - [`BackgroundSink::flush`] is `async` and resolves only after every
//!   message accepted before this call is durably written by the drain.
//!
//! # Shape
//!
//! ```text
//! consumer (many) ──try_push──► mpsc bounded ──► actor task ──► drain.write_batch
//! ```
//!
//! The actor batches messages by size (`batch_size`) or interval
//! (`flush_interval`), whichever fires first, then hands the batch to
//! the drain. Drain implementations supply backend-specific async I/O.
//!
//! # Shutdown
//!
//! When the [`tokio_util::sync::CancellationToken`] is cancelled, the
//! actor drains everything currently in the queue, writes a final
//! batch, calls `drain.close()`, and exits. Use the returned
//! [`BackgroundSinkHandle::join`] to await graceful drain.

use std::future::Future;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::time::Duration;

use tokio::sync::{mpsc, oneshot};
use tokio::task::JoinHandle;
use tokio::time::{MissedTickBehavior, interval};
use tokio_util::sync::CancellationToken;
use tracing::warn;

use super::error::{DrainError, SinkError};

/// Configuration for a [`BackgroundSink`].
#[derive(Debug, Clone)]
pub struct BackgroundSinkConfig {
    /// Maximum queued messages before overflow policy kicks in.
    /// Default 10_000.
    pub queue_capacity: usize,

    /// Coalesce up to this many messages into one drain call.
    /// Default 256.
    pub batch_size: usize,

    /// Flush a partial batch after this duration even if it's not full.
    /// Default 100 ms.
    pub flush_interval: Duration,

    /// What `try_push` does when the queue is full.
    /// Default `Overflow::Drop`.
    pub overflow: Overflow,

    /// Optional Prometheus metric prefix. When `Some("dfe_dlq_file")`,
    /// the sink auto-registers and emits:
    ///   - `<prefix>_pushed_total`         (counter)
    ///   - `<prefix>_dropped_total`        (counter, only `Overflow::Drop`)
    ///   - `<prefix>_writes_total`         (counter, per batch)
    ///   - `<prefix>_write_errors_total`   (counter, per failed batch)
    ///   - `<prefix>_pending`              (gauge, current queue depth)
    ///
    /// When `None`, the sink tracks `dropped()` / `pending()` via
    /// internal atomics only — callers can read them but nothing is
    /// published.
    pub metric_prefix: Option<&'static str>,
}

impl Default for BackgroundSinkConfig {
    fn default() -> Self {
        Self {
            queue_capacity: 10_000,
            batch_size: 256,
            flush_interval: Duration::from_millis(100),
            overflow: Overflow::Drop,
            metric_prefix: None,
        }
    }
}

/// Overflow policy for [`BackgroundSink::try_push`].
///
/// `push_blocking` and `flush` always wait for queue space regardless
/// of this setting — overflow only governs the sync `try_push` path.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Overflow {
    /// `try_push` returns `Err(Overflow)` immediately and increments
    /// the dropped counter. Default for hot paths.
    Drop,
    /// `try_push` is rejected — callers in this mode MUST use
    /// `push_blocking` or `flush`. Documents intent: "this sink may
    /// not be used from a sync context."
    Block,
}

/// Internal channel message. Tagged so flush() barriers and data
/// messages share the queue and are processed in FIFO order.
enum SinkMsg<T> {
    Data(T),
    Barrier(oneshot::Sender<()>),
}

/// A drain consumes batches of messages and writes them to the backend.
///
/// Implementations are captured by [`BackgroundSink::spawn`] and live
/// inside the actor task. They run only on the actor — never on
/// consumer hot paths.
///
/// **CRITICAL:** drain methods may take time (disk fsync, network
/// roundtrip). Use true-async I/O (`tokio::fs`, async clients like
/// `rdkafka` / `reqwest` / `redis`) or `tokio::task::spawn_blocking`
/// for unavoidable sync work. **NEVER** put `std::fs::*` /
/// `std::io::Write::*` / `std::thread::sleep` directly in `write_batch`
/// — that pins the actor task's tokio worker, and `tests/sync_in_async.rs`
/// will fail the lint.
pub trait SinkDrain<T: Send>: Send + 'static {
    /// Flush a batch. Implementer chooses the async I/O strategy.
    /// Returned `Err` is logged + counted by the actor; the actor
    /// continues draining subsequent batches.
    ///
    /// Takes `&mut self` — drains typically own mutable I/O state
    /// (file handles, connection pools, write buffers). The actor
    /// holds the only reference; this method is never called
    /// concurrently for a given drain instance.
    fn write_batch(&mut self, batch: Vec<T>)
    -> impl Future<Output = Result<(), DrainError>> + Send;

    /// One-shot close at actor shutdown. Default: no-op.
    /// Typical implementations flush remaining state, close file
    /// handles, return network connections to a pool.
    fn close(&mut self) -> impl Future<Output = Result<(), DrainError>> + Send {
        std::future::ready(Ok(()))
    }
}

/// Cloneable handle for pushing messages. Hot-path consumers clone
/// freely and push from many tasks concurrently.
#[derive(Debug, Clone)]
pub struct BackgroundSink<T: Send + 'static> {
    tx: mpsc::Sender<SinkMsg<T>>,
    dropped: Arc<AtomicU64>,
    pending: Arc<AtomicUsize>,
    overflow: Overflow,
    metric_prefix: Option<&'static str>,
}

/// Single-owner handle for awaiting actor shutdown.
///
/// Held by the orchestrator that called [`BackgroundSink::spawn`]. The
/// actor task is detached; the handle exists to let the owner join it
/// after cancelling the shutdown token.
pub struct BackgroundSinkHandle {
    join: JoinHandle<()>,
}

impl BackgroundSinkHandle {
    /// Await the actor's clean exit. Use after signalling shutdown via
    /// the `CancellationToken` passed to `spawn`.
    ///
    /// Returns `Err(JoinError)` only if the actor panicked.
    pub async fn join(self) -> Result<(), tokio::task::JoinError> {
        self.join.await
    }
}

impl<T: Send + 'static> BackgroundSink<T> {
    /// Spawn the actor task. Returns a cloneable sink + single-owner
    /// join handle.
    pub fn spawn<D: SinkDrain<T>>(
        drain: D,
        config: BackgroundSinkConfig,
        shutdown: CancellationToken,
    ) -> (Self, BackgroundSinkHandle) {
        let (tx, rx) = mpsc::channel(config.queue_capacity);
        let dropped = Arc::new(AtomicU64::new(0));
        let pending = Arc::new(AtomicUsize::new(0));
        let metric_prefix = config.metric_prefix;
        let overflow = config.overflow;

        if let Some(prefix) = metric_prefix {
            metrics::describe_counter!(
                format!("{prefix}_pushed_total"),
                "Messages successfully enqueued by the background sink"
            );
            metrics::describe_counter!(
                format!("{prefix}_dropped_total"),
                "Messages dropped due to queue overflow"
            );
            metrics::describe_counter!(
                format!("{prefix}_writes_total"),
                "Batch writes attempted by the drain"
            );
            metrics::describe_counter!(
                format!("{prefix}_write_errors_total"),
                "Batch writes that returned an error"
            );
            metrics::describe_gauge!(
                format!("{prefix}_pending"),
                "Current background sink queue depth"
            );
        }

        let actor_pending = Arc::clone(&pending);
        let join = tokio::spawn(actor_loop(
            rx,
            drain,
            config,
            shutdown,
            actor_pending,
            metric_prefix,
        ));

        (
            Self {
                tx,
                dropped,
                pending,
                overflow,
                metric_prefix,
            },
            BackgroundSinkHandle { join },
        )
    }

    /// Sync-shaped fire-and-forget push. ~100 ns happy path. Never
    /// awaits, never blocks the runtime, never holds a lock.
    ///
    /// - `Overflow::Drop` (default): returns `Err(Overflow)` immediately
    ///   when the queue is full and increments the dropped counter.
    /// - `Overflow::Block`: returns `Err(Overflow)` unconditionally —
    ///   callers in Block mode must use `push_blocking`. This makes the
    ///   policy decision explicit at the call site.
    pub fn try_push(&self, msg: T) -> Result<(), SinkError> {
        match self.overflow {
            Overflow::Drop => match self.tx.try_send(SinkMsg::Data(msg)) {
                Ok(()) => {
                    self.pending.fetch_add(1, Ordering::Relaxed);
                    if let Some(p) = self.metric_prefix {
                        metrics::counter!(format!("{p}_pushed_total")).increment(1);
                    }
                    Ok(())
                }
                Err(mpsc::error::TrySendError::Full(_)) => {
                    self.dropped.fetch_add(1, Ordering::Relaxed);
                    if let Some(p) = self.metric_prefix {
                        metrics::counter!(format!("{p}_dropped_total")).increment(1);
                    }
                    Err(SinkError::Overflow)
                }
                Err(mpsc::error::TrySendError::Closed(_)) => Err(SinkError::Closed),
            },
            Overflow::Block => Err(SinkError::Overflow),
        }
    }

    /// Async push that awaits queue space. Returns when queued (NOT
    /// yet durably written — use [`Self::flush`] for that).
    pub async fn push_blocking(&self, msg: T) -> Result<(), SinkError> {
        self.tx
            .send(SinkMsg::Data(msg))
            .await
            .map_err(|_| SinkError::Closed)?;
        self.pending.fetch_add(1, Ordering::Relaxed);
        if let Some(p) = self.metric_prefix {
            metrics::counter!(format!("{p}_pushed_total")).increment(1);
        }
        Ok(())
    }

    /// Await durability of every message accepted before this call.
    /// Returns when the actor has processed past a barrier inserted at
    /// the back of the queue.
    ///
    /// In `Overflow::Drop` mode, "every message accepted" excludes
    /// messages that were dropped via overflow — those were never in
    /// the queue. If you need lossless flush semantics, use
    /// `Overflow::Block` + `push_blocking`.
    pub async fn flush(&self) -> Result<(), SinkError> {
        let (ack_tx, ack_rx) = oneshot::channel();
        self.tx
            .send(SinkMsg::Barrier(ack_tx))
            .await
            .map_err(|_| SinkError::Closed)?;
        ack_rx.await.map_err(|_| SinkError::Closed)
    }

    /// Total messages dropped due to overflow since spawn.
    #[must_use]
    pub fn dropped(&self) -> u64 {
        self.dropped.load(Ordering::Relaxed)
    }

    /// Current queue depth (approximate — actor may be mid-recv).
    #[must_use]
    pub fn pending(&self) -> usize {
        self.pending.load(Ordering::Relaxed)
    }
}

async fn actor_loop<T, D>(
    mut rx: mpsc::Receiver<SinkMsg<T>>,
    mut drain: D,
    config: BackgroundSinkConfig,
    shutdown: CancellationToken,
    pending: Arc<AtomicUsize>,
    metric_prefix: Option<&'static str>,
) where
    T: Send + 'static,
    D: SinkDrain<T>,
{
    let mut batch: Vec<T> = Vec::with_capacity(config.batch_size);
    let mut tick = interval(config.flush_interval);
    tick.set_missed_tick_behavior(MissedTickBehavior::Delay);
    // Consume the immediate first tick; otherwise interval fires at t=0.
    tick.tick().await;

    loop {
        tokio::select! {
            biased;

            () = shutdown.cancelled() => {
                // Drain remaining queue before exit.
                if !batch.is_empty() {
                    write_batch_with_metrics(
                        &mut drain, std::mem::take(&mut batch),
                        &pending, metric_prefix,
                    ).await;
                }
                while let Ok(msg) = rx.try_recv() {
                    match msg {
                        SinkMsg::Data(t) => batch.push(t),
                        SinkMsg::Barrier(ack) => { let _ = ack.send(()); }
                    }
                    if batch.len() >= config.batch_size {
                        write_batch_with_metrics(
                            &mut drain, std::mem::take(&mut batch),
                            &pending, metric_prefix,
                        ).await;
                    }
                }
                if !batch.is_empty() {
                    write_batch_with_metrics(
                        &mut drain, std::mem::take(&mut batch),
                        &pending, metric_prefix,
                    ).await;
                }
                if let Err(e) = drain.close().await {
                    warn!(error = %e, "sink drain close failed");
                }
                return;
            }

            msg = rx.recv() => match msg {
                Some(SinkMsg::Data(t)) => {
                    batch.push(t);
                    if batch.len() >= config.batch_size {
                        write_batch_with_metrics(
                            &mut drain, std::mem::take(&mut batch),
                            &pending, metric_prefix,
                        ).await;
                    }
                }
                Some(SinkMsg::Barrier(ack)) => {
                    if !batch.is_empty() {
                        write_batch_with_metrics(
                            &mut drain, std::mem::take(&mut batch),
                            &pending, metric_prefix,
                        ).await;
                    }
                    let _ = ack.send(());
                }
                None => {
                    // All senders dropped — graceful exit.
                    if !batch.is_empty() {
                        write_batch_with_metrics(
                            &mut drain, std::mem::take(&mut batch),
                            &pending, metric_prefix,
                        ).await;
                    }
                    if let Err(e) = drain.close().await {
                        warn!(error = %e, "sink drain close failed");
                    }
                    return;
                }
            },

            _ = tick.tick() => {
                if !batch.is_empty() {
                    write_batch_with_metrics(
                        &mut drain, std::mem::take(&mut batch),
                        &pending, metric_prefix,
                    ).await;
                }
            }
        }
    }
}

async fn write_batch_with_metrics<T, D: SinkDrain<T>>(
    drain: &mut D,
    batch: Vec<T>,
    pending: &AtomicUsize,
    metric_prefix: Option<&'static str>,
) where
    T: Send,
{
    let count = batch.len();
    pending.fetch_sub(count, Ordering::Relaxed);
    if let Some(p) = metric_prefix {
        metrics::counter!(format!("{p}_writes_total")).increment(1);
        metrics::gauge!(format!("{p}_pending")).set(pending.load(Ordering::Relaxed) as f64);
    }
    if let Err(e) = drain.write_batch(batch).await {
        warn!(error = %e, count, "sink drain write_batch failed");
        if let Some(p) = metric_prefix {
            metrics::counter!(format!("{p}_write_errors_total")).increment(1);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicU64, Ordering};
    use std::time::Instant;

    use tokio::sync::Notify;
    use tokio_util::sync::CancellationToken;

    /// Counts written messages. Used as the trivial drain in fast tests.
    struct CountingDrain {
        count: Arc<AtomicU64>,
    }

    impl SinkDrain<u32> for CountingDrain {
        async fn write_batch(&mut self, batch: Vec<u32>) -> Result<(), DrainError> {
            self.count.fetch_add(batch.len() as u64, Ordering::SeqCst);
            Ok(())
        }
    }

    /// Drain that blocks each call on a Notify. Used to simulate slow
    /// backends and verify the consumer hot path is unaffected.
    struct ThrottledDrain {
        release: Arc<Notify>,
        count: Arc<AtomicU64>,
    }

    impl SinkDrain<u32> for ThrottledDrain {
        async fn write_batch(&mut self, batch: Vec<u32>) -> Result<(), DrainError> {
            self.release.notified().await;
            self.count.fetch_add(batch.len() as u64, Ordering::SeqCst);
            Ok(())
        }
    }

    fn fast_config() -> BackgroundSinkConfig {
        BackgroundSinkConfig {
            queue_capacity: 1024,
            batch_size: 16,
            flush_interval: Duration::from_millis(20),
            overflow: Overflow::Drop,
            metric_prefix: None,
        }
    }

    #[tokio::test]
    async fn try_push_succeeds_when_queue_has_space() {
        let count = Arc::new(AtomicU64::new(0));
        let shutdown = CancellationToken::new();
        let (sink, _handle) = BackgroundSink::spawn(
            CountingDrain {
                count: count.clone(),
            },
            fast_config(),
            shutdown.clone(),
        );

        for i in 0..10 {
            sink.try_push(i).expect("queue has space");
        }
        sink.flush().await.expect("flush ok");
        assert_eq!(count.load(Ordering::SeqCst), 10);
        shutdown.cancel();
    }

    #[tokio::test]
    async fn try_push_returns_overflow_when_full() {
        let count = Arc::new(AtomicU64::new(0));
        let release = Arc::new(Notify::new());
        let shutdown = CancellationToken::new();
        let cfg = BackgroundSinkConfig {
            queue_capacity: 4,
            batch_size: 16,
            flush_interval: Duration::from_mins(1),
            overflow: Overflow::Drop,
            metric_prefix: None,
        };
        let (sink, _handle) = BackgroundSink::spawn(
            ThrottledDrain {
                release: release.clone(),
                count: count.clone(),
            },
            cfg,
            shutdown.clone(),
        );

        // Fill the queue without releasing the actor's first write.
        let mut accepted: u64 = 0;
        let mut overflowed: u64 = 0;
        for i in 0..20 {
            match sink.try_push(i) {
                Ok(()) => accepted += 1,
                Err(SinkError::Overflow) => overflowed += 1,
                Err(e) => panic!("unexpected error: {e}"),
            }
        }
        assert!(overflowed > 0, "expected at least one overflow");
        assert_eq!(sink.dropped(), overflowed);
        // Queue cap is 4 — once filled, every push past that overflows.
        assert!(accepted >= 4, "should accept at least queue_capacity");
        let _ = (accepted, count); // silence unused
        shutdown.cancel();
        release.notify_waiters();
    }

    #[tokio::test]
    async fn try_push_in_block_mode_always_errors() {
        let count = Arc::new(AtomicU64::new(0));
        let shutdown = CancellationToken::new();
        let cfg = BackgroundSinkConfig {
            overflow: Overflow::Block,
            ..fast_config()
        };
        let (sink, _handle) = BackgroundSink::spawn(
            CountingDrain {
                count: count.clone(),
            },
            cfg,
            shutdown.clone(),
        );
        // try_push in Block mode is rejected regardless of queue state.
        match sink.try_push(1) {
            Err(SinkError::Overflow) => {}
            other => panic!("expected Overflow, got {other:?}"),
        }
        // push_blocking still works.
        sink.push_blocking(1)
            .await
            .expect("push_blocking ok in Block mode");
        sink.flush().await.expect("flush ok");
        assert_eq!(count.load(Ordering::SeqCst), 1);
        shutdown.cancel();
    }

    #[tokio::test]
    async fn flush_waits_for_pre_flush_messages() {
        let count = Arc::new(AtomicU64::new(0));
        let shutdown = CancellationToken::new();
        let (sink, _handle) = BackgroundSink::spawn(
            CountingDrain {
                count: count.clone(),
            },
            fast_config(),
            shutdown.clone(),
        );

        for i in 0..100 {
            sink.try_push(i).expect("queue has space");
        }
        sink.flush().await.expect("flush ok");
        // Every pushed message must be drained before flush returns.
        assert_eq!(count.load(Ordering::SeqCst), 100);
        shutdown.cancel();
    }

    #[tokio::test]
    async fn shutdown_drains_remaining_queue() {
        let count = Arc::new(AtomicU64::new(0));
        let shutdown = CancellationToken::new();
        let (sink, handle) = BackgroundSink::spawn(
            CountingDrain {
                count: count.clone(),
            },
            fast_config(),
            shutdown.clone(),
        );
        for i in 0..50 {
            sink.try_push(i).expect("queue has space");
        }
        shutdown.cancel();
        handle.join().await.expect("clean exit");
        assert_eq!(count.load(Ordering::SeqCst), 50);
    }

    #[tokio::test]
    async fn dropped_counter_reflects_overflow_count() {
        let count = Arc::new(AtomicU64::new(0));
        let release = Arc::new(Notify::new());
        let shutdown = CancellationToken::new();
        let cfg = BackgroundSinkConfig {
            queue_capacity: 2,
            batch_size: 16,
            flush_interval: Duration::from_mins(1),
            overflow: Overflow::Drop,
            metric_prefix: None,
        };
        let (sink, _handle) = BackgroundSink::spawn(
            ThrottledDrain {
                release: release.clone(),
                count: count.clone(),
            },
            cfg,
            shutdown.clone(),
        );
        for i in 0..100 {
            let _ = sink.try_push(i);
        }
        // Most pushes overflowed since drain is throttled and queue cap is 2.
        assert!(sink.dropped() >= 90, "dropped={}", sink.dropped());
        shutdown.cancel();
        release.notify_waiters();
    }

    // ---------------------------------------------------------------
    // NON-BLOCKING-UNDER-LOAD TESTS
    //
    // These directly address the "async + non-blocking issues are
    // opaque" concern. They prove the hot path stays sub-microsecond
    // even when the actor is throttled by a slow drain.
    // ---------------------------------------------------------------

    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn try_push_stays_fast_under_load() {
        let count = Arc::new(AtomicU64::new(0));
        let release = Arc::new(Notify::new());
        let shutdown = CancellationToken::new();
        let cfg = BackgroundSinkConfig {
            queue_capacity: 100_000,
            batch_size: 256,
            flush_interval: Duration::from_mins(1),
            overflow: Overflow::Drop,
            metric_prefix: None,
        };
        let (sink, _handle) = BackgroundSink::spawn(
            ThrottledDrain {
                release: release.clone(),
                count: count.clone(),
            },
            cfg,
            shutdown.clone(),
        );

        // Push 10_000 messages back-to-back. Drain is gated — actor is
        // blocked on `notified()`. The hot path MUST stay fast anyway.
        let start = Instant::now();
        for i in 0..10_000_u32 {
            sink.try_push(i).expect("queue has space");
        }
        let elapsed = start.elapsed();
        // Generous: average <50µs per push (way above the ~100ns target,
        // but the test must be robust against CI noise + multi-thread
        // contention). The bench gives the real performance number.
        let avg_us = elapsed.as_micros() as f64 / 10_000.0;
        assert!(
            avg_us < 50.0,
            "try_push p_avg = {avg_us}µs (expected <50µs under load with throttled drain)",
        );
        shutdown.cancel();
        release.notify_waiters();
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn many_concurrent_producers_dont_block_each_other() {
        let count = Arc::new(AtomicU64::new(0));
        let shutdown = CancellationToken::new();
        let cfg = BackgroundSinkConfig {
            queue_capacity: 100_000,
            batch_size: 1024,
            flush_interval: Duration::from_millis(5),
            overflow: Overflow::Drop,
            metric_prefix: None,
        };
        let (sink, _handle) = BackgroundSink::spawn(
            CountingDrain {
                count: count.clone(),
            },
            cfg,
            shutdown.clone(),
        );

        // 8 concurrent producers, each pushing 1000 messages.
        let mut handles = Vec::new();
        for _ in 0..8 {
            let s = sink.clone();
            handles.push(tokio::spawn(async move {
                for i in 0..1_000_u32 {
                    s.try_push(i).expect("queue has space");
                }
            }));
        }
        for h in handles {
            h.await.expect("producer exit");
        }
        sink.flush().await.expect("flush ok");
        assert_eq!(count.load(Ordering::SeqCst), 8_000);
        shutdown.cancel();
    }

    #[tokio::test]
    async fn flush_completes_quickly_when_queue_is_already_empty() {
        let count = Arc::new(AtomicU64::new(0));
        let shutdown = CancellationToken::new();
        let (sink, _handle) = BackgroundSink::spawn(
            CountingDrain {
                count: count.clone(),
            },
            fast_config(),
            shutdown.clone(),
        );
        // Empty-queue flush should return in well under one flush_interval.
        let start = Instant::now();
        sink.flush().await.expect("flush ok");
        let elapsed = start.elapsed();
        assert!(
            elapsed < Duration::from_millis(50),
            "empty flush took {elapsed:?} (expected <50ms)",
        );
        shutdown.cancel();
    }

    /// Verify a slow drain doesn't propagate latency back to the
    /// consumer: try_push must remain fast while the actor is blocked
    /// inside write_batch.
    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn slow_drain_doesnt_block_consumer() {
        struct SlowDrain {
            count: Arc<AtomicU64>,
        }
        impl SinkDrain<u32> for SlowDrain {
            async fn write_batch(&mut self, batch: Vec<u32>) -> Result<(), DrainError> {
                tokio::time::sleep(Duration::from_millis(50)).await;
                self.count.fetch_add(batch.len() as u64, Ordering::SeqCst);
                Ok(())
            }
        }

        let count = Arc::new(AtomicU64::new(0));
        let shutdown = CancellationToken::new();
        let cfg = BackgroundSinkConfig {
            queue_capacity: 10_000,
            batch_size: 16,
            flush_interval: Duration::from_millis(5),
            overflow: Overflow::Drop,
            metric_prefix: None,
        };
        let (sink, _handle) = BackgroundSink::spawn(
            SlowDrain {
                count: count.clone(),
            },
            cfg,
            shutdown.clone(),
        );

        // Push 200 messages while the drain takes 50ms per batch.
        // Consumer should NOT see 50ms latencies on try_push.
        let mut max_us: u128 = 0;
        for i in 0..200_u32 {
            let t0 = Instant::now();
            sink.try_push(i).expect("queue has space");
            let elapsed_us = t0.elapsed().as_micros();
            if elapsed_us > max_us {
                max_us = elapsed_us;
            }
        }
        assert!(
            max_us < 5_000,
            "max try_push latency was {max_us}µs — slow drain leaked back to consumer",
        );
        shutdown.cancel();
    }
}