proxima-primitives 0.1.0

The pipe algebra itself: the Pipe trait — an async In -> Result<Out, Err> that becomes a transform, source, sink or observe by choice of types — plus its three tier siblings (SendPipe, UnpinPipe, UnpinSendPipe), Handler, SourcePipe, the payload types Carry, ResponseStream and RequestStream (buffered bodies are plain Bytes), HeaderList, the sans-IO byte-stream traits, runtime-agnostic concurrency primitives, and the Replay/GenericStream transport seam. Everything else in proxima composes what is defined here.
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
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
use bytes::Bytes;
use std::borrow::Cow;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;

use crate::pipe::SendPipe;
use crate::pipe::capabilities::Clock;
pub use crate::pipe::capabilities::{ExceededAction, KeyOf};
use crate::pipe::primitives::Pipe;
use bon::Builder;
use conflaguration::{Settings, Validate, ValidationMessage};
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::pipe::bucket_table::BucketTable;
use crate::pipe::clock::TimeClock;
use crate::pipe::handler::{PipeHandle, ThreadLocalPipeHandle, into_handle};
use crate::pipe::labeled::Labeled;
use crate::pipe::pipe_factory::PipeFactory;
use crate::pipe::request::{Request, Response};
use crate::pipe::telemetry_surface::Labels;
use proxima_core::ProximaError;

const MICROS_PER_TOKEN: u64 = 1_000_000;
const NANOS_PER_SEC: u128 = 1_000_000_000;

const METRIC_REJECTED: &str = "proxima.rate_limit.rejected_total";
const METRIC_ADMITTED: &str = "proxima.rate_limit.admitted_total";

const MISSING_HEADER_KEY: &str = "__missing__";

// ── HTTP key extractor ────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub enum KeyExtractor {
    ConstantKey(String),
    Header(String),
    PathAndMethod,
}

impl KeyOf<KeyExtractor> for Request<Bytes> {
    type Rejection = Response<Bytes>;

    fn rate_key<'a>(&'a self, extractor: &'a KeyExtractor) -> Cow<'a, [u8]> {
        match extractor {
            KeyExtractor::ConstantKey(value) => Cow::Borrowed(value.as_bytes()),
            KeyExtractor::Header(name) => self
                .metadata
                .iter()
                .find(|(header_name, _)| header_name.as_ref().eq_ignore_ascii_case(name.as_bytes()))
                .map_or(
                    Cow::Borrowed(MISSING_HEADER_KEY.as_bytes()),
                    |(_, value)| Cow::Borrowed(value.as_ref()),
                ),
            KeyExtractor::PathAndMethod => {
                let mut buffer =
                    Vec::with_capacity(self.method.as_bytes().len() + 1 + self.path.len());
                buffer.extend_from_slice(self.method.as_bytes());
                buffer.push(b' ');
                buffer.extend_from_slice(&self.path);
                Cow::Owned(buffer)
            }
        }
    }

    fn build_rejection(action: &ExceededAction) -> Response<Bytes> {
        rejected_response(action)
    }
}

#[derive(Debug, Clone, Copy)]
pub struct TokenBucketConfig {
    pub capacity: u64,
    pub refill_per_sec: u64,
}

/// Caps on the per-key bucket map.
#[derive(Debug, Clone, Copy)]
pub struct RateLimitCaps {
    pub max_keys: usize,
    pub idle_ttl: Duration,
    pub sweep_period: Duration,
}

impl Default for RateLimitCaps {
    fn default() -> Self {
        Self {
            max_keys: 100_000,
            idle_ttl: Duration::from_secs(600),
            sweep_period: Duration::from_secs(30),
        }
    }
}

/// Token-bucket rate limiter. Generic over the inner handle, the extractor
/// type, AND the clock — mirrors `resilience::Retry<Inner, Clk>`'s injected-clock
/// seam so refill timing is deterministically testable. `Clk` defaults to
/// [`TimeClock`], the production monotonic clock, so every existing caller
/// (`RateLimit::new`, `RateLimitConfig`, `RateLimitFactory`) is unaffected.
///
/// Sans-IO: the limiter owns no runtime and spawns no task. Map growth is bounded
/// by `caps.max_keys` (LRU eviction on insert, inline on the call path). Proactive
/// reclamation of idle buckets is the host's job — call [`sweep_idle`] on whatever
/// cadence fits (read `caps.sweep_period` for the recommended interval) from a
/// `proxima_core::time::interval` loop on your own runtime. That keeps the primitive
/// runtime-agnostic (prime, tokio, embassy, bare-metal — it neither knows nor
/// cares which drives the host's tick).
///
/// [`sweep_idle`]: RateLimit::sweep_idle
pub struct RateLimit<Inner = PipeHandle, Extractor = KeyExtractor, Clk = TimeClock> {
    inner: Inner,
    config: TokenBucketConfig,
    extractor: Extractor,
    on_exceeded: ExceededAction,
    buckets: BucketTable<AtomicBucket>,
    caps: RateLimitCaps,
    clock: Clk,
}

