runlimit-postgres 0.3.1

Replica-safe PostgreSQL rate-limit storage
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
//! Replica-safe `PostgreSQL` storage for Runlimit fixed-window policies.
//!
//! [`PostgresLimiter`] implements the same anchored fixed-window model as the
//! in-memory backend: the first admitted check starts a window, and subsequent
//! checks share that window until it expires. `PostgreSQL`'s clock is the sole
//! time authority, so callers on different hosts do not need synchronized
//! clocks. Atomic batches use set-based lock, preflight, and update phases, so
//! their number of SQL phases does not grow with the batch size. Single checks
//! use the same separate logical-key, counter-row, and capacity-shard lock
//! phases so every statement that tests row absence starts after the logical
//! key lock has been acquired.
//! [`PostgresLimiter`] implements [`runlimit_core::Limiter`] for async generic
//! adapters.
//! The optional `serde` feature enables validated [`PostgresConfig`] loading
//! and the corresponding `runlimit-core` policy and response metadata.
//!
//! # Installation
//!
//! Call [`PostgresLimiter::migrate`] before serving traffic. It can share
//! `SQLx`'s `_sqlx_migrations` table with application migrations only when every
//! participating [`Migrator`] configures
//! [`Migrator::set_ignore_missing`] to `true`.
//! A strict application migrator will reject Runlimit's migration versions as
//! unknown. The first migration creates `runlimit_fixed_windows` in the
//! connection's default schema.
//!
//! [`MIGRATOR`] is the raw bundled migrator with `SQLx`'s strict defaults. It is
//! intended for an exclusively managed migration history and connection. It
//! does not add [`PostgresLimiter::migrate`]'s protection against returning a
//! possibly session-locked connection to a pool after an error or cancellation.
//! Applications that retain a strict host migrator should vendor the bundled
//! Runlimit SQL as application-owned migrations, in order, using
//! [`CREATE_RUNLIMIT_FIXED_WINDOWS_SQL`] and
//! [`SET_RUNLIMIT_FIXED_WINDOWS_FILLFACTOR_SQL`] followed by
//! [`BOUND_RUNLIMIT_FIXED_WINDOW_CARDINALITY_SQL`] instead of invoking either
//! Runlimit migrator against the shared history.
//!
//! # Storage capacity
//!
//! Every counter belongs to one of [`CAPACITY_SHARD_COUNT`] persistent shards.
//! The migration-maintained ledger enforces
//! [`HARD_MAX_ROWS_PER_SHARD`] even for older replicas, while
//! [`PostgresConfig::maximum_rows_per_shard`] lets current replicas fail closed
//! at a lower operational bound. A full shard denies only new storage keys;
//! existing rows remain usable and active rows are never evicted. Expiry alone
//! does not release a row. Capacity becomes reusable only after
//! [`PostgresLimiter::cleanup_expired`] commits its deletion.
//!
//! # Failure semantics
//!
//! Pre-commit database failures and pre-commit timeouts mean the transaction
//! did not commit. A
//! [`CheckError::CommitOutcomeUnknown`] or [`CheckError::CommitTimedOut`] error
//! means `PostgreSQL` may have committed even though the client did not receive
//! confirmation. [`CheckError::CommittedResponseInvariant`] means the commit
//! was confirmed but its response was unusable. Fail closed in all three cases,
//! and do not blindly retry a check that may already have consumed quota.
//! Once the database has produced a denial, an explicit rollback failure or
//! deadline cannot replace that valid decision; the connection is discarded so
//! closing it rolls back the non-mutating denied transaction.
//! Dropping a check or cleanup future prevents the backend from reporting its
//! outcome; if cancellation races with commit, callers must likewise treat the
//! outcome as unknown. Prefer this backend's configured acquisition and
//! operation budgets to a shorter outer timeout.
//!
//! The advisory-lock `v1` domain and derivation are a cross-replica protocol.
//! They must not be changed in place: replicas from a rolling deployment must
//! calculate identical lock IDs for the same persistent counter. `PostgreSQL`'s
//! advisory-lock namespace is shared by every application in the same database.
//! An unrelated or deliberately held matching `bigint` lock therefore blocks
//! the affected Runlimit key until the operation deadline; use a trusted
//! database boundary and restrict advisory-lock privileges for unrelated roles.
//! The persisted capacity-shard derivation is likewise a cross-replica
//! protocol: it XORs the leading bytes of the policy fingerprint and opaque
//! subject key. Changing the derivation or shard count requires a new storage
//! protocol and migration.
//!
//! # Integration tests
//!
//! The database tests are opt-in:
//!
//! ```text
//! RUNLIMIT_POSTGRES_TEST_DATABASE_URL=postgresql:///runlimit_test \
//! cargo test -p runlimit-postgres --test postgres -- --ignored
//! ```

use std::{
    fmt,
    future::Future,
    sync::Arc,
    time::{Duration, Instant as WallClockInstant},
};

use runlimit_core::{
    AdmissionObservation, BatchDecision, Check, CleanupObservation, Decision, FixedWindowPolicy,
    Limiter, Observation, Observer, observe_safely, validate_batch,
};
use sqlx::{
    PgPool, Postgres,
    migrate::{MigrateError, Migrator},
    pool::PoolConnection,
};
use tokio::time::Instant;

