axum-limit 0.1.0

Production-oriented rate limiting for Axum via extractors, with pluggable algorithms, storage backends, and dynamic quotas.
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
#![doc = include_str!("../README.md")]
#![deny(unsafe_code, missing_docs, clippy::unwrap_used)]

mod backend;
mod codec;
mod key;
mod policy;
mod quota;
mod quota_source;
mod snapshot;
mod time;

use axum_core::extract::{FromRef, FromRequestParts};
use axum_core::response::{IntoResponse, Response};
use http::request::Parts;
use http::StatusCode;
use std::convert::Infallible;
use std::error::Error;
use std::fmt::Display;
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use time::now_ms;

#[cfg(feature = "redis")]
pub use backend::RedisBackend;
pub use backend::{
    apply_policy, build_storage_key, BackendError, MemoryBackend, RateLimitBackend, StorageKey,
};
pub use codec::CodecError;
pub use policy::{
    FixedWindowPolicy, PolicyState, RateLimitPolicy, SlidingWindowPolicy, TokenBucketPolicy,
};
pub use quota::{Quota, QuotaFingerprint};
pub use quota_source::{FixedQuota, QuotaSource};
pub use snapshot::{RateLimitInfo, RateLimitSnapshot};

macro_rules! define_limit_extractor {
    (
        $(#[$struct_meta:meta])*
        $name:ident => $policy:ty
    ) => {
        $(#[$struct_meta])*
        #[derive(Debug, Clone, Copy, Default)]
        pub struct $name<const COUNT: usize, const PER: u64, K, B = MemoryBackend>(
            pub K::Extractor,
            pub std::marker::PhantomData<B>,
        )
        where
            K: Key,
            B: RateLimitBackend;

        impl<const COUNT: usize, const PER: u64, K, B> AsRef<K::Extractor> for $name<COUNT, PER, K, B>
        where
            K: Key,
            B: RateLimitBackend,
        {
            fn as_ref(&self) -> &K::Extractor {
                &self.0
            }
        }

        impl<const COUNT: usize, const PER: u64, K, B> AsMut<K::Extractor> for $name<COUNT, PER, K, B>
        where
            K: Key,
            B: RateLimitBackend,
        {
            fn as_mut(&mut self) -> &mut K::Extractor {
                &mut self.0
            }
        }

        impl<const COUNT: usize, const PER: u64, K, B> Deref for $name<COUNT, PER, K, B>
        where
            K: Key,
            B: RateLimitBackend,
        {
            type Target = K::Extractor;

            fn deref(&self) -> &Self::Target {
                &self.0
            }
        }

        impl<const COUNT: usize, const PER: u64, K, B> DerefMut for $name<COUNT, PER, K, B>
        where
            K: Key,
            B: RateLimitBackend,
        {
            fn deref_mut(&mut self) -> &mut Self::Target {
                &mut self.0
            }
        }

        impl<const COUNT: usize, const PER: u64, K, B> Display for $name<COUNT, PER, K, B>
        where
            K: Key,
            B: RateLimitBackend,
            K::Extractor: Display,
        {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                self.0.fmt(f)
            }
        }

        impl<const COUNT: usize, const PER: u64, K, B> $name<COUNT, PER, K, B>
        where
            K: Key,
            B: RateLimitBackend,
        {
            /// Returns the count of requests allowed within the specified period.
            pub const fn count() -> usize {
                COUNT
            }

            /// Returns the period (in milliseconds) for which the limit applies.
            pub const fn per() -> u64 {
                PER
            }

            /// Returns the quota configured by this extractor.
            pub const fn quota() -> Quota {
                Quota::new(COUNT, PER)
            }

            /// Consumes the limit and returns the inner extractor.
            pub fn into_inner(self) -> K::Extractor {
                self.0
            }
        }

        impl<const C: usize, const P: u64, K, B, S> FromRequestParts<S> for $name<C, P, K, B>
        where
            B: RateLimitBackend,
            B::Error: Display,
            S: Send + Sync,
            K: Key + StorageKey,
            K::Extractor: FromRequestParts<S> + Send,
            LimitState<K, $policy, B>: FromRef<S>,
        {
            type Rejection =
                LimitRejection<<<K as Key>::Extractor as FromRequestParts<S>>::Rejection>;

            fn from_request_parts(
                parts: &mut Parts,
                state: &S,
            ) -> impl std::future::Future<Output = Result<Self, Self::Rejection>> + Send {
                async move {
                    let key_extractor = match K::Extractor::from_request_parts(parts, state).await {
                        Ok(ke) => ke,
                        Err(rejection) => {
                            return Err(LimitRejection::KeyExtractionFailure(rejection));
                        }
                    };

                    let limit_state: LimitState<K, $policy, B> = FromRef::from_ref(state);
                    let key = K::from_extractor(&key_extractor);
                    let snapshot = limit_state
                        .check(key, Quota::new(C, P))
                        .await
                        .map_err(|error| LimitRejection::Backend(error.to_string()))?;

                    if snapshot.allowed {
                        parts.extensions.insert(RateLimitInfo(snapshot));
                        Ok(Self(key_extractor, std::marker::PhantomData))
                    } else {
                        Err(LimitRejection::RateLimitExceeded(snapshot))
                    }
                }
            }
        }
    };
}

define_limit_extractor! {
    /// Token-bucket rate limit extractor.
    Limit => TokenBucketPolicy
}

define_limit_extractor! {
    /// Fixed-window rate limit extractor.
    FixedWindowLimit => FixedWindowPolicy
}

define_limit_extractor! {
    /// Sliding-window rate limit extractor.
    SlidingWindowLimit => SlidingWindowPolicy
}

macro_rules! define_dynamic_limit_extractor {
    (
        $(#[$struct_meta:meta])*
        $name:ident => $policy:ty
    ) => {
        $(#[$struct_meta])*
        #[derive(Debug, Clone)]
        pub struct $name<K, Q, B = MemoryBackend>(
            pub K::Extractor,
            pub Quota,
            pub std::marker::PhantomData<(Q, B)>,
        )
        where
            K: Key,
            B: RateLimitBackend;

        impl<K, Q, B> AsRef<K::Extractor> for $name<K, Q, B>
        where
            K: Key,
            B: RateLimitBackend,
        {
            fn as_ref(&self) -> &K::Extractor {
                &self.0
            }
        }

        impl<K, Q, B> AsMut<K::Extractor> for $name<K, Q, B>
        where
            K: Key,
            B: RateLimitBackend,
        {
            fn as_mut(&mut self) -> &mut K::Extractor {
                &mut self.0
            }
        }

        impl<K, Q, B> Deref for $name<K, Q, B>
        where
            K: Key,
            B: RateLimitBackend,
        {
            type Target = K::Extractor;

            fn deref(&self) -> &Self::Target {
                &self.0
            }
        }

        impl<K, Q, B> DerefMut for $name<K, Q, B>
        where
            K: Key,
            B: RateLimitBackend,
        {
            fn deref_mut(&mut self) -> &mut Self::Target {
                &mut self.0
            }
        }

        impl<K, Q, B> Display for $name<K, Q, B>
        where
            K: Key,
            B: RateLimitBackend,
            K::Extractor: Display,
        {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                self.0.fmt(f)
            }
        }

        impl<K, Q, B> $name<K, Q, B>
        where
            K: Key,
            B: RateLimitBackend,
        {
            /// Returns the quota resolved for this request.
            pub fn resolved_quota(&self) -> Quota {
                self.1
            }

            /// Consumes the limit and returns the inner extractor and resolved quota.
            pub fn into_parts(self) -> (K::Extractor, Quota) {
                (self.0, self.1)
            }

            /// Consumes the limit and returns the inner extractor.
            pub fn into_inner(self) -> K::Extractor {
                self.0
            }
        }

        impl<K, Q, B, S> FromRequestParts<S> for $name<K, Q, B>
        where
            B: RateLimitBackend,
            B::Error: Display,
            S: Send + Sync,
            K: Key + StorageKey,
            K::Extractor: FromRequestParts<S> + Send,
            Q: QuotaSource<S>,
            LimitState<K, $policy, B>: FromRef<S>,
        {
            type Rejection = LimitRejection<
                <<K as Key>::Extractor as FromRequestParts<S>>::Rejection,
                Q::Rejection,
            >;

            fn from_request_parts(
                parts: &mut Parts,
                state: &S,
            ) -> impl std::future::Future<Output = Result<Self, Self::Rejection>> + Send
            where
                S: Sync,
            {
                async move {
                    let key_extractor = match K::Extractor::from_request_parts(parts, state).await {
                        Ok(ke) => ke,
                        Err(rejection) => {
                            return Err(LimitRejection::KeyExtractionFailure(rejection));
                        }
                    };

                    let quota = match Q::resolve(parts, state).await {
                        Ok(quota) => quota,
                        Err(rejection) => {
                            return Err(LimitRejection::QuotaResolutionFailure(rejection));
                        }
                    };

                    let limit_state: LimitState<K, $policy, B> = FromRef::from_ref(state);
                    let key = K::from_extractor(&key_extractor);
                    let snapshot = limit_state
                        .check(key, quota)
                        .await
                        .map_err(|error| LimitRejection::Backend(error.to_string()))?;

                    if snapshot.allowed {
                        parts.extensions.insert(RateLimitInfo(snapshot));
                        Ok(Self(
                            key_extractor,
                            quota,
                            std::marker::PhantomData,
                        ))
                    } else {
                        Err(LimitRejection::RateLimitExceeded(snapshot))
                    }
                }
            }
        }
    };
}

define_dynamic_limit_extractor! {
    /// Token-bucket rate limit extractor with a runtime [`QuotaSource`].
    DynamicLimit => TokenBucketPolicy
}

define_dynamic_limit_extractor! {
    /// Fixed-window rate limit extractor with a runtime [`QuotaSource`].
    DynamicFixedWindowLimit => FixedWindowPolicy
}

define_dynamic_limit_extractor! {
    /// Sliding-window rate limit extractor with a runtime [`QuotaSource`].
    DynamicSlidingWindowLimit => SlidingWindowPolicy
}

/// Token-bucket rate limit configured to apply per second.
pub type LimitPerSecond<const COUNT: usize, K, B = MemoryBackend> = Limit<COUNT, 1000, K, B>;

/// Token-bucket rate limit configured to apply per minute.
pub type LimitPerMinute<const COUNT: usize, K, B = MemoryBackend> = Limit<COUNT, 60_000, K, B>;

/// Token-bucket rate limit configured to apply per hour.
pub type LimitPerHour<const COUNT: usize, K, B = MemoryBackend> = Limit<COUNT, 3_600_000, K, B>;

/// Token-bucket rate limit configured to apply per day.
pub type LimitPerDay<const COUNT: usize, K, B = MemoryBackend> = Limit<COUNT, 86_400_000, K, B>;

/// Fixed-window rate limit configured to apply per second.
pub type FixedWindowPerSecond<const COUNT: usize, K, B = MemoryBackend> =
    FixedWindowLimit<COUNT, 1000, K, B>;

/// Fixed-window rate limit configured to apply per minute.
pub type FixedWindowPerMinute<const COUNT: usize, K, B = MemoryBackend> =
    FixedWindowLimit<COUNT, 60_000, K, B>;

/// Fixed-window rate limit configured to apply per hour.
pub type FixedWindowPerHour<const COUNT: usize, K, B = MemoryBackend> =
    FixedWindowLimit<COUNT, 3_600_000, K, B>;

/// Fixed-window rate limit configured to apply per day.
pub type FixedWindowPerDay<const COUNT: usize, K, B = MemoryBackend> =
    FixedWindowLimit<COUNT, 86_400_000, K, B>;

/// Sliding-window rate limit configured to apply per second.
pub type SlidingWindowPerSecond<const COUNT: usize, K, B = MemoryBackend> =
    SlidingWindowLimit<COUNT, 1000, K, B>;

/// Sliding-window rate limit configured to apply per minute.
pub type SlidingWindowPerMinute<const COUNT: usize, K, B = MemoryBackend> =
    SlidingWindowLimit<COUNT, 60_000, K, B>;

/// Sliding-window rate limit configured to apply per hour.
pub type SlidingWindowPerHour<const COUNT: usize, K, B = MemoryBackend> =
    SlidingWindowLimit<COUNT, 3_600_000, K, B>;

/// Sliding-window rate limit configured to apply per day.
pub type SlidingWindowPerDay<const COUNT: usize, K, B = MemoryBackend> =
    SlidingWindowLimit<COUNT, 86_400_000, K, B>;

/// Trait defining the requirements for a key extractor, which is used to uniquely identify limit subjects
/// and extract rate limit parameters dynamically in request processing.
#[async_trait::async_trait]
pub trait Key: Send + Sync {
    /// Extractor used to build a rate limit key from request parts.
    type Extractor;
    /// Creates an instance of `Self` from the provided extractor reference.
    fn from_extractor(extractor: &Self::Extractor) -> Self;
}

/// Manages per-key state for a specific [`RateLimitPolicy`] and [`RateLimitBackend`].
#[derive(Clone)]
pub struct LimitState<K, P = TokenBucketPolicy, B = MemoryBackend> {
    backend: B,
    _marker: PhantomData<(K, P)>,
}

impl<K, P, B> LimitState<K, P, B>
where
    K: Key,
    P: RateLimitPolicy,
    B: RateLimitBackend,
{
    /// Creates a limit state backed by the provided storage backend.
    pub fn new(backend: B) -> Self {
        Self {
            backend,
            _marker: PhantomData,
        }
    }

    /// Returns the configured storage backend.
    pub fn backend(&self) -> &B {
        &self.backend
    }

    /// Checks and updates the rate limit for the given key.
    pub async fn check(&self, key: K, quota: Quota) -> Result<RateLimitSnapshot, B::Error>
    where
        K: StorageKey,
    {
        let storage_key =
            build_storage_key::<P>(self.backend.namespace(), &key.storage_key(), quota);
        self.backend
            .transact::<P>(&storage_key, quota, now_ms())
            .await
    }
}

impl<K, P> Default for LimitState<K, P, MemoryBackend>
where
    K: Key,
    P: RateLimitPolicy,
{
    fn default() -> Self {
        Self::new(MemoryBackend::default())
    }
}

/// Enumerates possible failure modes for rate limiting when extracting from request parts.
#[derive(Debug)]
pub enum LimitRejection<K, Q = Infallible> {
    /// Indicates a failure during key extraction, storing the underlying rejection reason.
    KeyExtractionFailure(K),

    /// Indicates that quota resolution failed.
    QuotaResolutionFailure(Q),

    /// Indicates that the rate limit has been exceeded.
    RateLimitExceeded(RateLimitSnapshot),

    /// Indicates that the storage backend failed while checking the limit.
    Backend(String),
}

impl<K: Display, Q: Display> Display for LimitRejection<K, Q> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            LimitRejection::KeyExtractionFailure(r) => write!(f, "{r}"),
            LimitRejection::QuotaResolutionFailure(r) => write!(f, "{r}"),
            LimitRejection::RateLimitExceeded(_) => write!(f, "Rate limit exceeded."),
            LimitRejection::Backend(message) => write!(f, "Rate limit storage failure: {message}"),
        }
    }
}