// Hardcoded to `TimeClock` (not `Clk: Default`) so `RateLimit::new(...)` and
// `RateLimit::with_caps(...)` resolve without turbofish, the same way
// `HashMap::new()` pins its defaulted hasher param — a bare `Clk: Default`
// bound here would leave the type parameter ambiguous at every existing call
// site with no annotation to pin it.
impl<Inner, Extractor> RateLimit<Inner, Extractor, TimeClock> {
    #[must_use]
    pub fn new(inner: Inner, config: TokenBucketConfig, extractor: Extractor) -> Self {
        Self::with_caps(inner, config, extractor, RateLimitCaps::default())
    }

    #[must_use]
    pub fn with_caps(
        inner: Inner,
        config: TokenBucketConfig,
        extractor: Extractor,
        caps: RateLimitCaps,
    ) -> Self {
        Self::with_clock(inner, config, extractor, caps, TimeClock)
    }
}

impl<Inner, Extractor, Clk> RateLimit<Inner, Extractor, Clk> {
    /// Materialise with an explicit clock — the seam a deterministic test or
    /// example injects a fake clock through; production code goes via `new`
    /// / `with_caps`, which default `Clk` to [`TimeClock`].
    #[must_use]
    pub fn with_clock(
        inner: Inner,
        config: TokenBucketConfig,
        extractor: Extractor,
        caps: RateLimitCaps,
        clock: Clk,
    ) -> Self {
        let buckets: BucketTable<AtomicBucket> = BucketTable::with_max_keys(caps.max_keys);
        Self {
            inner,
            config,
            extractor,
            on_exceeded: ExceededAction::Reject429 {
                retry_after_ms: 1000,
            },
            buckets,
            caps,
            clock,
        }
    }

    // only called from `mod tests` below, which is itself `not(loom)`-gated.
    #[cfg(all(test, not(loom)))]
    pub(crate) fn bucket_count(&self) -> usize {
        self.buckets.len()
    }

    #[must_use]
    pub fn with_action(mut self, action: ExceededAction) -> Self {
        self.on_exceeded = action;
        self
    }
}

impl<Inner, Extractor, Clk: Clock> RateLimit<Inner, Extractor, Clk> {
    /// Evict buckets idle longer than `caps.idle_ttl`. Sans-IO: the host calls
    /// this on its own cadence (the limiter spawns nothing). Bounded growth does
    /// not depend on it — `caps.max_keys` already caps the map via LRU eviction
    /// on insert; this just reclaims idle memory sooner.
    pub fn sweep_idle(&self) {
        sweep_idle_at(
            &self.buckets,
            self.clock.now_nanos() / 1_000,
            self.caps.idle_ttl,
        );
    }

    pub(crate) fn bucket_for(&self, key: &[u8]) -> Arc<AtomicBucket> {
        // proactively cap before a possible insert; the table also self-defends
        // on a full wrap, but evicting here keeps `count <= max_keys`. when the
        // key already exists get_or_insert returns it unchanged, so an eviction
        // here only ever sheds a colder distinct key.
        if self.buckets.len() >= self.caps.max_keys {
            evict_lru(&self.buckets);
        }
        let config = self.config;
        let now_nanos = self.clock.now_nanos();
        let bucket = self
            .buckets
            .get_or_insert(key, || AtomicBucket::new(config, now_nanos));
        bucket.touch(now_nanos);
        bucket
    }
}

/// Serialisable key-extraction strategy — the config mirror of [`KeyExtractor`].
/// The `path_and_method` default matches the historical hand-parser.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "key", rename_all = "snake_case")]
pub enum KeyConfig {
    /// One shared bucket keyed by a constant string.
    Constant {
        #[serde(default = "default_constant_value", rename = "constant_value")]
        value: String,
    },
    /// Per-value bucket keyed by a request header.
    Header { header_name: String },
    /// Per-(method, path) bucket — the default.
    #[default]
    PathAndMethod,
}

fn default_constant_value() -> String {
    "__global__".to_string()
}

impl From<KeyConfig> for KeyExtractor {
    fn from(config: KeyConfig) -> Self {
        match config {
            KeyConfig::Constant { value } => KeyExtractor::ConstantKey(value),
            KeyConfig::Header { header_name } => KeyExtractor::Header(header_name),
            KeyConfig::PathAndMethod => KeyExtractor::PathAndMethod,
        }
    }
}

/// Serialisable caps on the per-key bucket map (durations in ms). Mirrors
/// [`RateLimitCaps`] with the same defaults.
#[derive(Debug, Clone, Copy, Builder, Serialize, Deserialize)]
#[builder(derive(Clone, Debug))]
pub struct CapsConfig {
    #[serde(default = "default_max_keys")]
    #[builder(default = default_max_keys())]
    pub max_keys: usize,
    #[serde(default = "default_idle_ttl_ms")]
    #[builder(default = default_idle_ttl_ms())]
    pub idle_ttl_ms: u64,
    #[serde(default = "default_sweep_ms")]
    #[builder(default = default_sweep_ms())]
    pub sweep_ms: u64,
}