#[derive(Debug)]
pub(crate) enum ConnectionOutcome<T> {
    Reusable(T),
    MustClose(T),
}

impl<T> ConnectionOutcome<T> {
    fn map<U>(self, map: impl FnOnce(T) -> U) -> ConnectionOutcome<U> {
        match self {
            Self::Reusable(value) => ConnectionOutcome::Reusable(map(value)),
            Self::MustClose(value) => ConnectionOutcome::MustClose(map(value)),
        }
    }
}

mod admission;
mod config;
mod errors;
mod maintenance;
mod protocol;

pub use config::{PostgresConfig, PostgresConfigError};
pub use errors::{CheckError, MaintenanceError};
pub use protocol::{CAPACITY_SHARD_COUNT, HARD_MAX_ROWS_PER_SHARD};

use admission::{
    BatchSqlInput, acquire_check_connection, run_check_transaction, single_decision_from_batch,
};
use errors::check_error_consumption;
use maintenance::{acquire_maintenance_connection, run_cleanup_transaction};

#[cfg(test)]
use admission::{
    PendingAllowance, PendingBatchDenial, PendingDenial, PendingQuotaDenial, database_integer,
    finish_denied_transaction,
};
#[cfg(test)]
use protocol::{
    BATCH_PREFLIGHT_SQL, BATCH_UPSERT_SQL, CLEANUP_SQL, advisory_lock_id, capacity_shard,
};
#[cfg(test)]
use runlimit_core::BatchError;
#[cfg(test)]
use sqlx::postgres::types::PgInterval;

/// SQL for the immutable `20260723000000_create_runlimit_fixed_windows`
/// migration.
///
/// Strict host migrators can copy this SQL into their application-owned
/// migration stream instead of running [`MIGRATOR`] against a shared
/// `_sqlx_migrations` history. Apply it before
/// [`SET_RUNLIMIT_FIXED_WINDOWS_FILLFACTOR_SQL`].
pub const CREATE_RUNLIMIT_FIXED_WINDOWS_SQL: &str =
    include_str!("../migrations/20260723000000_create_runlimit_fixed_windows.sql");

/// SQL for the additive
/// `20260725000000_set_runlimit_fixed_windows_fillfactor` migration.
///
/// Strict host migrators can copy this SQL into their application-owned
/// migration stream after [`CREATE_RUNLIMIT_FIXED_WINDOWS_SQL`]. The
/// application remains responsible for assigning versions that fit its own
/// migration history.
pub const SET_RUNLIMIT_FIXED_WINDOWS_FILLFACTOR_SQL: &str =
    include_str!("../migrations/20260725000000_set_runlimit_fixed_windows_fillfactor.sql");

/// SQL for the additive
/// `20260726000000_bound_runlimit_fixed_window_cardinality` migration.
///
/// This migration assigns every persisted counter to one of 256 stable
/// capacity shards and installs a trigger-maintained row-count ledger. Apply it
/// after [`SET_RUNLIMIT_FIXED_WINDOWS_FILLFACTOR_SQL`].
pub const BOUND_RUNLIMIT_FIXED_WINDOW_CARDINALITY_SQL: &str =
    include_str!("../migrations/20260726000000_bound_runlimit_fixed_window_cardinality.sql");

/// Raw bundled migrations required by [`PostgresLimiter`].
///
/// This value retains `SQLx`'s strict missing-version behavior and is suitable
/// only for an exclusively managed migration history and connection. For a
/// pooled application that shares `_sqlx_migrations`, prefer
/// [`PostgresLimiter::migrate`] and configure every other participating
/// [`Migrator`] to set [`Migrator::set_ignore_missing`] to `true`.
pub static MIGRATOR: Migrator = sqlx::migrate!("./migrations");

/// A replica-safe rate limiter backed by a `PostgreSQL` pool.
///
/// Cloning this value is cheap because [`PgPool`] is internally reference
/// counted.
#[derive(Clone)]
pub struct PostgresLimiter {
    pool: PgPool,
    config: PostgresConfig,
    observer: Option<Arc<dyn Observer>>,
}

impl fmt::Debug for PostgresLimiter {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("PostgresLimiter")
            .field("pool", &self.pool)
            .field("config", &self.config)
            .field("has_observer", &self.observer.is_some())
            .finish_non_exhaustive()
    }
}

impl PostgresLimiter {
    /// Creates a limiter using `pool`.
    ///
    /// Call [`Self::migrate`] during application startup before admitting
    /// requests.
    pub const fn new(pool: PgPool) -> Self {
        Self {
            pool,
            config: PostgresConfig::new(),
            observer: None,
        }
    }

    /// Creates a limiter using an explicit runtime configuration.
    pub const fn with_config(pool: PgPool, config: PostgresConfig) -> Self {
        Self {
            pool,
            config,
            observer: None,
        }
    }

    /// Returns this limiter with an operational observer.
    ///
    /// Observer callbacks run only after a database operation has released its
    /// connection. Callback panics are isolated and cannot change admission or
    /// cleanup results.
    #[must_use]
    pub fn with_observer(mut self, observer: Arc<dyn Observer>) -> Self {
        self.observer = Some(observer);
        self
    }