impl<K: Error + 'static, Q: Error + 'static> Error for LimitRejection<K, Q> {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            LimitRejection::KeyExtractionFailure(error) => Some(error),
            LimitRejection::QuotaResolutionFailure(error) => Some(error),
            LimitRejection::RateLimitExceeded(_) | LimitRejection::Backend(_) => None,
        }
    }
}

impl<K: IntoResponse, Q: IntoResponse> IntoResponse for LimitRejection<K, Q> {
    fn into_response(self) -> Response {
        match self {
            LimitRejection::KeyExtractionFailure(rejection) => rejection.into_response(),
            LimitRejection::QuotaResolutionFailure(rejection) => rejection.into_response(),
            LimitRejection::RateLimitExceeded(snapshot) => {
                let now_ms = now_ms();
                let mut response =
                    (StatusCode::TOO_MANY_REQUESTS, "Rate limit exceeded.").into_response();
                response.headers_mut().extend(snapshot.to_headers(now_ms));
                response
            }
            LimitRejection::Backend(message) => (
                StatusCode::SERVICE_UNAVAILABLE,
                format!("Rate limit storage failure: {message}"),
            )
                .into_response(),
        }
    }
}

/// Returns rate limit headers from request extensions when a limit check succeeded.
pub fn rate_limit_headers_from_parts(parts: &Parts) -> Option<http::HeaderMap> {
    parts
        .extensions
        .get::<RateLimitInfo>()
        .map(|info| info.0.to_headers(now_ms()))
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::routing::get;
    use axum::Router;
    use axum_test::TestServer;
    use http::Uri;
    use std::future::IntoFuture;
    use std::time::Duration;

    #[tokio::test]
    async fn token_bucket_limit() {
        const TEST_ROUTE0: &str = "/limit0";
        const TEST_ROUTE1: &str = "/limit1";
        async fn handler0(Limit(_uri, _): LimitPerSecond<1, Uri>) -> impl IntoResponse {}

        async fn handler1(Limit(_uri, _): LimitPerSecond<3, Uri>) -> impl IntoResponse {}

        let my_app = Router::new()
            .route(TEST_ROUTE0, get(handler0))
            .route(TEST_ROUTE1, get(handler1))
            .with_state(LimitState::default());

        let server = TestServer::new(my_app);

        let response = server.get(TEST_ROUTE0).await;
        assert_eq!(response.status_code(), StatusCode::OK);
        let response = server.get(TEST_ROUTE0).await;
        assert_eq!(response.status_code(), StatusCode::TOO_MANY_REQUESTS);
        tokio::time::sleep(Duration::from_secs(1)).await;
        let response = server.get(TEST_ROUTE0).await;
        assert_eq!(response.status_code(), StatusCode::OK);

        let gets = vec![
            server.get(TEST_ROUTE1).into_future(),
            server.get(TEST_ROUTE1).into_future(),
            server.get(TEST_ROUTE1).into_future(),
        ];

        let resp = futures::future::join_all(gets).await;
        assert!(!resp.iter().any(|r| !r.status_code().is_success()));
        assert_eq!(
            server.get(TEST_ROUTE1).await.status_code(),
            StatusCode::TOO_MANY_REQUESTS
        );
        tokio::time::sleep(Duration::from_secs(1)).await;
        let response = server.get(TEST_ROUTE1).await;
        assert_eq!(response.status_code(), StatusCode::OK);
    }

    #[tokio::test]
    async fn token_bucket_limit_per_second_allows_count_per_second() {
        const ROUTE: &str = "/per_sec";
        async fn handler(_: LimitPerSecond<5, Uri>) -> impl IntoResponse {}

        let app = Router::new()
            .route(ROUTE, get(handler))
            .with_state(LimitState::default());

        let server = TestServer::new(app);

        for _ in 0..5 {
            assert_eq!(server.get(ROUTE).await.status_code(), StatusCode::OK);
        }
        assert_eq!(
            server.get(ROUTE).await.status_code(),
            StatusCode::TOO_MANY_REQUESTS
        );

        tokio::time::sleep(Duration::from_secs(1)).await;
        for _ in 0..5 {
            assert_eq!(server.get(ROUTE).await.status_code(), StatusCode::OK);
        }
    }

    #[tokio::test]
    async fn fixed_window_limit_per_second() {
        const ROUTE: &str = "/fixed_per_sec";
        async fn handler(_: FixedWindowPerSecond<3, Uri>) -> impl IntoResponse {}

        let app = Router::new()
            .route(ROUTE, get(handler))
            .with_state(LimitState::<Uri, FixedWindowPolicy>::default());

        let server = TestServer::new(app);

        for _ in 0..3 {
            assert_eq!(server.get(ROUTE).await.status_code(), StatusCode::OK);
        }
        assert_eq!(
            server.get(ROUTE).await.status_code(),
            StatusCode::TOO_MANY_REQUESTS
        );

        tokio::time::sleep(Duration::from_secs(1)).await;

        for _ in 0..3 {
            assert_eq!(server.get(ROUTE).await.status_code(), StatusCode::OK);
        }
    }

    #[tokio::test]
    async fn sliding_window_limit_per_second() {
        const ROUTE: &str = "/sliding_per_sec";
        async fn handler(_: SlidingWindowPerSecond<3, Uri>) -> impl IntoResponse {}

        let app = Router::new()
            .route(ROUTE, get(handler))
            .with_state(LimitState::<Uri, SlidingWindowPolicy>::default());

        let server = TestServer::new(app);

        for _ in 0..3 {
            assert_eq!(server.get(ROUTE).await.status_code(), StatusCode::OK);
        }
        assert_eq!(
            server.get(ROUTE).await.status_code(),
            StatusCode::TOO_MANY_REQUESTS
        );

        tokio::time::sleep(Duration::from_secs(2)).await;

        for _ in 0..3 {
            assert_eq!(server.get(ROUTE).await.status_code(), StatusCode::OK);
        }
    }

    #[tokio::test]
    async fn rate_limit_exceeded_includes_headers() {
        const ROUTE: &str = "/headers";
        async fn handler(_: LimitPerSecond<1, Uri>) -> impl IntoResponse {}

        let app = Router::new()
            .route(ROUTE, get(handler))
            .with_state(LimitState::default());

        let server = TestServer::new(app);

        assert_eq!(server.get(ROUTE).await.status_code(), StatusCode::OK);
        let response = server.get(ROUTE).await;
        assert_eq!(response.status_code(), StatusCode::TOO_MANY_REQUESTS);
        assert!(response.headers().get("x-ratelimit-limit").is_some());
        assert!(response.headers().get("retry-after").is_some());
    }

    #[tokio::test]
    async fn different_quotas_on_same_key_are_isolated() {
        let state = LimitState::<Uri>::default();
        let uri: Uri = "/test".parse().expect("valid uri");
        let quota_a = Quota::new(1, 1000);
        let quota_b = Quota::new(5, 1000);

        assert!(
            state
                .check(uri.clone(), quota_a)
                .await
                .expect("check")
                .allowed
        );
        assert!(
            !state
                .check(uri.clone(), quota_a)
                .await
                .expect("check")
                .allowed
        );
        assert!(state.check(uri, quota_b).await.expect("check").allowed);
    }

    #[tokio::test]
    async fn custom_memory_namespace_isolates_state() {
        let left = LimitState::<Uri>::new(MemoryBackend::with_namespace("left"));
        let right = LimitState::<Uri>::new(MemoryBackend::with_namespace("right"));
        let uri: Uri = "/shared".parse().expect("valid uri");
        let quota = Quota::per_second(1);

        assert!(left.check(uri.clone(), quota).await.expect("left").allowed);
        assert!(!left.check(uri.clone(), quota).await.expect("left").allowed);
        assert!(right.check(uri, quota).await.expect("right").allowed);
    }

    #[derive(Clone)]
    struct ConfigState {
        limits: LimitState<Uri>,
        api_quota: Quota,
    }

    impl FromRef<ConfigState> for LimitState<Uri> {
        fn from_ref(state: &ConfigState) -> Self {
            state.limits.clone()
        }
    }

    impl FromRef<ConfigState> for Quota {
        fn from_ref(state: &ConfigState) -> Quota {
            state.api_quota
        }
    }

    #[derive(Clone, Copy)]
    struct ApiQuota(Quota);

    impl FromRef<ConfigState> for ApiQuota {
        fn from_ref(state: &ConfigState) -> Self {
            ApiQuota(state.api_quota)
        }
    }

    impl From<ApiQuota> for Quota {
        fn from(value: ApiQuota) -> Self {
            value.0
        }
    }

    #[tokio::test]
    async fn dynamic_limit_reads_quota_from_state() {
        const ROUTE: &str = "/dynamic";
        async fn handler(_: DynamicLimit<Uri, Quota>) -> impl IntoResponse {}

        let state = ConfigState {
            limits: LimitState::default(),
            api_quota: Quota::per_second(2),
        };

        let app = Router::new().route(ROUTE, get(handler)).with_state(state);

        let server = TestServer::new(app);

        assert_eq!(server.get(ROUTE).await.status_code(), StatusCode::OK);
        assert_eq!(server.get(ROUTE).await.status_code(), StatusCode::OK);
        assert_eq!(
            server.get(ROUTE).await.status_code(),
            StatusCode::TOO_MANY_REQUESTS
        );
    }

    #[tokio::test]
    async fn dynamic_limit_state_quota_newtype() {
        const ROUTE: &str = "/dynamic_newtype";
        async fn handler(_: DynamicLimit<Uri, ApiQuota>) -> impl IntoResponse {}

        let state = ConfigState {
            limits: LimitState::default(),
            api_quota: Quota::per_second(1),
        };

        let app = Router::new().route(ROUTE, get(handler)).with_state(state);

        let server = TestServer::new(app);

        assert_eq!(server.get(ROUTE).await.status_code(), StatusCode::OK);
        assert_eq!(
            server.get(ROUTE).await.status_code(),
            StatusCode::TOO_MANY_REQUESTS
        );
    }

    #[tokio::test]
    async fn different_keys_are_isolated() {
        let state = LimitState::<Uri>::default();
        let route_a: Uri = "/a".parse().expect("valid uri");
        let route_b: Uri = "/b".parse().expect("valid uri");
        let quota = Quota::per_second(1);

        assert!(
            state
                .check(route_a.clone(), quota)
                .await
                .expect("a")
                .allowed
        );
        assert!(!state.check(route_a, quota).await.expect("a").allowed);
        assert!(state.check(route_b, quota).await.expect("b").allowed);
    }

    #[test]
    fn rate_limit_headers_from_parts_reads_extension() {
        let mut parts = http::Request::new(()).into_parts().0;
        parts.extensions.insert(RateLimitInfo(RateLimitSnapshot {
            allowed: true,
            limit: 5,
            remaining: 4,
            reset_at_ms: 1_000,
        }));

        let headers = rate_limit_headers_from_parts(&parts).expect("headers");
        assert_eq!(
            headers
                .get("x-ratelimit-limit")
                .and_then(|value| value.to_str().ok()),
            Some("5")
        );
        assert_eq!(
            headers
                .get("x-ratelimit-remaining")
                .and_then(|value| value.to_str().ok()),
            Some("4")
        );
    }

    #[tokio::test]
    async fn dynamic_fixed_window_limit_reads_quota_from_state() {
        #[derive(Clone)]
        struct FixedConfigState {
            limits: LimitState<Uri, FixedWindowPolicy>,
            api_quota: Quota,
        }

        impl FromRef<FixedConfigState> for LimitState<Uri, FixedWindowPolicy> {
            fn from_ref(state: &FixedConfigState) -> Self {
                state.limits.clone()
            }
        }

        impl FromRef<FixedConfigState> for Quota {
            fn from_ref(state: &FixedConfigState) -> Quota {
                state.api_quota
            }
        }

        const ROUTE: &str = "/dynamic_fixed";
        async fn handler(_: DynamicFixedWindowLimit<Uri, Quota>) -> impl IntoResponse {}

        let state = FixedConfigState {
            limits: LimitState::<Uri, FixedWindowPolicy>::default(),
            api_quota: Quota::per_second(2),
        };

        let app = Router::new().route(ROUTE, get(handler)).with_state(state);
        let server = TestServer::new(app);

        assert_eq!(server.get(ROUTE).await.status_code(), StatusCode::OK);
        assert_eq!(server.get(ROUTE).await.status_code(), StatusCode::OK);
        assert_eq!(
            server.get(ROUTE).await.status_code(),
            StatusCode::TOO_MANY_REQUESTS
        );
    }

    #[tokio::test]
    async fn dynamic_sliding_window_limit_reads_quota_from_state() {
        #[derive(Clone)]
        struct SlidingConfigState {
            limits: LimitState<Uri, SlidingWindowPolicy>,
            api_quota: Quota,
        }

        impl FromRef<SlidingConfigState> for LimitState<Uri, SlidingWindowPolicy> {
            fn from_ref(state: &SlidingConfigState) -> Self {
                state.limits.clone()
            }
        }

        impl FromRef<SlidingConfigState> for Quota {
            fn from_ref(state: &SlidingConfigState) -> Quota {
                state.api_quota
            }
        }

        const ROUTE: &str = "/dynamic_sliding";
        async fn handler(_: DynamicSlidingWindowLimit<Uri, Quota>) -> impl IntoResponse {}

        let state = SlidingConfigState {
            limits: LimitState::<Uri, SlidingWindowPolicy>::default(),
            api_quota: Quota::per_second(1),
        };

        let app = Router::new().route(ROUTE, get(handler)).with_state(state);
        let server = TestServer::new(app);

        assert_eq!(server.get(ROUTE).await.status_code(), StatusCode::OK);
        assert_eq!(
            server.get(ROUTE).await.status_code(),
            StatusCode::TOO_MANY_REQUESTS
        );
    }

    #[derive(Clone)]
    struct FailingBackend;

    #[async_trait::async_trait]
    impl RateLimitBackend for FailingBackend {
        type Error = BackendError;

        fn namespace(&self) -> &str {
            "failing"
        }

        async fn transact<P>(
            &self,
            _storage_key: &str,
            _quota: Quota,
            _now_ms: u64,
        ) -> Result<RateLimitSnapshot, Self::Error>
        where
            P: RateLimitPolicy,
        {
            Err(BackendError::Contention)
        }
    }

    #[tokio::test]
    async fn backend_failure_returns_service_unavailable() {
        const ROUTE: &str = "/backend_fail";
        async fn handler(_: Limit<1, 1000, Uri, FailingBackend>) -> impl IntoResponse {}

        let app = Router::new()
            .route(ROUTE, get(handler))
            .with_state(LimitState::<Uri, TokenBucketPolicy, FailingBackend>::new(
                FailingBackend,
            ));

        let server = TestServer::new(app);
        assert_eq!(
            server.get(ROUTE).await.status_code(),
            StatusCode::SERVICE_UNAVAILABLE
        );
    }
}