fn default_max_keys() -> usize {
    100_000
}

fn default_idle_ttl_ms() -> u64 {
    600_000
}

fn default_sweep_ms() -> u64 {
    30_000
}

impl Default for CapsConfig {
    fn default() -> Self {
        Self {
            max_keys: default_max_keys(),
            idle_ttl_ms: default_idle_ttl_ms(),
            sweep_ms: default_sweep_ms(),
        }
    }
}

impl From<CapsConfig> for RateLimitCaps {
    fn from(config: CapsConfig) -> Self {
        Self {
            max_keys: config.max_keys,
            idle_ttl: Duration::from_millis(config.idle_ttl_ms),
            sweep_period: Duration::from_millis(config.sweep_ms),
        }
    }
}

/// Typed config surface for the `rate_limit` middleware (token bucket).
#[derive(Debug, Clone, Builder, Deserialize, Serialize, Settings)]
#[settings(prefix = "PROXIMA_RATE_LIMIT")]
#[builder(derive(Clone, Debug))]
pub struct RateLimitConfig {
    /// Token bucket capacity (burst). Required.
    #[setting(default)]
    #[serde(default)]
    #[builder(default)]
    pub capacity: u64,

    /// Steady-state refill rate in tokens per second. Required.
    #[setting(default)]
    #[serde(default)]
    #[builder(default)]
    pub refill_per_sec: u64,

    /// Key-extraction strategy. Flattened so `key = "header"` + `header_name`
    /// sit at the top level, matching the historical spec.
    #[setting(skip)]
    #[serde(default, flatten)]
    #[builder(default)]
    pub key: KeyConfig,

    /// Per-key bucket-map caps.
    #[setting(skip)]
    #[serde(default)]
    #[builder(default)]
    pub caps: CapsConfig,

    /// `Retry-After` (ms) advertised on a 429. `None` keeps the limiter default.
    #[setting(default)]
    #[serde(default)]
    pub retry_after_ms: Option<u64>,
}

impl Validate for RateLimitConfig {
    fn validate(&self) -> conflaguration::Result<()> {
        let mut errors = Vec::new();
        if self.capacity == 0 {
            errors.push(ValidationMessage::new(
                "capacity",
                "rate_limit.capacity required",
            ));
        }
        if self.refill_per_sec == 0 {
            errors.push(ValidationMessage::new(
                "refill_per_sec",
                "rate_limit.refill_per_sec required",
            ));
        }
        if errors.is_empty() {
            Ok(())
        } else {
            Err(conflaguration::Error::Validation { errors })
        }
    }
}

impl RateLimitConfig {
    /// Materialise the limiter around `inner`, on the production [`TimeClock`].
    pub fn from_config(
        self,
        inner: PipeHandle,
    ) -> Result<RateLimit<PipeHandle, KeyExtractor, TimeClock>, ProximaError> {
        self.validate()
            .map_err(|err| ProximaError::Config(format!("{err}")))?;
        let mut limiter = RateLimit::with_caps(
            inner,
            TokenBucketConfig {
                capacity: self.capacity,
                refill_per_sec: self.refill_per_sec,
            },
            self.key.into(),
            self.caps.into(),
        );
        if let Some(retry_after) = self.retry_after_ms {
            limiter.on_exceeded = ExceededAction::Reject429 {
                retry_after_ms: retry_after,
            };
        }
        Ok(limiter)
    }
}

impl RateLimit<PipeHandle, KeyExtractor, TimeClock> {
    pub fn from_spec(inner: PipeHandle, value: &Value) -> Result<Self, ProximaError> {
        let config: RateLimitConfig = serde_json::from_value(value.clone())
            .map_err(|err| ProximaError::Config(format!("rate_limit config: {err}")))?;
        config.from_config(inner)
    }
}

fn evict_lru(buckets: &BucketTable<AtomicBucket>) {
    buckets.evict_one_lru(|bucket| bucket.last_access_micros.load(Ordering::Relaxed));
}

fn sweep_idle_at(buckets: &BucketTable<AtomicBucket>, now_micros: u64, idle_ttl: Duration) {
    let ttl_micros = idle_ttl.as_micros() as u64;
    buckets.sweep_idle(now_micros, ttl_micros, |bucket| {
        bucket.last_access_micros.load(Ordering::Relaxed)
    });
}

async fn run_rate_limit<In, Out, Err, Key, Call, Fut>(
    args: RunArgs<In, Key>,
    call_inner: Call,
) -> Result<Out, Err>
where
    In: KeyOf<Key, Rejection = Out> + Labeled,
    Key: Send + Sync,
    Call: Fn(In) -> Fut,
    Fut: Future<Output = Result<Out, Err>>,
{
    let RunArgs {
        extractor,
        input,
        bucket,
        action,
        now_nanos,
    } = args;
    let telemetry = input.telemetry();
    let base_labels = input.labels();
    let key = input.rate_key(&extractor);
    let admitted = bucket.try_take(MICROS_PER_TOKEN, now_nanos);
    let label_key_str = std::str::from_utf8(key.as_ref()).unwrap_or("");
    let labels = with_extra(&base_labels, "key", label_key_str);
    if admitted {
        telemetry.counter_inc(METRIC_ADMITTED, &labels, 1);
        call_inner(input).await
    } else {
        telemetry.counter_inc(METRIC_REJECTED, &labels, 1);
        Ok(In::build_rejection(&action))
    }
}