    /// Returns the underlying connection pool.
    pub const fn pool(&self) -> &PgPool {
        &self.pool
    }

    /// Returns the backend runtime configuration.
    pub const fn config(&self) -> PostgresConfig {
        self.config
    }

    /// Applies the bundled Runlimit database migrations.
    ///
    /// Unrelated successful versions in `_sqlx_migrations` are ignored, which
    /// permits a shared migration history only when every other participating
    /// `SQLx` migrator also sets [`Migrator::set_ignore_missing`] to `true`.
    /// Known Runlimit versions still receive `SQLx`'s checksum validation.
    /// Applications that keep a strict host migrator should instead vendor the
    /// bundled SQL into their application-owned migration stream.
    ///
    /// Migration locking is session scoped. An acquired connection is returned
    /// to the pool only after a successful run and unlock; an error or
    /// cancellation closes it instead.
    ///
    /// # Errors
    ///
    /// Returns an error if migration metadata cannot be read or `PostgreSQL`
    /// rejects a migration.
    pub async fn migrate(&self) -> Result<(), MigrateError> {
        let connection = self.pool.acquire().await?;
        let mut guarded_connection = ConnectionCancellationGuard::new(connection);
        let mut migrator = sqlx::migrate!("./migrations");
        migrator.set_ignore_missing(true);

        let result = migrator
            .run_direct(&mut **guarded_connection.connection())
            .await;
        if result.is_ok() {
            guarded_connection.reuse();
        }
        result
    }

    /// Checks and, when allowed, consumes one fixed-window quota.
    ///
    /// A denied decision does not change the stored counter.
    ///
    /// # Errors
    ///
    /// Returns [`CheckError::DefinitelyNotConsumed`] for failures before a
    /// successful commit and [`CheckError::CommitOutcomeUnknown`] when commit
    /// confirmation is unavailable. An internal single-response invariant
    /// failure reports whether the batch was rolled back or committed.
    pub async fn check(&self, check: &Check<'_>) -> Result<Decision, CheckError> {
        let started = WallClockInstant::now();
        let result = self
            .check_all_unobserved(std::slice::from_ref(check))
            .await
            .and_then(single_decision_from_batch);
        self.observe_single_admission(check, &result, started.elapsed());
        result
    }