struct RunArgs<In, Key> {
    extractor: Key,
    input: In,
    bucket: Arc<AtomicBucket>,
    action: ExceededAction,
    now_nanos: u64,
}

impl<Inner, Extractor, Clk> SendPipe for RateLimit<Inner, Extractor, Clk>
where
    Inner: SendPipe + Clone + Send + Sync + 'static,
    Inner::In: KeyOf<Extractor, Rejection = Inner::Out> + Labeled + Send + 'static,
    Inner::Out: Send + 'static,
    Inner::Err: Send + 'static,
    Extractor: Clone + Send + Sync + 'static,
    Clk: Clock + Send + Sync + 'static,
{
    type In = Inner::In;
    type Out = Inner::Out;
    type Err = Inner::Err;

    fn call(
        &self,
        input: Inner::In,
    ) -> impl Future<Output = Result<Inner::Out, Inner::Err>> + Send {
        let bucket = self.bucket_for(input.rate_key(&self.extractor).as_ref());
        let now_nanos = self.clock.now_nanos();
        let inner = self.inner.clone();
        run_rate_limit(
            RunArgs {
                extractor: self.extractor.clone(),
                input,
                bucket,
                action: self.on_exceeded,
                now_nanos,
            },
            move |admitted| {
                let inner = inner.clone();
                async move { SendPipe::call(&inner, admitted).await }
            },
        )
    }
}

impl<Extractor, Clk> Pipe for RateLimit<ThreadLocalPipeHandle, Extractor, Clk>
where
    Request<Bytes>: KeyOf<Extractor, Rejection = Response<Bytes>>,
    Extractor: Clone + Send + Sync + 'static,
    Clk: Clock + Send + Sync + 'static,
{
    type In = Request<Bytes>;
    type Out = Response<Bytes>;
    type Err = ProximaError;

    fn call(
        &self,
        input: Request<Bytes>,
    ) -> impl Future<Output = Result<Response<Bytes>, ProximaError>> {
        let bucket = self.bucket_for(input.rate_key(&self.extractor).as_ref());
        let now_nanos = self.clock.now_nanos();
        let inner = self.inner.clone();
        run_rate_limit(
            RunArgs {
                extractor: self.extractor.clone(),
                input,
                bucket,
                action: self.on_exceeded,
                now_nanos,
            },
            move |admitted| {
                let inner = inner.clone();
                async move { Pipe::call(&inner, admitted).await }
            },
        )
    }
}

fn rejected_response(action: &ExceededAction) -> Response<Bytes> {
    match action {
        ExceededAction::Reject429 { retry_after_ms } => {
            let retry_after_seconds = retry_after_ms.div_ceil(1000).max(1);
            Response::new(429)
                .with_header("retry-after", retry_after_seconds.to_string())
                .with_body("rate limit exceeded")
        }
    }
}

fn with_extra(base: &Labels, key: &str, value: &str) -> Labels {
    let mut pairs: Vec<(String, String)> = base.entries().to_vec();
    pairs.push((key.to_string(), value.to_string()));
    let pair_refs: Vec<(&str, &str)> = pairs
        .iter()
        .map(|(name, value)| (name.as_str(), value.as_str()))
        .collect();
    Labels::from_pairs(&pair_refs)
}

pub struct RateLimitFactory;

impl PipeFactory for RateLimitFactory {
    fn name(&self) -> &str {
        "rate_limit"
    }

    fn build(
        &self,
        spec: &Value,
        inner: Option<PipeHandle>,
    ) -> Pin<Box<dyn Future<Output = Result<PipeHandle, ProximaError>> + Send + '_>> {
        let spec = spec.clone();
        Box::pin(async move {
            let inner = inner
                .ok_or_else(|| ProximaError::Config("rate_limit requires an inner pipe".into()))?;
            let limiter = RateLimit::from_spec(inner, &spec)?;
            Ok(into_handle(limiter))
        })
    }
}

pub(crate) struct AtomicBucket {
    tokens_micro: AtomicU64,
    last_refill_ns: AtomicU64,
    pub(crate) last_access_micros: AtomicU64,
    capacity_micro: u64,
    refill_per_sec_micro: u64,
}

impl AtomicBucket {
    /// `now_nanos` comes from the injected [`Clock`] — the bucket itself holds
    /// no clock and reads no real time, so its refill math is deterministic
    /// under a fake clock.
    fn new(config: TokenBucketConfig, now_nanos: u64) -> Self {
        let capacity_micro = config.capacity.saturating_mul(MICROS_PER_TOKEN);
        let refill_per_sec_micro = config.refill_per_sec.saturating_mul(MICROS_PER_TOKEN);
        Self {
            tokens_micro: AtomicU64::new(capacity_micro),
            last_refill_ns: AtomicU64::new(now_nanos),
            last_access_micros: AtomicU64::new(now_nanos / 1_000),
            capacity_micro,
            refill_per_sec_micro,
        }
    }

    fn touch(&self, now_nanos: u64) {
        self.last_access_micros
            .store(now_nanos / 1_000, Ordering::Relaxed);
    }

    // `now_nanos` is read once per call by the caller (the injected Clock),
    // not re-read on each CAS retry below — a CAS spin is contention on this
    // same bucket, not a new logical "check", so one timestamp covers it.
    pub(crate) fn try_take(&self, cost_micro: u64, now_nanos: u64) -> bool {
        loop {
            let last_ns = self.last_refill_ns.load(Ordering::Acquire);
            let current = self.tokens_micro.load(Ordering::Acquire);
            let elapsed_ns = now_nanos.saturating_sub(last_ns);
            let added_micro = (u128::from(elapsed_ns) * u128::from(self.refill_per_sec_micro)
                / NANOS_PER_SEC) as u64;
            let refilled = current.saturating_add(added_micro).min(self.capacity_micro);
            if refilled < cost_micro {
                if added_micro > 0
                    && self
                        .tokens_micro
                        .compare_exchange(current, refilled, Ordering::Release, Ordering::Acquire)
                        .is_ok()
                {
                    self.last_refill_ns.store(now_nanos, Ordering::Release);
                }
                return false;
            }
            let after = refilled - cost_micro;
            if self
                .tokens_micro
                .compare_exchange(current, after, Ordering::Release, Ordering::Acquire)
                .is_ok()
            {
                self.last_refill_ns.store(now_nanos, Ordering::Release);
                return true;
            }
        }
    }
}

// `#[proxima::test]` pulls in the `proxima` dev-dependency, which the
// loom build keeps out of the graph (see
// `[target.'cfg(not(loom))'.dev-dependencies]` in Cargo.toml); these
// tests are unrelated to the Notify/watch loom protocol.
#[cfg(all(test, not(loom)))]
#[allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::field_reassign_with_default,
    clippy::type_complexity,
    clippy::useless_vec,
    clippy::needless_range_loop,
    clippy::default_constructed_unit_structs
)]
mod tests {
    use std::collections::HashMap;
    use std::future::Future;
    use std::sync::Mutex;

    use super::*;
    use crate::pipe::handler::into_handle;
    use crate::pipe::telemetry_surface::{Telemetry, TelemetryHandle};

    #[derive(Default)]
    struct Metrics {
        counters: Mutex<HashMap<(String, Vec<(String, String)>), u64>>,
    }

    impl Telemetry for Metrics {
        fn counter_inc(&self, metric: &str, labels: &Labels, by: u64) {
            let key = (metric.to_string(), labels.entries().to_vec());
            *self.counters.lock().unwrap().entry(key).or_insert(0) += by;
        }

        fn gauge_set(&self, _: &str, _: &Labels, _: i64) {}

        fn histogram_record(&self, _: &str, _: &Labels, _: f64) {}
    }

    impl Metrics {
        fn counter(&self, metric: &str, labels: &Labels) -> Option<u64> {
            let key = (metric.to_string(), labels.entries().to_vec());
            self.counters.lock().unwrap().get(&key).copied()
        }
    }

    struct AlwaysOk;

    impl SendPipe for AlwaysOk {
        type In = Request<Bytes>;
        type Out = Response<Bytes>;
        type Err = ProximaError;

        fn call(
            &self,
            _request: Request<Bytes>,
        ) -> impl Future<Output = Result<Response<Bytes>, ProximaError>> + Send {
            async { Ok(Response::ok("ok")) }
        }
    }

    fn build_request_with_metrics(method: &str, path: &str) -> (Request<Bytes>, Arc<Metrics>) {
        let metrics: Arc<Metrics> = Arc::new(Metrics::default());
        let telemetry: TelemetryHandle = metrics.clone();
        let request = Request::builder()
            .method(method)
            .path(path)
            .telemetry(telemetry)
            .build()
            .expect("builder");
        (request, metrics)
    }