    /// Atomically checks and consumes every quota in `checks`.
    ///
    /// Rows are acquired in a deterministic storage-key order to avoid
    /// deadlocks between overlapping batches. Returned allowed decisions retain
    /// the caller's input order. Repeated occurrences of the same storage key
    /// are rejected before a connection is acquired because their independent
    /// decision metadata would be ambiguous.
    ///
    /// New keys lock their capacity-ledger rows in shard order. If the earliest
    /// caller-ordered failure is the configured shard bound, the batch returns
    /// [`runlimit_core::Denial::storage_capacity`] with no retry duration and changes neither
    /// quota nor storage. Existing and expired target rows need no new slot.
    ///
    /// If any check is denied, the backend attempts an explicit rollback and
    /// the returned denial index is the earliest denied item in caller order.
    /// A rollback error or deadline does not replace the denial because no
    /// counter was changed; instead, the connection is discarded so socket
    /// closure rolls the transaction back. Passing an empty slice succeeds
    /// without acquiring a database connection.
    ///
    /// # Errors
    ///
    /// Pool-acquisition or operation timeout before commit is
    /// [`CheckError::TimedOutBeforeCommit`]. Other database failures before
    /// commit are [`CheckError::DefinitelyNotConsumed`]. Any commit error is
    /// conservatively classified as [`CheckError::CommitOutcomeUnknown`].
    pub async fn check_all(&self, checks: &[Check<'_>]) -> Result<BatchDecision, CheckError> {
        let started = WallClockInstant::now();
        let result = self.check_all_unobserved(checks).await;
        self.observe_batch_admission(checks, &result, started.elapsed());
        result
    }

    async fn check_all_unobserved(
        &self,
        checks: &[Check<'_>],
    ) -> Result<BatchDecision, CheckError> {
        validate_batch(checks, self.config.max_batch_size())?;
        if checks.is_empty() {
            return Ok(BatchDecision::allowed(Vec::new()));
        }

        // Materialize every SQL array and both lock orders before reserving a
        // pool slot. Every database phase then consumes the exact same key
        // arrays, preventing identity drift between locking and mutation.
        let input = BatchSqlInput::from_checks(checks);
        let connection =
            acquire_check_connection(&self.pool, self.config.pool_acquire_timeout()).await?;
        // Pool contention does not consume the database-work budget. A
        // survivor receives a fresh deadline for begin, statements, locks,
        // rollback, and commit.
        let deadline = Instant::now() + self.config.operation_timeout();
        let mut guarded_connection = ConnectionCancellationGuard::new(connection);
        let outcome = run_check_transaction(
            guarded_connection.connection(),
            &input,
            checks,
            self.config.maximum_rows_per_shard(),
            deadline,
        )
        .await;
        guarded_connection.finish(outcome)
    }

    /// Deletes at most `maximum_rows` expired windows and releases their
    /// capacity-ledger slots in the same transaction.
    ///
    /// Cleanup uses `FOR UPDATE SKIP LOCKED`, so it does not wait behind active
    /// checks and cannot remove a row concurrently being renewed. A zero limit
    /// performs no database work.
    ///
    /// # Errors
    ///
    /// Returns an error if `PostgreSQL` rejects the maintenance transaction or
    /// if either [`PostgresConfig::pool_acquire_timeout`] or the fresh
    /// [`PostgresConfig::operation_timeout`] budget is exceeded. A commit error
    /// or commit timeout means rows may have been removed; inspect
    /// [`MaintenanceError::may_have_removed_rows`]. Cleanup only targets
    /// already-expired rows and never consumes quota.
    pub async fn cleanup_expired(&self, maximum_rows: u32) -> Result<u64, MaintenanceError> {
        let started = WallClockInstant::now();
        let result = self.cleanup_expired_unobserved(maximum_rows).await;
        self.observe_cleanup(maximum_rows, &result, started.elapsed());
        result
    }

    async fn cleanup_expired_unobserved(&self, maximum_rows: u32) -> Result<u64, MaintenanceError> {
        if maximum_rows == 0 {
            return Ok(0);
        }

        let connection =
            acquire_maintenance_connection(&self.pool, self.config.pool_acquire_timeout()).await?;
        let deadline = Instant::now() + self.config.operation_timeout();
        let mut guarded_connection = ConnectionCancellationGuard::new(connection);
        let outcome =
            run_cleanup_transaction(guarded_connection.connection(), maximum_rows, deadline).await;
        guarded_connection.finish(outcome)
    }

    fn observe_single_admission(
        &self,
        check: &Check<'_>,
        result: &Result<Decision, CheckError>,
        elapsed: Duration,
    ) {
        match result {
            Ok(decision) => self
                .observe_admission(|| AdmissionObservation::from_check(check, decision, elapsed)),
            Err(error) => self.observe_admission(|| {
                AdmissionObservation::failed_check(check, check_error_consumption(error), elapsed)
            }),
        }
    }

    fn observe_batch_admission(
        &self,
        checks: &[Check<'_>],
        result: &Result<BatchDecision, CheckError>,
        elapsed: Duration,
    ) {
        match result {
            Ok(decision) => self
                .observe_admission(|| AdmissionObservation::from_batch(checks, decision, elapsed)),
            Err(error) => {
                self.observe_admission(|| {
                    if let [check] = checks {
                        AdmissionObservation::failed_batch_for_check(
                            check,
                            check_error_consumption(error),
                            elapsed,
                        )
                    } else {
                        AdmissionObservation::failed_batch(
                            checks.len(),
                            check_error_consumption(error),
                            elapsed,
                        )
                    }
                });
            }
        }
    }

    fn observe_admission<'a>(&self, build_observation: impl FnOnce() -> AdmissionObservation<'a>) {
        let Some(observer) = &self.observer else {
            return;
        };
        let admission = build_observation();
        observe_safely(observer.as_ref(), &Observation::Admission(admission));
    }

    fn observe_cleanup(
        &self,
        requested: u32,
        result: &Result<u64, MaintenanceError>,
        elapsed: Duration,
    ) {
        let Some(observer) = &self.observer else {
            return;
        };
        let requested = usize::try_from(requested).unwrap_or(usize::MAX);
        let observation = match result {
            Ok(removed) => CleanupObservation::confirmed(requested, *removed, elapsed),
            Err(error) if error.may_have_removed_rows() => {
                CleanupObservation::outcome_unknown(requested, elapsed)
            }
            Err(_) => CleanupObservation::definitely_no_effect(requested, elapsed),
        };
        observe_safely(observer.as_ref(), &Observation::Cleanup(observation));
    }
}

impl Limiter for PostgresLimiter {
    type Policy = FixedWindowPolicy;
    type Error = CheckError;

    fn check(
        &self,
        check: &Check<'_>,
    ) -> impl Future<Output = Result<Decision, Self::Error>> + Send {
        PostgresLimiter::check(self, check)
    }

    fn check_all(
        &self,
        checks: &[Check<'_>],
    ) -> impl Future<Output = Result<BatchDecision, Self::Error>> + Send {
        PostgresLimiter::check_all(self, checks)
    }
}

struct ConnectionCancellationGuard {
    connection: PoolConnection<Postgres>,
    armed: bool,
}

impl ConnectionCancellationGuard {
    const fn new(connection: PoolConnection<Postgres>) -> Self {
        Self {
            connection,
            armed: true,
        }
    }

    const fn connection(&mut self) -> &mut PoolConnection<Postgres> {
        &mut self.connection
    }

    const fn reuse(&mut self) {
        self.armed = false;
    }

    fn finish<T, E>(mut self, outcome: ConnectionOutcome<Result<T, E>>) -> Result<T, E> {
        match outcome {
            ConnectionOutcome::Reusable(result) => {
                self.reuse();
                result
            }
            ConnectionOutcome::MustClose(result) => result,
        }
    }
}

impl Drop for ConnectionCancellationGuard {
    fn drop(&mut self) {
        if self.armed {
            // Dropping an SQLx query future does not send a PostgreSQL cancel
            // request. Close the socket instead of returning a potentially
            // busy connection to the pool when either Runlimit's deadline or
            // cancellation of the outer limiter future wins the race.
            self.connection.close_on_drop();
        }
    }
}

#[cfg(test)]
mod tests {
    use std::{
        sync::{Arc, Mutex},
        time::Duration,
    };

    use super::*;
    use runlimit_core::{
        AdmissionOperation, AdmissionOutcome, ConsumptionStatus, FixedWindowPolicy, MAX_LIMIT,
        MAX_WINDOW, MAX_WINDOW_MILLIS, PolicyId, QuotaDenial, ScopeId, SubjectKey,
    };

    fn policy(id: &str, scope: &str) -> FixedWindowPolicy {
        FixedWindowPolicy::new(
            PolicyId::new(id).expect("valid policy identifier"),
            ScopeId::new(scope).expect("valid scope identifier"),
            10,
            Duration::from_secs(60),
        )
        .expect("valid policy")
    }

    fn key(byte: u8) -> SubjectKey {
        SubjectKey::from_digest([byte; 32])
    }

    #[derive(Default)]
    struct RecordingObserver {
        events: Mutex<Vec<(AdmissionOperation, AdmissionOutcome, ConsumptionStatus)>>,
        cleanups: Mutex<Vec<(usize, Option<u64>, ConsumptionStatus)>>,
    }

    impl Observer for RecordingObserver {
        fn observe(&self, observation: &Observation<'_>) {
            match observation {
                Observation::Admission(admission) => self.events.lock().unwrap().push((
                    admission.operation(),
                    admission.outcome(),
                    admission.consumption(),
                )),
                Observation::Cleanup(cleanup) => self.cleanups.lock().unwrap().push((
                    cleanup.requested(),
                    cleanup.removed(),
                    cleanup.consumption(),
                )),
                _ => {}
            }
        }
    }

    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
    struct RecordedAdmissionMetadata {
        operation: AdmissionOperation,
        batch_size: usize,
        has_policy_id: bool,
        has_scope_id: bool,
        has_policy_fingerprint: bool,
        consumption: ConsumptionStatus,
    }

    #[derive(Default)]
    struct AdmissionMetadataObserver {
        admissions: Mutex<Vec<RecordedAdmissionMetadata>>,
    }

    impl Observer for AdmissionMetadataObserver {
        fn observe(&self, observation: &Observation<'_>) {
            let Observation::Admission(admission) = observation else {
                return;
            };
            self.admissions
                .lock()
                .unwrap()
                .push(RecordedAdmissionMetadata {
                    operation: admission.operation(),
                    batch_size: admission.batch_size(),
                    has_policy_id: admission.policy_id().is_some(),
                    has_scope_id: admission.scope_id().is_some(),
                    has_policy_fingerprint: admission.policy_fingerprint().is_some(),
                    consumption: admission.consumption(),
                });
        }
    }

    struct PanickingObserver;

    impl Observer for PanickingObserver {
        fn observe(&self, _: &Observation<'_>) {
            panic!("injected observer panic");
        }
    }

    #[test]
    fn portable_core_boundaries_fit_database_values_exactly() {
        let policy = FixedWindowPolicy::new(
            PolicyId::new("portable.maximum").unwrap(),
            ScopeId::new("subject").unwrap(),
            MAX_LIMIT,
            MAX_WINDOW,
        )
        .unwrap();
        let check = Check::with_cost(&policy, key(1), MAX_LIMIT).unwrap();

        assert_eq!(database_integer(policy.limit()), i64::MAX);
        assert_eq!(database_integer(check.cost()), i64::MAX);
        let interval = PgInterval::try_from(policy.window()).unwrap();
        assert_eq!(interval.months, 0);
        assert_eq!(interval.days, 0);
        assert_eq!(
            interval.microseconds,
            i64::try_from(MAX_WINDOW_MILLIS * 1_000).unwrap()
        );
    }

    #[test]
    fn row_lock_permutation_orders_complete_counter_keys() {
        let alpha_client = policy("alpha", "client");
        let alpha_identity = policy("alpha", "identity");
        let beta_client = policy("beta", "client");
        let checks = [
            Check::new(&beta_client, key(0)),
            Check::new(&alpha_identity, key(0)),
            Check::new(&alpha_client, key(1)),
            Check::new(&alpha_client, key(0)),
        ];

        let input = BatchSqlInput::from_checks(&checks);
        let locked_keys = input
            .lock_input_positions
            .iter()
            .map(|position| checks[usize::try_from(*position - 1).unwrap()].counter_key())
            .collect::<Vec<_>>();

        assert!(locked_keys.windows(2).all(|pair| pair[0] < pair[1]));
    }

    #[test]
    fn advisory_lock_protocol_has_a_golden_vector() {
        let policy = policy("login", "client");
        let check = Check::new(&policy, key(7));

        assert_eq!(
            advisory_lock_id(check.counter_key()),
            4_549_358_434_381_602_087
        );
    }

    #[test]
    fn capacity_shard_protocol_has_a_golden_vector() {
        let policy = policy("login", "client");
        let check = Check::new(&policy, key(7));

        assert_eq!(capacity_shard(check.counter_key()), 141);
    }

    #[tokio::test]
    async fn observer_reports_connection_free_operations_and_isolates_panics() {
        let pool = sqlx::postgres::PgPoolOptions::new()
            .connect_lazy("postgresql://runlimit:runlimit@127.0.0.1:1/runlimit")
            .expect("syntactically valid database URL");
        let observer = Arc::new(RecordingObserver::default());
        let limiter = PostgresLimiter::new(pool.clone()).with_observer(observer.clone());

        assert_eq!(
            limiter.check_all(&[]).await.unwrap(),
            BatchDecision::allowed(Vec::new())
        );
        assert_eq!(limiter.cleanup_expired(0).await.unwrap(), 0);
        assert_eq!(
            observer.events.lock().unwrap().as_slice(),
            [(
                AdmissionOperation::Batch,
                AdmissionOutcome::Allowed,
                ConsumptionStatus::NotConsumed,
            )]
        );
        assert_eq!(
            observer.cleanups.lock().unwrap().as_slice(),
            [(0, Some(0), ConsumptionStatus::Consumed)]
        );

        let panicking = PostgresLimiter::new(pool).with_observer(Arc::new(PanickingObserver));
        assert_eq!(
            panicking.check_all(&[]).await.unwrap(),
            BatchDecision::allowed(Vec::new())
        );
        assert_eq!(panicking.cleanup_expired(0).await.unwrap(), 0);
    }

    #[tokio::test]
    async fn failed_postgres_batches_preserve_one_item_policy_metadata() {
        let pool = sqlx::postgres::PgPoolOptions::new()
            .connect_lazy("postgresql://runlimit:runlimit@127.0.0.1:1/runlimit")
            .expect("syntactically valid database URL");
        let observer = Arc::new(AdmissionMetadataObserver::default());
        let limiter = PostgresLimiter::new(pool).with_observer(observer.clone());
        let first_policy = policy("observed.failure.first", "client");
        let second_policy = policy("observed.failure.second", "client");
        let first = Check::new(&first_policy, key(1));
        let second = Check::new(&second_policy, key(2));
        let elapsed = Duration::from_millis(5);

        limiter.observe_single_admission(&first, &Err(CheckError::CommitTimedOut), elapsed);
        limiter.observe_batch_admission(&[first], &Err(CheckError::CommitTimedOut), elapsed);
        limiter.observe_batch_admission(
            &[first, second],
            &Err(CheckError::CommitTimedOut),
            elapsed,
        );

        assert_eq!(
            observer.admissions.lock().unwrap().as_slice(),
            [
                RecordedAdmissionMetadata {
                    operation: AdmissionOperation::Check,
                    batch_size: 1,
                    has_policy_id: true,
                    has_scope_id: true,
                    has_policy_fingerprint: true,
                    consumption: ConsumptionStatus::PossiblyConsumed,
                },
                RecordedAdmissionMetadata {
                    operation: AdmissionOperation::Batch,
                    batch_size: 1,
                    has_policy_id: true,
                    has_scope_id: true,
                    has_policy_fingerprint: true,
                    consumption: ConsumptionStatus::PossiblyConsumed,
                },
                RecordedAdmissionMetadata {
                    operation: AdmissionOperation::Batch,
                    batch_size: 2,
                    has_policy_id: false,
                    has_scope_id: false,
                    has_policy_fingerprint: false,
                    consumption: ConsumptionStatus::PossiblyConsumed,
                },
            ]
        );
    }

    #[tokio::test]
    async fn cleanup_observations_map_every_backend_effect_class() {
        let pool = sqlx::postgres::PgPoolOptions::new()
            .connect_lazy("postgresql://runlimit:runlimit@127.0.0.1:1/runlimit")
            .expect("syntactically valid database URL");
        let observer = Arc::new(RecordingObserver::default());
        let limiter = PostgresLimiter::new(pool).with_observer(observer.clone());
        let elapsed = Duration::from_millis(5);

        limiter.observe_cleanup(7, &Ok(3), elapsed);
        limiter.observe_cleanup(
            7,
            &Err(MaintenanceError::Database(sqlx::Error::RowNotFound)),
            elapsed,
        );
        limiter.observe_cleanup(
            7,
            &Err(MaintenanceError::TimedOutBeforeCommit {
                operation: "testing",
            }),
            elapsed,
        );
        limiter.observe_cleanup(
            7,
            &Err(MaintenanceError::CommitOutcomeUnknown(
                sqlx::Error::RowNotFound,
            )),
            elapsed,
        );
        limiter.observe_cleanup(7, &Err(MaintenanceError::CommitTimedOut), elapsed);

        assert_eq!(
            observer.cleanups.lock().unwrap().as_slice(),
            [
                (7, Some(3), ConsumptionStatus::Consumed),
                (7, Some(0), ConsumptionStatus::NotConsumed),
                (7, Some(0), ConsumptionStatus::NotConsumed),
                (7, None, ConsumptionStatus::PossiblyConsumed),
                (7, None, ConsumptionStatus::PossiblyConsumed),
            ]
        );
    }

    #[test]
    fn response_metadata_subtracts_only_authoritative_elapsed_time() {
        let allowed = PendingAllowance {
            limit: 10,
            remaining: 4,
            reset_from_sample: Duration::from_millis(250),
        }
        .finish(Duration::from_millis(80));
        let denied = PendingDenial::Quota(PendingQuotaDenial {
            limit: 10,
            retry_from_sample: Duration::from_millis(150),
        })
        .finish(Duration::from_millis(80));

        assert_eq!(
            allowed.replenishes_after(),
            Some(Duration::from_millis(170))
        );
        assert_eq!(denied.retry_after(), Some(Duration::from_millis(70)));
        assert_eq!(denied.retry_after_seconds(), Some(1));
    }

    #[tokio::test]
    async fn denied_decision_survives_rollback_failure_and_discards_connection() {
        let outcome = finish_denied_transaction(
            Instant::now() + Duration::from_secs(1),
            PendingBatchDenial::Enforced {
                index: 2,
                denial: PendingDenial::Quota(PendingQuotaDenial {
                    limit: 10,
                    retry_from_sample: Duration::from_millis(150),
                }),
            },
            Duration::from_millis(20),
            std::future::ready(Err(sqlx::Error::Protocol(
                "injected rollback failure".to_owned(),
            ))),
        )
        .await;

        let ConnectionOutcome::MustClose(decision) = outcome else {
            panic!("rollback failure must close the connection")
        };
        assert_eq!(decision.denied_index(), Some(2));
        assert_eq!(
            decision.quota_denial(),
            Some(QuotaDenial::try_new(10, Duration::from_millis(130)).unwrap())
        );
    }

    #[tokio::test]
    async fn denied_decision_survives_rollback_deadline_and_discards_connection() {
        let outcome = finish_denied_transaction(
            Instant::now(),
            PendingBatchDenial::Enforced {
                index: 0,
                denial: PendingDenial::Quota(PendingQuotaDenial {
                    limit: 1,
                    retry_from_sample: Duration::from_millis(50),
                }),
            },
            Duration::ZERO,
            std::future::pending(),
        )
        .await;

        let ConnectionOutcome::MustClose(decision) = outcome else {
            panic!("rollback deadline must close the connection")
        };
        assert_eq!(decision.denied_index(), Some(0));
        assert_eq!(decision.quota_denial().map(QuotaDenial::capacity), Some(1));
    }

    #[tokio::test]
    async fn shadow_quota_denial_is_reported_after_rollback() {
        let outcome = finish_denied_transaction(
            Instant::now() + Duration::from_secs(1),
            PendingBatchDenial::Shadow {
                index: 0,
                denial: PendingQuotaDenial {
                    limit: 1,
                    retry_from_sample: Duration::from_secs(1),
                },
            },
            Duration::ZERO,
            std::future::ready(Ok(())),
        )
        .await;

        let ConnectionOutcome::Reusable(decision) = outcome else {
            panic!("successful rollback must reuse the connection")
        };
        assert!(decision.is_shadow_denied());
        assert_eq!(decision.denied_index(), Some(0));
        assert_eq!(decision.quota_denial().map(QuotaDenial::capacity), Some(1));
    }

    #[test]
    fn configuration_bounds_batches_and_deadlines() {
        let defaults = PostgresConfig::new();
        assert_eq!(defaults.maximum_rows_per_shard(), 4_096);
        assert_eq!(defaults.max_batch_size(), 32);
        assert_eq!(defaults.pool_acquire_timeout(), Duration::from_secs(3));
        assert_eq!(defaults.operation_timeout(), Duration::from_secs(3));
        assert_eq!(
            defaults.with_maximum_rows_per_shard(0),
            Err(PostgresConfigError::ZeroMaximumRowsPerShard)
        );
        assert_eq!(
            defaults.with_maximum_rows_per_shard(HARD_MAX_ROWS_PER_SHARD + 1),
            Err(PostgresConfigError::MaximumRowsPerShardTooLarge {
                actual: HARD_MAX_ROWS_PER_SHARD + 1,
                maximum: HARD_MAX_ROWS_PER_SHARD,
            })
        );
        assert_eq!(
            defaults.with_max_batch_size(0),
            Err(PostgresConfigError::ZeroBatchSize)
        );
        assert_eq!(
            defaults.with_pool_acquire_timeout(Duration::ZERO),
            Err(PostgresConfigError::ZeroPoolAcquireTimeout)
        );
        assert_eq!(
            defaults.with_pool_acquire_timeout(Duration::from_secs(61)),
            Err(PostgresConfigError::PoolAcquireTimeoutTooLong {
                actual: Duration::from_secs(61),
                maximum: Duration::from_secs(60),
            })
        );
        assert_eq!(
            defaults.with_operation_timeout(Duration::ZERO),
            Err(PostgresConfigError::ZeroOperationTimeout)
        );
        assert_eq!(
            defaults.with_operation_timeout(Duration::from_secs(61)),
            Err(PostgresConfigError::OperationTimeoutTooLong {
                actual: Duration::from_secs(61),
                maximum: Duration::from_secs(60),
            })
        );
    }

    #[tokio::test]
    async fn duplicate_storage_keys_fail_before_connecting() {
        let alpha = policy("alpha", "client");
        let beta = policy("beta", "client");
        let checks = [
            Check::new(&beta, key(7)),
            Check::new(&beta, key(7)),
            Check::new(&alpha, key(8)),
            Check::new(&alpha, key(8)),
        ];
        let pool = sqlx::postgres::PgPoolOptions::new()
            .connect_lazy("postgresql://runlimit:runlimit@127.0.0.1:1/runlimit")
            .expect("syntactically valid database URL");
        let limiter = PostgresLimiter::new(pool);

        let error = limiter
            .check_all(&checks)
            .await
            .expect_err("duplicate keys must be rejected");

        assert!(matches!(
            error,
            CheckError::InvalidBatch(BatchError::DuplicateKey {
                first_index: 0,
                duplicate_index: 1,
            })
        ));
    }

    #[tokio::test]
    async fn oversized_batch_fails_before_connecting() {
        let first_policy = policy("login", "client");
        let second_policy = policy("signup", "client");
        let checks = [
            Check::new(&first_policy, key(1)),
            Check::new(&second_policy, key(2)),
        ];
        let pool = sqlx::postgres::PgPoolOptions::new()
            .connect_lazy("postgresql://runlimit:runlimit@127.0.0.1:1/runlimit")
            .expect("syntactically valid database URL");
        let config = PostgresConfig::new()
            .with_max_batch_size(1)
            .expect("one is a valid batch limit");
        let limiter = PostgresLimiter::with_config(pool, config);

        let error = limiter
            .check_all(&checks)
            .await
            .expect_err("oversized batch must be rejected");

        assert!(matches!(
            error,
            CheckError::InvalidBatch(BatchError::BatchTooLarge {
                actual: 2,
                maximum: 1,
            })
        ));
    }

    #[test]
    fn error_reports_consumption_uncertainty() {
        let definite = CheckError::DefinitelyNotConsumed(sqlx::Error::RowNotFound);
        let unknown = CheckError::CommitOutcomeUnknown(sqlx::Error::RowNotFound);
        let rolled_back_invariant = CheckError::ResponseInvariant;
        let committed_invariant = CheckError::CommittedResponseInvariant;
        let cleanup_definite = MaintenanceError::Database(sqlx::Error::RowNotFound);
        let cleanup_unknown = MaintenanceError::CommitOutcomeUnknown(sqlx::Error::RowNotFound);

        assert!(!definite.may_have_consumed_quota());
        assert!(unknown.may_have_consumed_quota());
        assert!(CheckError::CommitTimedOut.may_have_consumed_quota());
        assert!(!rolled_back_invariant.may_have_consumed_quota());
        assert!(committed_invariant.may_have_consumed_quota());
        assert_eq!(
            check_error_consumption(&definite),
            ConsumptionStatus::NotConsumed
        );
        assert_eq!(
            check_error_consumption(&unknown),
            ConsumptionStatus::PossiblyConsumed
        );
        assert_eq!(
            check_error_consumption(&CheckError::CommitTimedOut),
            ConsumptionStatus::PossiblyConsumed
        );
        assert_eq!(
            check_error_consumption(&rolled_back_invariant),
            ConsumptionStatus::NotConsumed
        );
        assert_eq!(
            check_error_consumption(&committed_invariant),
            ConsumptionStatus::Consumed
        );
        assert!(!cleanup_definite.may_have_removed_rows());
        assert!(cleanup_unknown.may_have_removed_rows());
        assert!(MaintenanceError::CommitTimedOut.may_have_removed_rows());
    }

    #[test]
    fn malformed_single_responses_preserve_consumption_status() {
        let denial = QuotaDenial::try_new(1, Duration::from_secs(1)).unwrap();

        let committed = single_decision_from_batch(BatchDecision::allowed(Vec::new()))
            .expect_err("malformed allowed response follows a confirmed commit");
        let rolled_back = single_decision_from_batch(BatchDecision::denied(1, denial))
            .expect_err("malformed denied response follows a rollback");

        assert!(matches!(committed, CheckError::CommittedResponseInvariant));
        assert!(committed.may_have_consumed_quota());
        assert!(matches!(rolled_back, CheckError::ResponseInvariant));
        assert!(!rolled_back.may_have_consumed_quota());
    }

    #[test]
    fn cleanup_query_is_bounded_and_nonblocking() {
        assert_eq!(
            CLEANUP_SQL.matches("pg_catalog.clock_timestamp()").count(),
            1
        );
        assert!(CLEANUP_SQL.contains("AS MATERIALIZED"));
        assert!(CLEANUP_SQL.contains("SELECT sampled_at\n        FROM authoritative_time"));
        assert!(!CLEANUP_SQL.contains("window_expires_at <= pg_catalog.clock_timestamp()"));
        assert!(CLEANUP_SQL.contains("LIMIT $1"));
        assert!(CLEANUP_SQL.contains("FOR UPDATE SKIP LOCKED"));
        assert!(CLEANUP_SQL.contains("ORDER BY capacity.capacity_shard"));
        assert!(CLEANUP_SQL.contains("FOR UPDATE OF capacity"));
    }

    #[test]
    fn every_production_clock_call_is_catalog_qualified() {
        for (name, sql) in [
            ("batch preflight", BATCH_PREFLIGHT_SQL),
            ("batch upsert", BATCH_UPSERT_SQL),
            ("cleanup", CLEANUP_SQL),
        ] {
            let without_qualified_calls = sql.replace("pg_catalog.clock_timestamp()", "");
            assert!(
                !without_qualified_calls.contains("clock_timestamp()"),
                "{name} contains a search_path-resolved clock_timestamp() call"
            );
        }
    }
}