    // principle-4 parity: the fluent builder and the config value must lower to
    // identical RateLimit state (token config, extractor, caps, exceeded action).
    #[test]
    fn parity_fluent_builder_and_config_value_match() {
        let from_value: RateLimitConfig = serde_json::from_value(serde_json::json!({
            "capacity": 10,
            "refill_per_sec": 5,
            "key": "header",
            "header_name": "x-api-key",
            "caps": {"max_keys": 1000, "idle_ttl_ms": 60000, "sweep_ms": 5000},
            "retry_after_ms": 2000,
        }))
        .expect("from_value");
        let from_value = from_value
            .from_config(into_handle(AlwaysOk))
            .expect("from_config value");

        let from_builder = RateLimitConfig::builder()
            .capacity(10)
            .refill_per_sec(5)
            .key(KeyConfig::Header {
                header_name: "x-api-key".to_string(),
            })
            .caps(
                CapsConfig::builder()
                    .max_keys(1000)
                    .idle_ttl_ms(60000)
                    .sweep_ms(5000)
                    .build(),
            )
            .retry_after_ms(2000)
            .build()
            .from_config(into_handle(AlwaysOk))
            .expect("from_config builder");

        assert_eq!(from_value.config.capacity, from_builder.config.capacity);
        assert_eq!(
            from_value.config.refill_per_sec,
            from_builder.config.refill_per_sec
        );
        assert_eq!(from_value.caps.max_keys, from_builder.caps.max_keys);
        assert_eq!(from_value.caps.idle_ttl, from_builder.caps.idle_ttl);
        assert_eq!(from_value.caps.sweep_period, from_builder.caps.sweep_period);
        assert_eq!(
            format!("{:?}", from_value.extractor),
            format!("{:?}", from_builder.extractor)
        );
        assert_eq!(
            format!("{:?}", from_value.on_exceeded),
            format!("{:?}", from_builder.on_exceeded)
        );
    }

    #[proxima::test]
    async fn token_bucket_admits_until_empty_then_rejects() {
        let stack = RateLimit::new(
            into_handle(AlwaysOk),
            TokenBucketConfig {
                capacity: 2,
                refill_per_sec: 0,
            },
            KeyExtractor::ConstantKey("global".into()),
        );

        for _ in 0..2 {
            let (request, _metrics) = build_request_with_metrics("GET", "/");
            let response = SendPipe::call(&stack, request).await.expect("call");
            assert_eq!(response.status, 200);
        }
        let (request, _metrics) = build_request_with_metrics("GET", "/");
        let response = SendPipe::call(&stack, request).await.expect("call");
        assert_eq!(response.status, 429);
        assert_eq!(
            response.metadata.get_str("retry-after"),
            Some("1"),
            "retry-after must be present and round up to seconds",
        );
    }

    #[proxima::test]
    async fn distinct_keys_have_independent_buckets() {
        let stack = RateLimit::new(
            into_handle(AlwaysOk),
            TokenBucketConfig {
                capacity: 1,
                refill_per_sec: 0,
            },
            KeyExtractor::PathAndMethod,
        );

        let (request_a, _metrics_a) = build_request_with_metrics("GET", "/a");
        let (request_b, _metrics_b) = build_request_with_metrics("GET", "/b");
        assert_eq!(
            SendPipe::call(&stack, request_a).await.expect("a").status,
            200
        );
        assert_eq!(
            SendPipe::call(&stack, request_b).await.expect("b").status,
            200
        );

        let (request_a_again, _) = build_request_with_metrics("GET", "/a");
        assert_eq!(
            SendPipe::call(&stack, request_a_again)
                .await
                .expect("a again")
                .status,
            429
        );
    }

    #[proxima::test]
    async fn reject_increments_telemetry_counter() {
        let stack = RateLimit::new(
            into_handle(AlwaysOk),
            TokenBucketConfig {
                capacity: 1,
                refill_per_sec: 0,
            },
            KeyExtractor::ConstantKey("global".into()),
        );

        let metrics: Arc<Metrics> = Arc::new(Metrics::default());
        let telemetry: TelemetryHandle = metrics.clone();
        let one = Request::builder()
            .method("GET")
            .path("/")
            .telemetry(telemetry.clone())
            .build()
            .expect("builder one");
        let two = Request::builder()
            .method("GET")
            .path("/")
            .telemetry(telemetry)
            .build()
            .expect("builder two");

        let _ = SendPipe::call(&stack, one).await.expect("first admitted");
        let _ = SendPipe::call(&stack, two).await.expect("second rejected");

        let labels = Labels::from_pairs(&[("key", "global")]);
        let rejected = metrics.counter(METRIC_REJECTED, &labels);
        assert_eq!(rejected, Some(1));
    }

    #[proxima::test]
    async fn max_keys_cap_evicts_lru_on_insert() {
        let limiter = RateLimit::with_caps(
            into_handle(AlwaysOk),
            TokenBucketConfig {
                capacity: 1,
                refill_per_sec: 0,
            },
            KeyExtractor::Header("x-tenant".into()),
            RateLimitCaps {
                max_keys: 2,
                idle_ttl: Duration::from_secs(600),
                sweep_period: Duration::from_secs(60),
            },
        );

        let mut original_a: Option<Arc<AtomicBucket>> = None;
        for tenant in ["a", "b", "c"] {
            let metrics: Arc<Metrics> = Arc::new(Metrics::default());
            let telemetry: TelemetryHandle = metrics.clone();
            let request = Request::builder()
                .method("GET")
                .path("/")
                .header("x-tenant", tenant)
                .telemetry(telemetry)
                .build()
                .expect("builder");
            let bucket = limiter.bucket_for(tenant.as_bytes());
            if tenant == "a" {
                original_a = Some(bucket);
            }
            std::hint::black_box(request);
        }
        assert_eq!(
            limiter.bucket_count(),
            2,
            "max_keys cap must evict on insert"
        );
        // a was the lru victim before c's insert; reinserting it makes a fresh
        // bucket (distinct Arc), proving the original was evicted.
        let reinserted_a = limiter.bucket_for(b"a");
        assert!(
            !Arc::ptr_eq(&reinserted_a, original_a.as_ref().expect("a inserted")),
            "a was evicted; reinsert yields a fresh bucket"
        );
    }

    #[test]
    fn sweep_idle_at_evicts_only_buckets_past_ttl() {
        // injected clock -> deterministic, no sleep. fresh bucket touched "now",
        // stale bucket last seen long ago; sweep at now with a 1s ttl drops only stale.
        let buckets: BucketTable<AtomicBucket> = BucketTable::with_max_keys(8);
        let config = TokenBucketConfig {
            capacity: 1,
            refill_per_sec: 1,
        };
        let now_micros = 10_000_000;
        let idle_ttl = Duration::from_secs(1);

        let fresh = buckets.get_or_insert(b"fresh", || AtomicBucket::new(config, 0));
        fresh
            .last_access_micros
            .store(now_micros, Ordering::Relaxed);

        let stale = buckets.get_or_insert(b"stale", || AtomicBucket::new(config, 0));
        stale
            .last_access_micros
            .store(now_micros - 5_000_000, Ordering::Relaxed);

        sweep_idle_at(&buckets, now_micros, idle_ttl);

        assert_eq!(
            buckets.len(),
            1,
            "only the stale bucket past ttl is evicted"
        );
        let fresh_again = buckets.get_or_insert(b"fresh", || AtomicBucket::new(config, 0));
        assert!(
            Arc::ptr_eq(&fresh_again, &fresh),
            "fresh bucket survives unchanged"
        );
        let stale_again = buckets.get_or_insert(b"stale", || AtomicBucket::new(config, 0));
        assert!(
            !Arc::ptr_eq(&stale_again, &stale),
            "stale was evicted, fresh on reinsert"
        );
    }

    #[test]
    fn key_of_constant_returns_borrowed_slice() {
        let extractor = KeyExtractor::ConstantKey("tenant-42".into());
        let request = Request::builder()
            .method("GET")
            .path("/")
            .build()
            .expect("builder");
        match request.rate_key(&extractor) {
            Cow::Borrowed(value) => assert_eq!(value, b"tenant-42"),
            Cow::Owned(_) => panic!("ConstantKey must not allocate"),
        }
    }

    #[test]
    fn key_of_header_borrows_from_request_value() {
        let extractor = KeyExtractor::Header("x-api-key".into());
        let request = Request::builder()
            .method("GET")
            .path("/")
            .header("X-API-Key", "abc123")
            .build()
            .expect("builder");
        match request.rate_key(&extractor) {
            Cow::Borrowed(value) => assert_eq!(value, b"abc123"),
            Cow::Owned(_) => panic!("Header extractor must borrow from the request"),
        }
    }

    #[test]
    fn key_of_path_and_method_owns_the_synthesized_key() {
        let extractor = KeyExtractor::PathAndMethod;
        let request = Request::builder()
            .method("GET")
            .path("/v1/items/42")
            .build()
            .expect("builder");
        match request.rate_key(&extractor) {
            Cow::Borrowed(_) => panic!("PathAndMethod must synthesize a new String"),
            Cow::Owned(value) => assert_eq!(value, b"GET /v1/items/42"),
        }
    }

    #[test]
    fn parse_caps_uses_defaults_when_block_absent() {
        let caps: RateLimitCaps = CapsConfig::default().into();
        assert_eq!(caps.max_keys, 100_000);
        assert_eq!(caps.idle_ttl, Duration::from_secs(600));
    }

    #[test]
    fn parse_caps_overrides_individual_fields() {
        let value = serde_json::json!({"max_keys": 16, "idle_ttl_ms": 5000, "sweep_ms": 1000});
        let config: CapsConfig = serde_json::from_value(value).expect("parse");
        let caps: RateLimitCaps = config.into();
        assert_eq!(caps.max_keys, 16);
        assert_eq!(caps.idle_ttl, Duration::from_millis(5000));
        assert_eq!(caps.sweep_period, Duration::from_millis(1000));
    }

    #[derive(Clone, PartialEq, Debug)]
    struct TenantEvent {
        tenant: String,
        value: u64,
    }

    #[derive(Clone)]
    struct TenantExtractor;

    impl KeyOf<TenantExtractor> for TenantEvent {
        type Rejection = TenantEvent;

        fn rate_key<'a>(&'a self, _extractor: &'a TenantExtractor) -> Cow<'a, [u8]> {
            Cow::Borrowed(self.tenant.as_bytes())
        }

        fn build_rejection(_action: &ExceededAction) -> TenantEvent {
            TenantEvent {
                tenant: "__rate_limited__".into(),
                value: 0,
            }
        }
    }

    impl Labeled for TenantEvent {
        fn telemetry(&self) -> TelemetryHandle {
            Arc::new(Metrics::default())
        }

        fn labels(&self) -> Labels {
            Labels::empty()
        }
    }

    #[derive(Clone)]
    struct EventPassthrough;

    impl SendPipe for EventPassthrough {
        type In = TenantEvent;
        type Out = TenantEvent;
        type Err = ProximaError;

        fn call(
            &self,
            input: TenantEvent,
        ) -> impl Future<Output = Result<TenantEvent, ProximaError>> + Send {
            async move {
                Ok(TenantEvent {
                    value: input.value + 1,
                    ..input
                })
            }
        }
    }

    #[proxima::test]
    async fn rate_limit_is_generic_over_a_non_request_key_type() {
        let stack = RateLimit::new(
            EventPassthrough,
            TokenBucketConfig {
                capacity: 1,
                refill_per_sec: 0,
            },
            TenantExtractor,
        );

        let admitted = SendPipe::call(
            &stack,
            TenantEvent {
                tenant: "acme".into(),
                value: 10,
            },
        )
        .await
        .expect("first event admitted");
        assert_eq!(admitted.value, 11, "passthrough increments value");

        let limited = SendPipe::call(
            &stack,
            TenantEvent {
                tenant: "acme".into(),
                value: 10,
            },
        )
        .await
        .expect("second event returns Ok with sentinel");
        assert_eq!(
            limited.tenant, "__rate_limited__",
            "exhausted bucket returns build_rejection sentinel"
        );
        assert_eq!(limited.value, 0);

        let other = SendPipe::call(
            &stack,
            TenantEvent {
                tenant: "beta".into(),
                value: 5,
            },
        )
        .await
        .expect("different tenant has its own bucket");
        assert_eq!(other.value, 6, "beta bucket is independent");
    }

    // ── Clock-injectable determinism ────────────────────────────────────────
    //
    // `RateLimit::with_clock` takes any `Clock` impl, so refill can be proven
    // deterministically — `advance` moves virtual time, never a real sleep.
    // `RecordingClock` is the canonical double for this (see
    // `crate::pipe::clock::testing` for why it, not `MockClock`).

    use crate::pipe::clock::testing::RecordingClock;

    #[proxima::test]
    async fn fake_clock_drives_fill_exhaust_advance_refill_exhaust() {
        let clock = RecordingClock::new();
        let stack = RateLimit::with_clock(
            into_handle(AlwaysOk),
            TokenBucketConfig {
                capacity: 2,
                refill_per_sec: 1,
            },
            KeyExtractor::ConstantKey("global".into()),
            RateLimitCaps::default(),
            clock.clone(),
        );

        // fill -> exhaust: capacity is 2, so two calls admit and a third refuses.
        for attempt in 0..2 {
            let (request, _metrics) = build_request_with_metrics("GET", "/");
            let response = SendPipe::call(&stack, request).await.expect("call");
            assert_eq!(response.status, 200, "attempt {attempt} within capacity");
        }
        let (request, _metrics) = build_request_with_metrics("GET", "/");
        let exhausted = SendPipe::call(&stack, request).await.expect("call");
        assert_eq!(exhausted.status, 429, "bucket exhausted, no time elapsed");

        // no clock advance yet: still exhausted at the same instant.
        let (request, _metrics) = build_request_with_metrics("GET", "/");
        let still_exhausted = SendPipe::call(&stack, request).await.expect("call");
        assert_eq!(
            still_exhausted.status, 429,
            "no wall-clock time passed, no fake-clock advance either"
        );

        // advance -> refill resumes: refill_per_sec=1 over 1s refills one token.
        clock.advance(Duration::from_secs(1));
        let (request, _metrics) = build_request_with_metrics("GET", "/");
        let resumed = SendPipe::call(&stack, request).await.expect("call");
        assert_eq!(resumed.status, 200, "1s advance refills exactly one token");

        // exhaust again at the same instant: that one refilled token is spent.
        let (request, _metrics) = build_request_with_metrics("GET", "/");
        let exhausted_again = SendPipe::call(&stack, request).await.expect("call");
        assert_eq!(
            exhausted_again.status, 429,
            "the refilled token was just spent; no further clock advance"
        );
    }

    #[proxima::test]
    async fn fake_clock_never_refills_when_no_time_advances() {
        let clock = RecordingClock::new();
        let stack = RateLimit::with_clock(
            into_handle(AlwaysOk),
            TokenBucketConfig {
                capacity: 1,
                refill_per_sec: 1_000_000,
            },
            KeyExtractor::ConstantKey("global".into()),
            RateLimitCaps::default(),
            clock,
        );

        let (first, _metrics) = build_request_with_metrics("GET", "/");
        let admitted = SendPipe::call(&stack, first).await.expect("call");
        assert_eq!(admitted.status, 200);

        // even a generous refill rate cannot admit a second call: the clock
        // never advanced, so try_take's elapsed_ns is exactly zero.
        for _ in 0..5 {
            let (request, _metrics) = build_request_with_metrics("GET", "/");
            let response = SendPipe::call(&stack, request).await.expect("call");
            assert_eq!(
                response.status, 429,
                "no clock advance means no refill, regardless of call count"
            );
        }
    }
}