rustango 0.61.13

Batteries-included web framework for Rust: ORM + migrations + auto-admin + multi-tenancy + audit log + auth (sessions, JWT, OAuth2/OIDC, HMAC) + APIs (ViewSet, OpenAPI auto-derive, JSON:API) + jobs (in-mem + Postgres) + email + media (S3 / R2 / B2 / MinIO + presigned uploads + collections + tags) + production middleware (CSRF, CSP, rate-limiting, compression, idempotency, etc.).
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
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
//! One gate in front of every built-in password login.
//!
//! [`LoginThrottle::begin`] runs before the user lookup and reserves, in
//! order, an account-lock attempt, a per-IP token and a token from the
//! scope's global limit. Reserving before the password check means a
//! parallel burst cannot all pass a peek (#2378). A request refused by
//! one check spends nothing from the later ones. A limit full only with
//! attempts still in flight in this process makes the next one wait, so
//! parallel right credentials are delayed, never refused.
//!
//! - **Account lock**: keyed by the normalized *submitted* username, so
//!   an unknown name is limited exactly like a real one. Once the row is
//!   found, [`LoginAttempt::resolve`] adds the stored username, so every
//!   spelling the database matches to one account shares one lock.
//! - **Per IP** (IPv6 by /64) and **per scope** ([`LoginScope`]): only
//!   failed attempts use them up; a successful login gives its token back,
//!   so credentials sent on every request (HTTP Basic, API keys) only hold
//!   a token while they are checked. Only failures lock an account.
//! - **Known clients**: a username that signed in from an IP within
//!   [`KNOWN_CLIENT_TTL`] skips a spent global limit from that IP, so a
//!   spray from many IPs cannot lock every user out. Its per-IP limit and
//!   account lock still apply.
//!
//! Shared across scopes: the per-IP buckets (agent secrets have their
//! own), the account-lock store and the password-hashing queue
//! ([`crate::passwords`]).
//!
//! State: the per-IP and global buckets are in-process unless built
//! with [`LoginThrottle::with_cache`]; the account lock uses
//! [`crate::account_lockout::shared`]. Back both with a shared cache at
//! boot when you run more than one replica.
//!
//! Behind a reverse proxy, mount [`crate::real_ip::RealIpLayer`] with
//! trusted proxies. Without it every client shares the proxy's per-IP
//! bucket (a warning is logged once when forwarding headers arrive).

use std::sync::Arc;
use std::time::Duration;

use axum::extract::FromRequestParts;
use axum::http::request::Parts;
use axum::http::{Extensions, HeaderMap};
use axum::response::Response;

use tokio::sync::Notify;

use crate::account_lockout::{Reservation, ReservedSlot};
use crate::cache::BoxedCache;
use crate::rate_limit::RateLimitLayer;
use crate::rate_limit_cache::CacheRateLimitLayer;

/// Default failed attempts per IP per [`DEFAULT_IP_WINDOW_SECS`].
pub const DEFAULT_IP_LIMIT: u32 = 20;
/// Default per-IP window.
pub const DEFAULT_IP_WINDOW_SECS: u64 = 60;
/// Default failed attempts per scope per [`DEFAULT_GLOBAL_WINDOW_SECS`].
pub const DEFAULT_GLOBAL_LIMIT: u32 = 600;
/// Default global window.
pub const DEFAULT_GLOBAL_WINDOW_SECS: u64 = 60;
/// How long a sign-in lets its username and IP skip a spent global limit.
pub const KNOWN_CLIENT_TTL: Duration = Duration::from_secs(30 * 24 * 3600);
/// Known clients one process remembers without a shared cache; the least
/// recently used go first.
const KNOWN_CLIENT_MEMORY_CAP: usize = 10_000;

/// Limits for [`LoginThrottle`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LoginLimits {
    /// Failed attempts one IP may make per `ip_window`.
    pub ip_limit: u32,
    pub ip_window: Duration,
    /// Failed attempts all clients together may make per scope per
    /// `global_window`.
    pub global_limit: u32,
    pub global_window: Duration,
}

impl Default for LoginLimits {
    fn default() -> Self {
        Self {
            ip_limit: DEFAULT_IP_LIMIT,
            ip_window: Duration::from_secs(DEFAULT_IP_WINDOW_SECS),
            global_limit: DEFAULT_GLOBAL_LIMIT,
            global_window: Duration::from_secs(DEFAULT_GLOBAL_WINDOW_SECS),
        }
    }
}

impl LoginLimits {
    /// Read the `[auth] login_*` keys; unset keys keep the defaults.
    #[cfg(feature = "config")]
    #[must_use]
    pub fn from_settings(s: &crate::config::AuthSettings) -> Self {
        let d = Self::default();
        Self {
            ip_limit: s.login_ip_limit.unwrap_or(d.ip_limit),
            ip_window: s
                .login_ip_window_secs
                .map_or(d.ip_window, Duration::from_secs),
            global_limit: s.login_global_limit.unwrap_or(d.global_limit),
            global_window: s
                .login_global_window_secs
                .map_or(d.global_window, Duration::from_secs),
        }
    }
}

/// Which login an attempt belongs to. Each scope has its own global
/// limit and its own account locks.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum LoginScope {
    /// The bare admin login form.
    Admin,
    /// The operator console login form.
    Operator,
    /// A tenant's admin login form and JWT login (one user table).
    Tenant(String),
    /// HTTP Basic against a tenant. Only failures keep their token.
    TenantBasic(String),
    /// API keys against a tenant. Only failures keep their token; no account lock.
    TenantApiKey(String),
    /// MCP agent secrets against a tenant, as for API keys.
    TenantAgent(String),
}

impl LoginScope {
    fn key(&self) -> String {
        match self {
            Self::Admin => "admin".to_owned(),
            Self::Operator => "operator".to_owned(),
            Self::Tenant(slug) => format!("tenant:{slug}"),
            Self::TenantBasic(slug) => format!("tenant-basic:{slug}"),
            Self::TenantApiKey(slug) => format!("tenant-apikey:{slug}"),
            Self::TenantAgent(slug) => format!("tenant-agent:{slug}"),
        }
    }

    fn locks_accounts(&self) -> bool {
        !matches!(self, Self::TenantApiKey(_) | Self::TenantAgent(_))
    }

    /// The per-IP bucket key. Agent secrets count apart, so a looping
    /// agent cannot spend the IP's human logins.
    fn ip_bucket(&self, ip: &str) -> String {
        match self {
            Self::TenantAgent(_) => format!("agent/{ip}"),
            _ => ip.to_owned(),
        }
    }
}

/// Why a login was refused before its password was checked, or while
/// waiting to check it. The response is the same whether or not the
/// account exists.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LoginRefused {
    /// Too many attempts: `429` with `Retry-After`.
    Throttled { retry_after_secs: u64 },
    /// No password-hashing slot freed up in time: `503` with `Retry-After`.
    Busy,
}

impl LoginRefused {
    /// The `429` / `503` response, with `Retry-After`.
    #[must_use]
    pub fn into_response(self) -> Response {
        use axum::response::IntoResponse as _;
        match self {
            Self::Throttled { retry_after_secs } => {
                crate::api_errors::ApiError::rate_limited_response(
                    "too many login attempts",
                    retry_after_secs,
                )
            }
            Self::Busy => {
                let mut resp =
                    crate::api_errors::ApiError::service_unavailable("login is busy, try again")
                        .into_response();
                resp.headers_mut()
                    .insert(axum::http::header::RETRY_AFTER, 1u64.into());
                resp
            }
        }
    }
}

impl axum::response::IntoResponse for LoginRefused {
    fn into_response(self) -> Response {
        LoginRefused::into_response(self)
    }
}

/// The client address a login is limited by: a trusted forwarded IP,
/// else the connecting socket, else unknown (no per-IP limit). IPv6
/// addresses are grouped by /64.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ClientIp(Option<String>);

impl ClientIp {
    /// Resolve from request parts, as [`crate::rate_limit`] does.
    #[must_use]
    pub fn from_parts(extensions: &Extensions, headers: &HeaderMap) -> Self {
        Self(crate::rate_limit::client_ip(extensions, headers).map(crate::rate_limit::ip_bucket))
    }
}

impl<S: Send + Sync> FromRequestParts<S> for ClientIp {
    type Rejection = std::convert::Infallible;

    async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
        Ok(Self::from_parts(&parts.extensions, &parts.headers))
    }
}

struct Buckets {
    limits: LoginLimits,
    ip: Limit,
    /// Keyed by [`LoginScope::key`].
    global: Limit,
    /// Username and IP pairs that signed in; see [`KNOWN_CLIENT_TTL`].
    known: BoxedCache,
    in_flight: InFlight,
    /// A test's own lockout in place of the shared one.
    #[cfg(test)]
    lockout: Option<&'static crate::account_lockout::Lockout>,
}

/// How often a waiting attempt re-checks a limit held elsewhere.
const IN_FLIGHT_POLL: Duration = Duration::from_millis(50);

/// Attempts this process admitted and has not settled, per gate (an
/// account key, `ip:<ip>` or `global:<scope>`). A limit that is full only
/// because of these makes the next attempt wait, not fail.
#[derive(Default)]
struct InFlight(std::sync::Mutex<std::collections::HashMap<String, (usize, Arc<Notify>)>>);

impl InFlight {
    fn map(
        &self,
    ) -> std::sync::MutexGuard<'_, std::collections::HashMap<String, (usize, Arc<Notify>)>> {
        self.0
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
    }

    fn enter(&self, gate: &str) {
        self.map().entry(gate.to_owned()).or_default().0 += 1;
    }

    fn leave(&self, gate: &str) {
        let mut map = self.map();
        if let Some((n, notify)) = map.get_mut(gate) {
            *n -= 1;
            notify.notify_waiters();
            if *n == 0 {
                map.remove(gate);
            }
        }
    }

    /// `Some` while this process has attempts in flight at `gate`.
    fn busy(&self, gate: &str) -> Option<Arc<Notify>> {
        self.map().get(gate).map(|(_, notify)| Arc::clone(notify))
    }
}

/// The gates one attempt holds; leaving them on drop wakes the waiters.
struct Gates {
    buckets: Arc<Buckets>,
    held: Vec<String>,
}

impl Gates {
    fn enter(&mut self, gate: String) {
        self.buckets.in_flight.enter(&gate);
        self.held.push(gate);
    }
}

impl Drop for Gates {
    fn drop(&mut self) {
        for gate in &self.held {
            self.buckets.in_flight.leave(gate);
        }
    }
}

/// Why an attempt was not admitted.
enum Blocked {
    /// Locked, or a limit spent by settled failures.
    Refused(LoginRefused),
    /// Full at `gate`; it may free up when an attempt there settles.
    Full { gate: String, retry_after_secs: u64 },
}

impl Buckets {
    fn lockout(&self) -> &'static crate::account_lockout::Lockout {
        #[cfg(test)]
        if let Some(l) = self.lockout {
            return l;
        }
        crate::account_lockout::shared()
    }

    /// Wait for an attempt in flight at the full gate to settle; refuse
    /// when none is, and answer busy past `deadline`.
    async fn wait(
        &self,
        blocked: Blocked,
        deadline: tokio::time::Instant,
    ) -> Result<(), LoginRefused> {
        let (gate, retry_after_secs) = match blocked {
            Blocked::Refused(r) => return Err(r),
            Blocked::Full {
                gate,
                retry_after_secs,
            } => (gate, retry_after_secs),
        };
        let Some(notify) = self.in_flight.busy(&gate) else {
            return Err(LoginRefused::Throttled { retry_after_secs });
        };
        let notified = notify.notified();
        tokio::pin!(notified);
        notified.as_mut().enable();
        if self.in_flight.busy(&gate).is_none() {
            return Ok(());
        }
        let left = deadline.saturating_duration_since(tokio::time::Instant::now());
        if left.is_zero() {
            return Err(LoginRefused::Busy);
        }
        let _ = tokio::time::timeout(left.min(IN_FLIGHT_POLL), notified).await;
        Ok(())
    }

    /// `true` when `username` signed in from `ip` in this scope lately.
    async fn is_known(&self, scope: &LoginScope, ip: &ClientIp, username: &str) -> bool {
        let Some(ip) = ip.0.as_deref().filter(|_| scope.locks_accounts()) else {
            return false;
        };
        let key = known_key(&account_key(&scope.key(), username), ip);
        matches!(self.known.get(&key).await, Ok(Some(_)))
    }
}

fn known_key(account_key: &str, ip: &str) -> String {
    format!("login-known:{account_key}:{ip}")
}

fn ip_gate(ip: &str) -> String {
    format!("ip:{ip}")
}

fn global_gate(scope_key: &str) -> String {
    format!("global:{scope_key}")
}

/// Where one limit counts.
enum Limit {
    /// A token bucket in this process.
    Memory(RateLimitLayer),
    /// A fixed-window counter in a cache; on a cache error the attempt
    /// counts in the in-process `fallback`, so the limit never opens.
    Cache {
        shared: CacheRateLimitLayer,
        fallback: RateLimitLayer,
    },
}

/// What [`Limit::take`] counted, so the refund goes to the same bucket.
#[derive(Debug, Clone, Copy)]
enum Spent {
    Nothing,
    Memory,
    /// The cache window it was counted in.
    Window(u64),
}

impl Limit {
    /// Spend one token. A refused take counts nothing, so attempts that
    /// wait and retry do not fill the window themselves.
    async fn take(&self, key: &str) -> Result<Spent, u64> {
        match self {
            Self::Memory(l) => l.take(key).await.map(|_| Spent::Memory),
            Self::Cache { shared, fallback } => match shared.try_take(key).await {
                Ok(Ok((_, window))) => Ok(Spent::Window(window)),
                Ok(Err((secs, window))) => {
                    shared.give_back_at(key, window).await;
                    Err(secs)
                }
                Err(e) => {
                    tracing::warn!(target: "rustango::rate_limit", error = %e, "login limit cache incr failed");
                    fallback.take(key).await.map(|_| Spent::Memory)
                }
            },
        }
    }

    async fn peek(&self, key: &str) -> Result<(), u64> {
        match self {
            Self::Memory(l) => l.peek(key).await,
            Self::Cache { shared, fallback } => match shared.try_peek(key).await {
                Ok(r) => r,
                Err(e) => {
                    tracing::warn!(target: "rustango::rate_limit", error = %e, "login limit cache read failed");
                    fallback.peek(key).await
                }
            },
        }
    }

    async fn give_back(&self, key: &str, spent: Spent) {
        match (self, spent) {
            (Self::Memory(l) | Self::Cache { fallback: l, .. }, Spent::Memory) => {
                l.give_back(key).await;
            }
            (Self::Cache { shared, .. }, Spent::Window(w)) => shared.give_back_at(key, w).await,
            _ => {}
        }
    }

    fn cache(&self) -> Option<&BoxedCache> {
        match self {
            Self::Memory(_) => None,
            Self::Cache { shared, .. } => Some(shared.cache()),
        }
    }
}

/// The login gate. Use [`shared`] unless you are testing.
pub struct LoginThrottle(Arc<Buckets>);

impl LoginThrottle {
    /// Per-IP and global limits counted in this process.
    #[must_use]
    pub fn new(limits: LoginLimits) -> Self {
        Self(Arc::new(Buckets {
            limits,
            ip: Limit::Memory(memory_ip(limits)),
            global: Limit::Memory(memory_global(limits)),
            known: Arc::new(
                crate::cache::InMemoryCache::new().with_max_entries(KNOWN_CLIENT_MEMORY_CAP),
            ),
            in_flight: InFlight::default(),
            #[cfg(test)]
            lockout: None,
        }))
    }

    /// Per-IP and global limits counted in `cache`, so every replica on
    /// one Redis or database cache shares them (#1809). On a cache error
    /// they count in process.
    ///
    /// Fixed windows: a burst across a window edge can reach 2× a limit.
    /// Keys are `login-ip:*` / `login-global:*`, so apps on one cache share
    /// them; give each app its own [`crate::cache::ScopedCache`].
    #[must_use]
    pub fn with_cache(limits: LoginLimits, cache: BoxedCache) -> Self {
        let ip = CacheRateLimitLayer::new(cache.clone(), limits.ip_limit.max(1), limits.ip_window);
        let global = CacheRateLimitLayer::new(
            cache.clone(),
            limits.global_limit.max(1),
            limits.global_window,
        );
        Self(Arc::new(Buckets {
            limits,
            ip: Limit::Cache {
                shared: ip.key_prefix("login-ip"),
                fallback: memory_ip(limits),
            },
            global: Limit::Cache {
                shared: global.key_prefix("login-global"),
                fallback: memory_global(limits),
            },
            known: cache,
            in_flight: InFlight::default(),
            #[cfg(test)]
            lockout: None,
        }))
    }

    /// `true` when each replica counts its own per-IP and global limits.
    #[must_use]
    pub fn is_process_local(&self) -> bool {
        self.0.ip.cache().is_none_or(|c| c.is_process_local())
    }

    /// The warning for limits that never count or count per process.
    fn store_warning(&self) -> Option<&'static str> {
        if self.0.ip.cache().is_some_and(|c| c.stores_nothing()) {
            return Some(NULL_STORE_WARNING);
        }
        self.is_process_local().then_some(PROCESS_LOCAL_WARNING)
    }

    fn warn_once_on_weak_store(&self) {
        static WARNED: std::sync::Once = std::sync::Once::new();
        if let Some(msg) = self.store_warning() {
            WARNED.call_once(|| tracing::warn!(target: "rustango::rate_limit", "{msg}"));
        }
    }

    /// The limits this gate enforces.
    #[must_use]
    pub fn limits(&self) -> LoginLimits {
        self.0.limits
    }

    /// Admit one attempt at `username` in `scope`. Call before the user
    /// lookup, then report the outcome on the returned [`LoginAttempt`].
    /// `username` is ignored for [`LoginScope::TenantApiKey`] and
    /// [`LoginScope::TenantAgent`].
    ///
    /// A limit that is full only with attempts still in flight here waits
    /// for one to settle, so right credentials are never refused by
    /// parallel successes; wrong ones still get no more than each limit.
    ///
    /// # Errors
    /// [`LoginRefused::Throttled`] when the account is locked or a limit
    /// is spent by failures; [`LoginRefused::Busy`] when in-flight
    /// attempts do not settle within [`crate::passwords::hash_wait`].
    pub async fn begin(
        &self,
        scope: &LoginScope,
        ip: &ClientIp,
        username: &str,
    ) -> Result<LoginAttempt, LoginRefused> {
        let deadline = tokio::time::Instant::now() + crate::passwords::hash_wait();
        self.begin_until(scope, ip, username, deadline).await
    }

    /// [`Self::begin`], giving up at `deadline`.
    async fn begin_until(
        &self,
        scope: &LoginScope,
        ip: &ClientIp,
        username: &str,
        deadline: tokio::time::Instant,
    ) -> Result<LoginAttempt, LoginRefused> {
        self.warn_once_on_weak_store();
        loop {
            match self.try_begin(scope, ip, username).await {
                Ok(held) => return Ok(LoginAttempt(Some(held))),
                Err(blocked) => self.0.wait(blocked, deadline).await?,
            }
        }
    }

    /// Reserve the account, per-IP and global slots in that order. A
    /// request refused by one spends nothing from the later ones.
    async fn try_begin(
        &self,
        scope: &LoginScope,
        ip: &ClientIp,
        username: &str,
    ) -> Result<Held, Blocked> {
        let b = &self.0;
        let mut held = Held {
            buckets: Arc::clone(b),
            scope_key: scope.key(),
            ip: ip.0.clone(),
            ip_bucket: ip.0.as_deref().map(|ip| scope.ip_bucket(ip)),
            spent: (Spent::Nothing, Spent::Nothing),
            locks: Vec::with_capacity(2),
            gates: Gates {
                buckets: Arc::clone(b),
                held: Vec::with_capacity(4),
            },
        };
        if scope.locks_accounts() {
            held.reserve_lock(account_key(&held.scope_key, username))
                .await?;
        }
        if let Some(ip) = held.ip_bucket.clone() {
            match b.ip.take(&ip).await {
                Ok(spent) => {
                    held.spent.0 = spent;
                    held.gates.enter(ip_gate(&ip));
                }
                Err(retry_after_secs) => {
                    held.release().await;
                    let gate = ip_gate(&ip);
                    return Err(Blocked::Full {
                        gate,
                        retry_after_secs,
                    });
                }
            }
        }
        match b.global.take(&held.scope_key).await {
            Ok(spent) => {
                held.spent.1 = spent;
                held.gates.enter(global_gate(&held.scope_key));
            }
            Err(_) if b.is_known(scope, ip, username).await => {}
            Err(retry_after_secs) => {
                let gate = global_gate(&held.scope_key);
                held.release().await;
                return Err(Blocked::Full {
                    gate,
                    retry_after_secs,
                });
            }
        }
        Ok(held)
    }

    /// Refuse, before the user lookup, what [`Self::begin`] would refuse
    /// for good: a locked account or a limit spent by failures. Reserves
    /// nothing; call [`Self::begin`] before checking the password.
    ///
    /// # Errors
    /// [`LoginRefused::Throttled`].
    pub async fn check(
        &self,
        scope: &LoginScope,
        ip: &ClientIp,
        username: &str,
    ) -> Result<(), LoginRefused> {
        let scope_key = scope.key();
        if scope.locks_accounts() {
            let lockout = self.0.lockout();
            if lockout.is_locked(&account_key(&scope_key, username)).await {
                return Err(LoginRefused::Throttled {
                    retry_after_secs: lockout.lock_duration().as_secs().max(1),
                });
            }
        }
        let b = &self.0;
        let spent = |gate: &str, r: Result<(), u64>| match r {
            Err(retry_after_secs) if b.in_flight.busy(gate).is_none() => {
                Err(LoginRefused::Throttled { retry_after_secs })
            }
            _ => Ok(()),
        };
        if let Some(ip) = ip.0.as_deref().map(|ip| scope.ip_bucket(ip)) {
            spent(&ip_gate(&ip), b.ip.peek(&ip).await)?;
        }
        let global = b.global.peek(&scope_key).await;
        if global.is_err() && b.is_known(scope, ip, username).await {
            return Ok(());
        }
        spent(&global_gate(&scope_key), global)
    }

    /// Admit a credential sent on every request (HTTP Basic, an API key)
    /// before it is compared. A known client at a clean account takes one
    /// slot on the counter that bounds guesses (the account's, else the
    /// per-IP or global one), shared by every replica on the cache and
    /// given back by a right credential; else it is reserved like a login.
    ///
    /// # Errors
    /// As [`Self::begin`].
    pub(crate) async fn begin_per_request(
        &self,
        scope: &LoginScope,
        ip: &ClientIp,
        username: &str,
    ) -> Result<PerRequestAttempt, LoginRefused> {
        let deadline = tokio::time::Instant::now() + crate::passwords::hash_wait();
        let held = match self.take_quiet(scope, ip, username).await {
            Some(slot) => PerRequestHeld::Quiet(slot),
            // Not quiet, or not counted: the login path counts, waits or refuses.
            None => {
                PerRequestHeld::Reserved(self.begin_until(scope, ip, username, deadline).await?)
            }
        };
        Ok(PerRequestAttempt {
            throttle: Self(Arc::clone(&self.0)),
            scope: scope.clone(),
            ip: ip.clone(),
            username: username.to_owned(),
            deadline,
            held,
        })
    }

    /// One atomic slot on the counter that bounds guesses here, when
    /// admitting a right credential would change nothing: a known client,
    /// room in the limits, and no lock or other attempt on the account.
    /// `None` otherwise, or when the slot could not be counted.
    async fn take_quiet(
        &self,
        scope: &LoginScope,
        ip: &ClientIp,
        username: &str,
    ) -> Option<QuietSlot> {
        let b = &self.0;
        let scope_key = scope.key();
        let ip_bucket = ip.0.as_deref().map(|ip| scope.ip_bucket(ip));
        if scope.locks_accounts() {
            if !b.is_known(scope, ip, username).await {
                return None;
            }
        } else if b.global.peek(&scope_key).await.is_err() {
            return None;
        }
        if let Some(bucket) = &ip_bucket {
            b.ip.peek(bucket).await.ok()?;
        }
        let mut gates = Gates {
            buckets: Arc::clone(b),
            held: Vec::with_capacity(1),
        };
        let counted = if scope.locks_accounts() {
            let key = account_key(&scope_key, username);
            let slot = b.lockout().reserve_sole(&key).await?;
            gates.enter(key.clone());
            Counted::Account(key, slot)
        } else if let Some(bucket) = ip_bucket {
            let spent = b.ip.take(&bucket).await.ok()?;
            gates.enter(ip_gate(&bucket));
            Counted::Ip(bucket, spent)
        } else {
            let spent = b.global.take(&scope_key).await.ok()?;
            gates.enter(global_gate(&scope_key));
            Counted::Global(scope_key, spent)
        };
        Some(QuietSlot {
            counted: Some(counted),
            gates,
        })
    }

    /// Run a signed-in user's current-password check through the gate, so a
    /// stolen session cannot guess it at hash speed (#1873). A miss counts
    /// toward the same account lock as the login form.
    ///
    /// # Errors
    /// [`LoginRefused`] when throttled, or when `verify` returns one.
    pub async fn verify_current_password(
        &self,
        scope: &LoginScope,
        ip: &ClientIp,
        username: &str,
        verify: impl std::future::Future<Output = Result<bool, LoginRefused>>,
    ) -> Result<bool, LoginRefused> {
        let attempt = self.begin(scope, ip, username).await?;
        let ok = match verify.await {
            Ok(ok) => ok,
            // A full hash queue says nothing about the password: give the tokens back.
            Err(refused) => {
                attempt.prompted().await;
                return Err(refused);
            }
        };
        if ok {
            attempt.succeeded().await;
        } else {
            attempt.failed().await;
        }
        Ok(ok)
    }
}

fn memory_ip(limits: LoginLimits) -> RateLimitLayer {
    RateLimitLayer::per_ip(limits.ip_limit.max(1), limits.ip_window)
}

fn memory_global(limits: LoginLimits) -> RateLimitLayer {
    RateLimitLayer::global(limits.global_limit.max(1), limits.global_window)
}

/// One admitted attempt. Report [`Self::failed`] or [`Self::succeeded`].
/// `None` once reported; an unreported attempt (busy, DB error) is given
/// back on drop, as it checked nothing.
#[must_use = "report the outcome, or failures never lock the account"]
pub struct LoginAttempt(Option<Held>);

/// What an admitted attempt holds. Dropping it only leaves the in-process
/// gates and never spawns, so a refund that never runs (runtime shutting
/// down) cannot re-enter [`LoginAttempt`]'s `Drop`.
struct Held {
    buckets: Arc<Buckets>,
    scope_key: String,
    ip: Option<String>,
    /// The per-IP bucket key, from [`LoginScope::ip_bucket`].
    ip_bucket: Option<String>,
    /// What `begin` counted per IP and globally, refunded on success.
    spent: (Spent, Spent),
    /// Account-lock slots: the submitted name, then the stored one.
    locks: Vec<(String, ReservedSlot)>,
    /// Dropped last, after the refunds, so waiters see them.
    gates: Gates,
}

impl Drop for LoginAttempt {
    fn drop(&mut self) {
        let Some(held) = self.0.take() else {
            return;
        };
        if let Ok(rt) = tokio::runtime::Handle::try_current() {
            rt.spawn(held.release());
        }
    }
}

impl LoginAttempt {
    fn held(&mut self) -> Held {
        self.0.take().expect("a LoginAttempt is reported once")
    }

    /// Call once the row is found, before the password check, with the
    /// username as stored. Every spelling that finds this row then shares
    /// its lock.
    ///
    /// # Errors
    /// [`LoginRefused::Throttled`] while the stored username is locked.
    pub async fn resolve(&mut self, stored_username: &str) -> Result<(), LoginRefused> {
        let Some(held) = self.0.as_mut() else {
            return Ok(());
        };
        if held.locks.is_empty() {
            return Ok(());
        }
        let key = account_key(&held.scope_key, stored_username);
        if held.locks.iter().any(|(k, _)| *k == key) {
            return Ok(());
        }
        // Waits only on attempts past `resolve`, which never wait again.
        let buckets = Arc::clone(&held.buckets);
        let deadline = tokio::time::Instant::now() + crate::passwords::hash_wait();
        loop {
            match held.reserve_lock(key.clone()).await {
                Ok(()) => return Ok(()),
                Err(blocked) => buckets.wait(blocked, deadline).await?,
            }
        }
    }

    /// Count a failure against the username, known or not. `begin`
    /// already counted it; this keeps the tokens and may lock the account.
    pub async fn failed(mut self) {
        detached(self.held().failed()).await;
    }

    /// Clear the username's failures, remember it as known from this IP,
    /// and give back the limit tokens.
    pub async fn succeeded(mut self) {
        detached(self.held().right(true)).await;
    }

    /// The password was right and the form now asks for a second
    /// factor: give back the limit tokens, keep the username's failures.
    pub async fn prompted(mut self) {
        detached(self.held().release()).await;
    }
}

/// Run a report on its own task and wait for it, so a request cancelled
/// half-way still finishes it (as [`QuietSlot::give_back`]).
async fn detached(report: impl std::future::Future<Output = ()> + Send + 'static) {
    match tokio::runtime::Handle::try_current() {
        Ok(rt) => {
            let _ = rt.spawn(report).await;
        }
        Err(_) => report.await,
    }
}

/// A per-request credential admitted by [`LoginThrottle::begin_per_request`].
/// Dropped unsettled, it gives back what it holds.
#[must_use = "settle the compare, or a miss is never counted"]
pub(crate) struct PerRequestAttempt {
    throttle: LoginThrottle,
    scope: LoginScope,
    ip: ClientIp,
    username: String,
    /// The one `hash_wait` this request gets, however often it waits.
    deadline: tokio::time::Instant,
    held: PerRequestHeld,
}

enum PerRequestHeld {
    Quiet(QuietSlot),
    Reserved(LoginAttempt),
}

/// The one counter a quiet credential took a slot on.
enum Counted {
    Account(String, ReservedSlot),
    Ip(String, Spent),
    Global(String, Spent),
}

/// A quiet credential's slot. Dropped unreported, it is given back.
struct QuietSlot {
    counted: Option<Counted>,
    /// Left after the counter, so waiters see the refund.
    gates: Gates,
}

impl Counted {
    /// The compare proved nothing, or was right: give the slot back. Failures
    /// counted meanwhile stay.
    async fn refund(self, buckets: &Buckets) {
        match self {
            Self::Account(key, slot) => buckets.lockout().release(&key, slot).await,
            Self::Ip(key, spent) => buckets.ip.give_back(&key, spent).await,
            Self::Global(key, spent) => buckets.global.give_back(&key, spent).await,
        }
    }
}

impl QuietSlot {
    /// Give the slot back and wait for it. The refund runs on its own task,
    /// so a request cancelled half-way still finishes it.
    async fn give_back(&mut self) {
        if let Some(refund) = self.spawn_give_back() {
            let _ = refund.await;
        }
    }

    /// `None` when already given back or with no runtime to run on.
    fn spawn_give_back(&mut self) -> Option<tokio::task::JoinHandle<()>> {
        let counted = self.counted.take()?;
        let buckets = Arc::clone(&self.gates.buckets);
        let gates = std::mem::replace(
            &mut self.gates,
            Gates {
                buckets: Arc::clone(&buckets),
                held: Vec::new(),
            },
        );
        let rt = tokio::runtime::Handle::try_current().ok()?;
        Some(rt.spawn(async move {
            counted.refund(&buckets).await;
            drop(gates);
        }))
    }
}

impl Drop for QuietSlot {
    fn drop(&mut self) {
        let _ = self.spawn_give_back();
    }
}

impl PerRequestAttempt {
    /// Reserve like a login now, before a check at hash cost.
    ///
    /// # Errors
    /// As [`LoginThrottle::begin`].
    #[cfg_attr(not(feature = "tenancy"), allow(dead_code))]
    pub(crate) async fn reserve(&mut self) -> Result<(), LoginRefused> {
        if let PerRequestHeld::Quiet(slot) = &mut self.held {
            // Given back first, so the reservation is not refused by itself.
            slot.give_back().await;
            let attempt = self
                .throttle
                .begin_until(&self.scope, &self.ip, &self.username, self.deadline)
                .await?;
            self.held = PerRequestHeld::Reserved(attempt);
        }
        Ok(())
    }

    /// [`Self::reserve`], then [`LoginAttempt::resolve`] with the stored username.
    ///
    /// # Errors
    /// As [`Self::reserve`] and [`LoginAttempt::resolve`].
    #[cfg_attr(not(feature = "tenancy"), allow(dead_code))]
    pub(crate) async fn resolve(&mut self, stored_username: &str) -> Result<(), LoginRefused> {
        self.reserve().await?;
        match &mut self.held {
            PerRequestHeld::Reserved(attempt) => attempt.resolve(stored_username).await,
            PerRequestHeld::Quiet(_) => Ok(()),
        }
    }

    /// Report the compare. A right one gives its slots back and leaves the
    /// account's failures; a quiet miss keeps its slot, counted, and counts
    /// against the other limits too.
    pub(crate) async fn settle(self, right: bool) {
        match self.held {
            PerRequestHeld::Quiet(mut slot) if right => slot.give_back().await,
            PerRequestHeld::Quiet(mut slot) => {
                let Some(counted) = slot.counted.take() else {
                    return;
                };
                let b = &self.throttle.0;
                let scope_key = self.scope.key();
                match counted {
                    Counted::Account(key, lock) => {
                        b.lockout().reserved_failed(&key, lock).await;
                        if let Some(ip) = self.ip.0.as_deref() {
                            let _ = b.ip.take(&self.scope.ip_bucket(ip)).await;
                        }
                        let _ = b.global.take(&scope_key).await;
                    }
                    Counted::Ip(..) => {
                        let _ = b.global.take(&scope_key).await;
                    }
                    Counted::Global(..) => {}
                }
            }
            PerRequestHeld::Reserved(mut attempt) if right => {
                detached(attempt.held().right(false)).await;
            }
            PerRequestHeld::Reserved(attempt) => attempt.failed().await,
        }
    }
}

impl Held {
    /// A wrong password: count it on every account lock held.
    async fn failed(mut self) {
        let lockout = self.buckets.lockout();
        for (key, slot) in self.locks.drain(..) {
            lockout.reserved_failed(&key, slot).await;
        }
    }

    /// Count the attempt against the account before the password check.
    async fn reserve_lock(&mut self, key: String) -> Result<(), Blocked> {
        let lockout = self.buckets.lockout();
        crate::account_lockout::warn_once_on_weak_store(lockout);
        match lockout.reserve(&key).await {
            Reservation::Counted(slot) => {
                self.gates.enter(key.clone());
                self.locks.push((key, slot));
                Ok(())
            }
            Reservation::Locked => Err(Blocked::Refused(LoginRefused::Throttled {
                retry_after_secs: lockout.lock_duration().as_secs().max(1),
            })),
            _ => Err(Blocked::Full {
                gate: key,
                retry_after_secs: 1,
            }),
        }
    }

    /// A right credential: remember the client, give back the tokens. Only
    /// a sign-in clears failures; a per-request credential is sent so often
    /// it would wipe a guesser's misses on the same name.
    async fn right(mut self, clears_failures: bool) {
        let lockout = self.buckets.lockout();
        for (key, slot) in std::mem::take(&mut self.locks) {
            if let Some(ip) = &self.ip {
                // Written once per pair: an existing marker is left alone.
                let (cache, known) = (&self.buckets.known, known_key(&key, ip));
                if !matches!(cache.get(&known).await, Ok(Some(_))) {
                    let _ = cache.set(&known, "1", Some(KNOWN_CLIENT_TTL)).await;
                }
            }
            if clears_failures {
                lockout.reserved_succeeded(&key, slot).await;
            } else {
                lockout.release(&key, slot).await;
            }
        }
        self.give_back().await;
    }

    /// Give back everything `begin` reserved.
    async fn release(mut self) {
        let lockout = self.buckets.lockout();
        for (key, slot) in self.locks.drain(..) {
            lockout.release(&key, slot).await;
        }
        self.give_back().await;
    }

    async fn give_back(&self) {
        let (ip_spent, global_spent) = self.spent;
        if let Some(ip) = &self.ip_bucket {
            self.buckets.ip.give_back(ip, ip_spent).await;
        }
        self.buckets
            .global
            .give_back(&self.scope_key, global_spent)
            .await;
    }
}

/// Forget the account locks of tenant `slug`'s logins, so a new tenant
/// reusing the slug does not inherit them.
///
/// # Errors
/// The lockout cache's `delete_prefix` error.
#[cfg(feature = "tenancy")]
pub(crate) async fn forget_tenant(slug: &str) -> Result<(), crate::cache::CacheError> {
    let lockout = crate::account_lockout::shared();
    for scope in [
        LoginScope::Tenant(slug.to_owned()),
        LoginScope::TenantBasic(slug.to_owned()),
    ] {
        lockout
            .clear_accounts_with_prefix(&account_key_prefix(&scope.key()))
            .await?;
    }
    Ok(())
}

/// Every [`account_key`] of `scope_key` starts with this.
fn account_key_prefix(scope_key: &str) -> String {
    format!("login:{scope_key}:")
}

/// `login:<scope>:<sha256 of the trimmed, lowercased username>`. Hashed
/// so the key has a fixed size and a shared cache never holds the name.
fn account_key(scope_key: &str, username: &str) -> String {
    use sha2::{Digest as _, Sha256};
    use std::fmt::Write as _;
    let digest = Sha256::digest(username.trim().to_lowercase().as_bytes());
    let mut key = account_key_prefix(scope_key);
    for b in digest {
        let _ = write!(key, "{b:02x}");
    }
    key
}

/// `check --deploy` can't run the app's startup code, so it only advises.
pub(crate) const PROCESS_LOCAL_NOTE: &str =
    "login per-IP and global limits: unless your app installs \
     `login_throttle::configure_shared(LoginThrottle::with_cache(limits, cache))` with a Redis or \
     database cache, each replica counts its own, so N replicas allow N times each limit";

/// Logged once, at the first login, when the limits count per process.
const PROCESS_LOCAL_WARNING: &str =
    "login per-IP and global limits are counted in process memory, so each replica allows its own \
     attempts; install `login_throttle::configure_shared(LoginThrottle::with_cache(limits, cache))` \
     with a Redis or database cache";

/// Logged once when the limits sit on a cache that drops writes.
const NULL_STORE_WARNING: &str =
    "login per-IP and global limits use a cache that stores nothing (`NullCache`), so they never \
     count; use a Redis, database or in-memory cache";

static SHARED: crate::boot_slot::BootSlot<LoginThrottle> = crate::boot_slot::BootSlot::new();

/// The gate every built-in login uses, with [`LoginLimits::default`]
/// unless [`configure_shared`] or `[auth] login_*` settings set it.
#[must_use]
pub fn shared() -> &'static LoginThrottle {
    SHARED.get(|| LoginThrottle::new(LoginLimits::default()))
}

/// Install the [`shared`] gate at boot. It replaces the default and the
/// one built from `[auth]` settings; `false` if an earlier call won.
pub fn configure_shared(throttle: LoginThrottle) -> bool {
    SHARED.set_explicit(throttle)
}

/// The `[auth] login_*` gate; `false` if app code already set one.
// Only `manage` applies settings (#1948).
#[cfg(all(feature = "config", feature = "manage"))]
pub(crate) fn configure_from_settings(throttle: LoginThrottle) -> bool {
    SHARED.set_from_settings(throttle)
}

/// A test cache that counts the gate's writes.
#[cfg(test)]
pub(crate) mod counting {
    /// Counts the login gate's cache writes.
    #[derive(Default)]
    pub(crate) struct CountingWrites {
        inner: crate::cache::InMemoryCache,
        writes: std::sync::atomic::AtomicUsize,
        read_delay: std::time::Duration,
        write_delay: std::time::Duration,
    }

    impl CountingWrites {
        /// Every read waits `delay`, like a remote cache.
        pub(crate) fn with_read_delay(delay: std::time::Duration) -> Self {
            Self {
                read_delay: delay,
                ..Self::default()
            }
        }

        /// Every write waits `delay` first.
        pub(crate) fn with_write_delay(delay: std::time::Duration) -> Self {
            Self {
                write_delay: delay,
                ..Self::default()
            }
        }

        async fn read(&self) {
            if !self.read_delay.is_zero() {
                tokio::time::sleep(self.read_delay).await;
            }
        }

        async fn wrote(&self) {
            if !self.write_delay.is_zero() {
                tokio::time::sleep(self.write_delay).await;
            }
            self.writes
                .fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        }

        /// Writes so far.
        pub(crate) fn writes(&self) -> usize {
            self.writes.load(std::sync::atomic::Ordering::SeqCst)
        }
    }

    #[async_trait::async_trait]
    impl crate::cache::Cache for CountingWrites {
        async fn get(&self, k: &str) -> Result<Option<String>, crate::cache::CacheError> {
            self.read().await;
            self.inner.get(k).await
        }
        async fn set(
            &self,
            k: &str,
            v: &str,
            ttl: Option<std::time::Duration>,
        ) -> Result<(), crate::cache::CacheError> {
            self.wrote().await;
            self.inner.set(k, v, ttl).await
        }
        async fn delete(&self, k: &str) -> Result<(), crate::cache::CacheError> {
            self.wrote().await;
            self.inner.delete(k).await
        }
        async fn exists(&self, k: &str) -> Result<bool, crate::cache::CacheError> {
            self.read().await;
            self.inner.exists(k).await
        }
        async fn clear(&self) -> Result<(), crate::cache::CacheError> {
            self.inner.clear().await
        }
        async fn incr(
            &self,
            k: &str,
            by: i64,
            ttl: Option<std::time::Duration>,
        ) -> Result<i64, crate::cache::CacheError> {
            self.wrote().await;
            self.inner.incr(k, by, ttl).await
        }
        async fn decr(
            &self,
            k: &str,
            by: i64,
            ttl: Option<std::time::Duration>,
        ) -> Result<i64, crate::cache::CacheError> {
            self.wrote().await;
            self.inner.decr(k, by, ttl).await
        }
    }
}

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

    fn ip(s: &str) -> ClientIp {
        ClientIp(Some(s.to_owned()))
    }

    fn tenant(s: &str) -> LoginScope {
        LoginScope::Tenant(s.to_owned())
    }

    #[test]
    fn account_key_normalizes_and_scopes() {
        assert_eq!(
            account_key("admin", " Alice "),
            account_key("admin", "alice")
        );
        assert_ne!(account_key("admin", "alice"), account_key("op", "alice"));
        assert!(!account_key("admin", "alice").contains("alice"));
    }

    #[test]
    fn each_scope_has_its_own_key() {
        let keys = [
            LoginScope::Admin.key(),
            LoginScope::Operator.key(),
            tenant("a").key(),
            LoginScope::TenantBasic("a".into()).key(),
            LoginScope::TenantApiKey("a".into()).key(),
            LoginScope::TenantAgent("a".into()).key(),
        ];
        for (i, a) in keys.iter().enumerate() {
            for b in &keys[i + 1..] {
                assert_ne!(a, b);
            }
        }
    }

    #[tokio::test]
    async fn per_ip_limit_spares_other_ips() {
        let t = LoginThrottle::new(LoginLimits {
            ip_limit: 2,
            ..LoginLimits::default()
        });
        let s = tenant("t-ip");
        for n in 0..2 {
            let a = t.begin(&s, &ip("10.9.0.1"), &format!("u{n}")).await;
            a.unwrap().failed().await;
        }
        let r = t.begin(&s, &ip("10.9.0.1"), "u9").await;
        assert!(matches!(r, Err(LoginRefused::Throttled { .. })));
        assert!(t.begin(&s, &ip("10.9.0.2"), "u9").await.is_ok());
    }

    /// A busy verify spends no limit token.
    #[tokio::test]
    async fn busy_current_password_check_gives_tokens_back() {
        let t = LoginThrottle::new(LoginLimits {
            global_limit: 1,
            ..LoginLimits::default()
        });
        let s = tenant("t-busy");
        for _ in 0..2 {
            let r = t
                .verify_current_password(&s, &ClientIp::default(), "u", async {
                    Err(LoginRefused::Busy)
                })
                .await;
            assert_eq!(r, Err(LoginRefused::Busy));
        }
    }

    #[tokio::test]
    async fn global_ceiling_applies_without_an_ip() {
        let t = LoginThrottle::new(LoginLimits {
            global_limit: 1,
            ..LoginLimits::default()
        });
        let s = tenant("t-g");
        let a = t.begin(&s, &ClientIp::default(), "a").await.unwrap();
        a.failed().await;
        let r = t.begin(&s, &ClientIp::default(), "b").await;
        assert!(matches!(r, Err(LoginRefused::Throttled { .. })));
    }

    /// A request the per-IP limit refuses takes nothing from the global
    /// budget, so one IP cannot drain it for everyone.
    #[tokio::test]
    async fn an_ip_refusal_leaves_the_global_budget() {
        let t = LoginThrottle::new(LoginLimits {
            ip_limit: 1,
            global_limit: 3,
            ..LoginLimits::default()
        });
        let s = tenant("t-order");
        t.begin(&s, &ip("10.9.1.1"), "a")
            .await
            .unwrap()
            .failed()
            .await;
        for _ in 0..10 {
            assert!(t.begin(&s, &ip("10.9.1.1"), "a").await.is_err());
        }
        for n in 2..4 {
            let a = t.begin(&s, &ip(&format!("10.9.1.{n}")), "b").await;
            assert!(a.is_ok(), "global budget was drained by refused requests");
        }
    }

    /// One scope using up its global limit leaves the others alone.
    #[tokio::test]
    async fn global_limits_are_per_scope() {
        let t = LoginThrottle::new(LoginLimits {
            global_limit: 1,
            ..LoginLimits::default()
        });
        let none = ClientIp::default();
        t.begin(&tenant("t-a"), &none, "x")
            .await
            .unwrap()
            .failed()
            .await;
        assert!(t.begin(&tenant("t-a"), &none, "y").await.is_err());
        assert!(t.begin(&tenant("t-b"), &none, "y").await.is_ok());
        assert!(t.begin(&LoginScope::Operator, &none, "y").await.is_ok());
    }

    /// Successful logins give their tokens back, so users behind one
    /// address are not limited by their own logins.
    #[tokio::test]
    async fn successes_do_not_use_up_the_limits() {
        let t = LoginThrottle::new(LoginLimits {
            ip_limit: 2,
            global_limit: 2,
            ..LoginLimits::default()
        });
        let s = tenant("t-ok");
        for n in 0..10 {
            let a = t.begin(&s, &ip("10.9.2.1"), &format!("ok{n}")).await;
            a.unwrap().succeeded().await;
        }
    }

    /// Many IPs spending a scope's global limit do not lock out a user who
    /// signed in from this IP before; a new IP is still limited.
    async fn known_client_passes_a_spent_global_limit(t: LoginThrottle, slug: &str, net: u8) {
        let s = tenant(slug);
        let home = ip(&format!("10.{net}.1.1"));
        t.begin(&s, &home, "alice").await.unwrap().succeeded().await;
        for n in 0..2 {
            let (from, user) = (ip(&format!("10.{net}.2.{n}")), format!("x{n}"));
            t.begin(&s, &from, &user).await.unwrap().failed().await;
        }
        let other = ip(&format!("10.{net}.3.1"));
        assert!(
            t.begin(&s, &other, "bob").await.is_err(),
            "global limit open"
        );
        assert!(t.check(&s, &home, " Alice").await.is_ok(), "check refused");
        let a = t.begin(&s, &home, "alice").await;
        a.expect("a known client was locked out").succeeded().await;
        let away = ip(&format!("10.{net}.4.1"));
        assert!(
            t.begin(&s, &away, "alice").await.is_err(),
            "new IP bypassed"
        );
    }

    #[tokio::test]
    async fn a_known_client_passes_a_spent_global_limit() {
        let limits = LoginLimits {
            global_limit: 2,
            ..LoginLimits::default()
        };
        known_client_passes_a_spent_global_limit(LoginThrottle::new(limits), "t-known", 30).await;
        let cache: BoxedCache = Arc::new(crate::cache::InMemoryCache::new());
        let t = on_cache(&cache, limits);
        known_client_passes_a_spent_global_limit(t, "t-known-c", 31).await;
    }

    /// Agent-secret failures and human logins from one IP spend separate
    /// per-IP buckets, so neither can lock the other out.
    #[tokio::test]
    async fn agent_failures_spare_human_logins_on_the_same_ip() {
        let t = LoginThrottle::new(LoginLimits {
            ip_limit: 2,
            ..LoginLimits::default()
        });
        let (agent, human) = (LoginScope::TenantAgent("t-split".into()), tenant("t-split"));
        let shared_ip = ip("10.9.40.1");
        for _ in 0..2 {
            t.begin(&agent, &shared_ip, "")
                .await
                .unwrap()
                .failed()
                .await;
        }
        assert!(t.begin(&agent, &shared_ip, "").await.is_err());
        assert!(t.check(&human, &shared_ip, "bob").await.is_ok());
        for n in 0..2 {
            let a = t.begin(&human, &shared_ip, &format!("h{n}")).await;
            a.expect("agent failures locked a human out").failed().await;
        }
        let other = ip("10.9.40.2");
        for n in 0..2 {
            let a = t.begin(&human, &other, &format!("g{n}")).await;
            a.unwrap().failed().await;
        }
        let a = t.begin(&agent, &other, "").await;
        a.expect("human failures locked an agent out")
            .succeeded()
            .await;
    }

    /// A known-client marker is written once, not on every sign-in.
    #[tokio::test]
    async fn a_known_client_marker_is_written_once() {
        let counting = Arc::new(CountingSets::default());
        let cache: BoxedCache = counting.clone();
        let t = on_cache(&cache, LoginLimits::default());
        let (s, home) = (tenant("t-mark-once"), ip("10.9.41.1"));
        for _ in 0..3 {
            t.begin(&s, &home, "carol").await.unwrap().succeeded().await;
        }
        let marks = counting
            .known_sets
            .load(std::sync::atomic::Ordering::SeqCst);
        assert_eq!(marks, 1, "the marker was rewritten");
    }

    /// An in-memory cache that counts `login-known:` writes.
    #[derive(Default)]
    struct CountingSets {
        inner: crate::cache::InMemoryCache,
        known_sets: std::sync::atomic::AtomicUsize,
    }

    #[async_trait::async_trait]
    impl crate::cache::Cache for CountingSets {
        async fn get(&self, k: &str) -> Result<Option<String>, crate::cache::CacheError> {
            self.inner.get(k).await
        }
        async fn set(
            &self,
            k: &str,
            v: &str,
            ttl: Option<Duration>,
        ) -> Result<(), crate::cache::CacheError> {
            if k.contains("login-known:") {
                self.known_sets
                    .fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            }
            self.inner.set(k, v, ttl).await
        }
        async fn delete(&self, k: &str) -> Result<(), crate::cache::CacheError> {
            self.inner.delete(k).await
        }
        async fn exists(&self, k: &str) -> Result<bool, crate::cache::CacheError> {
            self.inner.exists(k).await
        }
        async fn clear(&self) -> Result<(), crate::cache::CacheError> {
            self.inner.clear().await
        }
        async fn incr(
            &self,
            k: &str,
            by: i64,
            ttl: Option<Duration>,
        ) -> Result<i64, crate::cache::CacheError> {
            self.inner.incr(k, by, ttl).await
        }
        async fn decr(
            &self,
            k: &str,
            by: i64,
            ttl: Option<Duration>,
        ) -> Result<i64, crate::cache::CacheError> {
            self.inner.decr(k, by, ttl).await
        }
    }

    /// Per-request credentials keep a token only when they fail.
    #[tokio::test]
    async fn per_request_scopes_count_only_failures() {
        let t = LoginThrottle::new(LoginLimits {
            ip_limit: 2,
            ..LoginLimits::default()
        });
        let s = LoginScope::TenantApiKey("t-key".into());
        for _ in 0..5 {
            t.begin(&s, &ip("10.9.3.1"), "")
                .await
                .unwrap()
                .succeeded()
                .await;
        }
        for _ in 0..2 {
            t.begin(&s, &ip("10.9.3.1"), "")
                .await
                .unwrap()
                .failed()
                .await;
        }
        assert!(t.begin(&s, &ip("10.9.3.1"), "").await.is_err());
    }

    /// Start `n` attempts at once; each holds its reservation for a moment,
    /// then reports `fail` or success. Returns how many were admitted.
    async fn burst(
        t: &LoginThrottle,
        s: &LoginScope,
        ip_of: impl Fn(usize) -> ClientIp,
        user: &str,
        n: usize,
        fail: impl Fn(usize) -> bool,
    ) -> usize {
        let runs = (0..n).map(|i| {
            let (ip, fail) = (ip_of(i), fail(i));
            async move {
                let Ok(a) = t.begin(s, &ip, user).await else {
                    return 0;
                };
                tokio::time::sleep(Duration::from_millis(if fail { 1 } else { 20 })).await;
                if fail {
                    a.failed().await;
                } else {
                    a.succeeded().await;
                }
                1
            }
        });
        futures_util::future::join_all(runs).await.iter().sum()
    }

    /// #2378 — a parallel burst of wrong keys from one IP gets no more
    /// than the per-IP limit checked.
    #[tokio::test]
    async fn per_request_burst_is_capped_by_the_ip_limit() {
        let t = LoginThrottle::new(LoginLimits {
            ip_limit: 3,
            ..LoginLimits::default()
        });
        let s = LoginScope::TenantApiKey("t-burst".into());
        let n = burst(&t, &s, |_| ip("10.9.3.9"), "", 20, |_| true).await;
        assert_eq!(n, 3, "in-flight attempts must hold their token");
    }

    /// #2378 — a burst of wrong passwords at one Basic account gets
    /// `max_attempts` guesses, then the account is locked.
    #[tokio::test]
    async fn basic_burst_is_capped_by_the_account_lock() {
        let t = LoginThrottle::new(LoginLimits {
            ip_limit: 1000,
            ..LoginLimits::default()
        });
        let s = LoginScope::TenantBasic("t-burst-lock".into());
        let at = |n: usize| ip(&format!("10.9.10.{n}"));
        let n = burst(&t, &s, at, "victim", 50, |_| true).await;
        assert_eq!(
            n,
            crate::account_lockout::DEFAULT_MAX_ATTEMPTS as usize,
            "the lock check must reserve, not peek"
        );
        let r = t.begin(&s, &ip("10.9.11.1"), "victim").await;
        assert!(matches!(r, Err(LoginRefused::Throttled { .. })));
    }

    /// security-002 — parallel right credentials wait for each other;
    /// in-flight successes never refuse one.
    #[tokio::test]
    async fn parallel_successes_are_never_refused() {
        let t = LoginThrottle::new(LoginLimits {
            ip_limit: 3,
            ..LoginLimits::default()
        });
        let s = LoginScope::TenantBasic("t-par-ok".into());
        let n = burst(&t, &s, |_| ip("10.9.15.1"), "svc", 20, |_| false).await;
        assert_eq!(n, 20);
        let s = LoginScope::TenantApiKey("t-par-key".into());
        let none = |_| ClientIp::default();
        assert_eq!(
            burst(&t, &s, |_| ip("10.9.15.2"), "", 25, |_| false).await,
            25
        );
        assert_eq!(burst(&t, &s, none, "", 5, |_| false).await, 5);
    }

    /// security-002 — one wrong password among in-flight successes
    /// counts once; it does not lock the account.
    #[tokio::test]
    async fn one_failure_in_a_good_burst_does_not_lock() {
        let t = LoginThrottle::new(LoginLimits::default());
        let s = LoginScope::TenantBasic("t-one-bad".into());
        let n = burst(&t, &s, |_| ip("10.9.16.1"), "svc2", 5, |i| i == 0).await;
        assert_eq!(n, 5);
        let a = t.begin(&s, &ip("10.9.16.1"), "svc2").await;
        assert!(a.is_ok(), "one failure locked the account");
    }

    /// A wrong burst on the cache-backed limits is capped too, and the
    /// refused retries do not fill the window themselves.
    #[tokio::test]
    async fn cache_backed_burst_is_capped_and_successes_wait() {
        let cache: BoxedCache = Arc::new(crate::cache::InMemoryCache::new());
        let t = on_cache(
            &cache,
            LoginLimits {
                ip_limit: 3,
                ..LoginLimits::default()
            },
        );
        let s = LoginScope::TenantApiKey("t-cache-burst".into());
        assert_eq!(
            burst(&t, &s, |_| ip("10.9.17.1"), "", 20, |_| false).await,
            20
        );
        assert_eq!(
            burst(&t, &s, |_| ip("10.9.17.1"), "", 20, |_| true).await,
            3
        );
    }

    /// An attempt dropped unreported (busy, DB error) gives its
    /// reservations back instead of counting toward the lock.
    #[tokio::test]
    async fn an_unreported_attempt_is_given_back() {
        let t = LoginThrottle::new(LoginLimits {
            ip_limit: 1,
            ..LoginLimits::default()
        });
        let s = tenant("t-drop");
        for _ in 0..10 {
            drop(t.begin(&s, &ip("10.9.13.1"), "dropper").await.unwrap());
            // The refund runs on a spawned task.
            for _ in 0..20 {
                tokio::task::yield_now().await;
            }
        }
    }

    /// tests-001 — an attempt dropped while its runtime shuts down must
    /// not recurse through `Drop` (stack overflow).
    #[test]
    fn an_attempt_dropped_at_runtime_shutdown_does_not_recurse() {
        let rt = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .unwrap();
        rt.block_on(async {
            let t = LoginThrottle::new(LoginLimits::default());
            let a = t.begin(&tenant("t-shutdown"), &ip("10.9.14.1"), "u").await;
            let a = a.unwrap();
            tokio::spawn(async move {
                let _held = a;
                std::future::pending::<()>().await;
            });
            tokio::task::yield_now().await;
        });
        drop(rt);
    }

    /// Succeeding attempts give the account reservation back.
    #[tokio::test]
    async fn successes_free_the_account_reservation() {
        let t = LoginThrottle::new(LoginLimits::default());
        let s = tenant("t-lock-ok");
        for _ in 0..20 {
            t.begin(&s, &ip("10.9.12.1"), "regular")
                .await
                .unwrap()
                .succeeded()
                .await;
        }
    }

    fn on_cache(cache: &BoxedCache, limits: LoginLimits) -> LoginThrottle {
        LoginThrottle::with_cache(limits, cache.clone())
    }

    /// Two replicas on one cache share the per-IP and global limits (#1809).
    #[tokio::test]
    async fn replicas_on_one_cache_share_the_limits() {
        let cache: BoxedCache = Arc::new(crate::cache::InMemoryCache::new());
        let limits = LoginLimits {
            ip_limit: 2,
            global_limit: 3,
            ..LoginLimits::default()
        };
        let (a, b) = (on_cache(&cache, limits), on_cache(&cache, limits));
        let s = tenant("t-shared");
        let one = ip("10.9.4.1");
        a.begin(&s, &one, "u").await.unwrap().failed().await;
        b.begin(&s, &one, "v").await.unwrap().failed().await;
        assert!(a.begin(&s, &one, "w").await.is_err(), "per-IP not shared");
        b.begin(&s, &ip("10.9.4.2"), "x")
            .await
            .unwrap()
            .failed()
            .await;
        let r = a.begin(&s, &ip("10.9.4.3"), "y").await;
        assert!(r.is_err(), "global not shared");
    }

    /// On the cache, successes still give their tokens back.
    #[tokio::test]
    async fn cache_backed_successes_do_not_use_up_the_limits() {
        let cache: BoxedCache = Arc::new(crate::cache::InMemoryCache::new());
        let t = on_cache(
            &cache,
            LoginLimits {
                ip_limit: 2,
                global_limit: 2,
                ..LoginLimits::default()
            },
        );
        for n in 0..10 {
            let a = t
                .begin(&tenant("t-ok2"), &ip("10.9.5.1"), &format!("ok{n}"))
                .await;
            a.unwrap().succeeded().await;
        }
    }

    struct Down;

    #[async_trait::async_trait]
    impl crate::cache::Cache for Down {
        async fn get(&self, _: &str) -> Result<Option<String>, crate::cache::CacheError> {
            Err(crate::cache::CacheError::Connection("down".into()))
        }
        async fn set(
            &self,
            _: &str,
            _: &str,
            _: Option<Duration>,
        ) -> Result<(), crate::cache::CacheError> {
            Err(crate::cache::CacheError::Connection("down".into()))
        }
        async fn delete(&self, _: &str) -> Result<(), crate::cache::CacheError> {
            Err(crate::cache::CacheError::Connection("down".into()))
        }
        async fn exists(&self, _: &str) -> Result<bool, crate::cache::CacheError> {
            Err(crate::cache::CacheError::Connection("down".into()))
        }
        async fn clear(&self) -> Result<(), crate::cache::CacheError> {
            Err(crate::cache::CacheError::Connection("down".into()))
        }
    }

    fn down(ip_limit: u32, global_limit: u32) -> LoginThrottle {
        let cache: BoxedCache = Arc::new(Down);
        on_cache(
            &cache,
            LoginLimits {
                ip_limit,
                global_limit,
                ..LoginLimits::default()
            },
        )
    }

    /// A cache outage keeps the per-IP limit, counted in process.
    #[tokio::test]
    async fn a_cache_outage_keeps_the_per_ip_limit() {
        let t = down(1, 10);
        let s = tenant("t-down");
        t.begin(&s, &ip("10.9.6.1"), "a")
            .await
            .unwrap()
            .failed()
            .await;
        assert!(t.begin(&s, &ip("10.9.6.1"), "a").await.is_err());
        assert!(t.begin(&s, &ip("10.9.6.2"), "b").await.is_ok());
    }

    /// A cache outage keeps the global limit too: many IPs are still capped.
    #[tokio::test]
    async fn a_cache_outage_keeps_the_global_limit() {
        let t = down(10, 1);
        let s = tenant("t-down-g");
        t.begin(&s, &ip("10.9.7.1"), "a")
            .await
            .unwrap()
            .failed()
            .await;
        assert!(t.begin(&s, &ip("10.9.7.2"), "b").await.is_err());
    }

    /// Successes during an outage refund the in-process fallback buckets.
    #[tokio::test]
    async fn a_cache_outage_refunds_the_fallback() {
        let t = down(1, 1);
        let s = tenant("t-down-ok");
        for n in 0..5 {
            let a = t.begin(&s, &ip("10.9.8.1"), &format!("ok{n}")).await;
            a.unwrap().succeeded().await;
        }
    }

    #[test]
    fn weak_stores_are_reported() {
        let limits = LoginLimits::default();
        let memory = LoginThrottle::new(limits);
        assert!(memory.is_process_local());
        assert_eq!(memory.store_warning(), Some(PROCESS_LOCAL_WARNING));
        let null: BoxedCache = Arc::new(crate::cache::NullCache);
        let null = LoginThrottle::with_cache(limits, null);
        assert_eq!(null.store_warning(), Some(NULL_STORE_WARNING));
        assert!(LoginThrottle::with_cache(limits, Arc::new(Down))
            .store_warning()
            .is_none());
    }

    #[test]
    fn refusals_carry_retry_after() {
        let r = LoginRefused::Throttled {
            retry_after_secs: 9,
        }
        .into_response();
        assert_eq!(r.status(), axum::http::StatusCode::TOO_MANY_REQUESTS);
        assert_eq!(r.headers()[axum::http::header::RETRY_AFTER], "9");
        let r = LoginRefused::Busy.into_response();
        assert_eq!(r.status(), axum::http::StatusCode::SERVICE_UNAVAILABLE);
        assert!(r.headers().contains_key(axum::http::header::RETRY_AFTER));
    }

    /// Two replicas on one shared cache, as a load balancer spreads them.
    fn replicas(limits: LoginLimits) -> [Arc<LoginThrottle>; 2] {
        let cache: BoxedCache = Arc::new(crate::cache::InMemoryCache::new());
        [
            Arc::new(LoginThrottle::with_cache(limits, cache.clone())),
            Arc::new(LoginThrottle::with_cache(limits, cache)),
        ]
    }

    /// Per-request guesses spread over `replicas`, each compared for 30 ms
    /// then reported wrong; how many were compared, refused or not after.
    async fn per_request_burst(
        replicas: &[Arc<LoginThrottle>; 2],
        s: &LoginScope,
        ip: &ClientIp,
        user: &str,
        n: usize,
    ) -> usize {
        let compared = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let runs: Vec<_> = (0..n)
            .map(|i| {
                let (t, s, ip, user) = (
                    Arc::clone(&replicas[i % 2]),
                    s.clone(),
                    ip.clone(),
                    user.to_owned(),
                );
                let compared = Arc::clone(&compared);
                tokio::spawn(async move {
                    let Ok(a) = t.begin_per_request(&s, &ip, &user).await else {
                        return;
                    };
                    compared.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                    tokio::time::sleep(Duration::from_millis(30)).await;
                    a.settle(false).await;
                })
            })
            .collect();
        for r in runs {
            r.await.unwrap();
        }
        compared.load(std::sync::atomic::Ordering::SeqCst)
    }

    /// A known client's Basic guesses over two replicas: the account lock
    /// bounds every compare, including ones answered 429 after it.
    #[tokio::test]
    async fn per_request_guesses_over_replicas_are_bounded_by_the_lock() {
        let r = replicas(LoginLimits::default());
        let (s, client) = (
            LoginScope::TenantBasic("t-repl-lock".into()),
            ip("10.9.70.1"),
        );
        let a = r[0].begin(&s, &client, "bob").await.unwrap();
        a.succeeded().await;
        let compared = per_request_burst(&r, &s, &client, "bob", 30).await;
        let max = crate::account_lockout::shared().attempt_limit() as usize;
        assert!(compared <= max, "{compared} guesses were compared");
    }

    /// API-key guesses over two replicas: the per-IP limit bounds every compare.
    #[tokio::test]
    async fn per_request_keys_over_replicas_are_bounded_by_the_ip_limit() {
        let r = replicas(LoginLimits {
            ip_limit: 3,
            ..LoginLimits::default()
        });
        let s = LoginScope::TenantApiKey("t-repl-ip".into());
        let compared = per_request_burst(&r, &s, &ip("10.9.70.2"), "", 30).await;
        assert!(compared <= 3, "{compared} keys were compared");
    }

    /// A wrong guess counted after a quiet right one was admitted stays
    /// counted: a client polling Basic must not wipe a guesser's misses.
    #[tokio::test]
    async fn a_right_per_request_credential_keeps_a_failure_counted_meanwhile() {
        let t = LoginThrottle::new(LoginLimits::default());
        let (s, client) = (
            LoginScope::TenantBasic("t-quiet-keep".into()),
            ip("10.9.70.3"),
        );
        let a = t.begin(&s, &client, "carol").await.unwrap();
        a.succeeded().await;
        let right = t.begin_per_request(&s, &client, "carol").await.unwrap();
        let wrong = t.begin(&s, &client, "carol").await.unwrap();
        wrong.failed().await;
        right.settle(true).await;
        let key = account_key(&s.key(), "carol");
        assert_eq!(
            crate::account_lockout::shared().attempt_count(&key).await,
            1
        );
    }

    /// With a failure already counted the credential is reserved like a
    /// login; a right one still leaves the failure.
    #[tokio::test]
    async fn a_right_per_request_credential_keeps_an_earlier_failure() {
        let t = LoginThrottle::new(LoginLimits::default());
        let (s, client) = (
            LoginScope::TenantBasic("t-reserved-keep".into()),
            ip("10.9.70.5"),
        );
        t.begin(&s, &client, "erin").await.unwrap().failed().await;
        for _ in 0..3 {
            let right = t.begin_per_request(&s, &client, "erin").await.unwrap();
            right.settle(true).await;
        }
        let key = account_key(&s.key(), "erin");
        assert_eq!(
            crate::account_lockout::shared().attempt_count(&key).await,
            1
        );
    }

    /// A request cancelled while its right credential's slot is refunded
    /// still gives the slot back.
    #[tokio::test]
    async fn a_cancelled_refund_still_gives_the_slot_back() {
        let cache: BoxedCache = Arc::new(counting::CountingWrites::with_read_delay(
            Duration::from_millis(100),
        ));
        let t = on_cache(
            &cache,
            LoginLimits {
                ip_limit: 1,
                ..LoginLimits::default()
            },
        );
        let (s, client) = (LoginScope::TenantApiKey("t-cancel".into()), ip("10.9.70.6"));
        let a = t.begin_per_request(&s, &client, "").await.unwrap();
        assert!(matches!(a.held, PerRequestHeld::Quiet(_)));
        let _ = tokio::time::timeout(Duration::from_millis(30), a.settle(true)).await;
        tokio::time::sleep(Duration::from_millis(500)).await;
        let bucket = s.ip_bucket("10.9.70.6");
        assert!(t.0.ip.peek(&bucket).await.is_ok(), "the slot leaked");
    }

    /// A login form request cancelled while a right password's or a second-factor
    /// prompt's tokens are refunded still gives them back.
    #[tokio::test]
    async fn a_cancelled_login_refund_still_gives_the_slot_back() {
        for (n, prompt) in [(7, false), (8, true)] {
            let cache: BoxedCache = Arc::new(counting::CountingWrites::with_read_delay(
                Duration::from_millis(100),
            ));
            let t = on_cache(
                &cache,
                LoginLimits {
                    ip_limit: 1,
                    ..LoginLimits::default()
                },
            );
            let (s, addr) = (
                tenant(&format!("t-cancel-login-{n}")),
                format!("10.9.70.{n}"),
            );
            let a = t.begin(&s, &ip(&addr), "dana").await.unwrap();
            let report = async move {
                if prompt {
                    a.prompted().await;
                } else {
                    a.succeeded().await;
                }
            };
            let _ = tokio::time::timeout(Duration::from_millis(30), report).await;
            tokio::time::sleep(Duration::from_millis(500)).await;
            let bucket = s.ip_bucket(&addr);
            assert!(
                t.0.ip.peek(&bucket).await.is_ok(),
                "the slot leaked (prompted: {prompt})"
            );
        }
    }

    /// A login form request cancelled while its wrong password is counted
    /// still counts it, so cancelling cannot dodge the account lock.
    #[tokio::test]
    async fn a_cancelled_login_failure_is_still_counted() {
        let mut t = LoginThrottle::new(LoginLimits::default());
        let slow: BoxedCache = Arc::new(counting::CountingWrites::with_write_delay(
            Duration::from_millis(100),
        ));
        let lockout: &'static _ = Box::leak(Box::new(
            crate::account_lockout::Lockout::new(slow).max_attempts(1),
        ));
        Arc::get_mut(&mut t.0).expect("unshared").lockout = Some(lockout);
        let s = tenant("t-cancel-failed");
        let a = t.begin(&s, &ip("10.9.70.9"), "gil").await.unwrap();
        let _ = tokio::time::timeout(Duration::from_millis(30), a.failed()).await;
        tokio::time::sleep(Duration::from_millis(500)).await;
        assert!(
            lockout.is_locked(&account_key(&s.key(), "gil")).await,
            "the failure was dropped"
        );
    }

    /// Counts every call; with `fail`, every call errors.
    #[derive(Default)]
    struct CountingCalls {
        inner: crate::cache::InMemoryCache,
        calls: std::sync::atomic::AtomicUsize,
        fail: bool,
    }

    impl CountingCalls {
        fn call(&self) -> Result<(), crate::cache::CacheError> {
            self.calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            if self.fail {
                return Err(crate::cache::CacheError::Connection("down".into()));
            }
            Ok(())
        }

        fn calls(&self) -> usize {
            self.calls.load(std::sync::atomic::Ordering::SeqCst)
        }
    }

    #[async_trait::async_trait]
    impl crate::cache::Cache for CountingCalls {
        async fn get(&self, k: &str) -> Result<Option<String>, crate::cache::CacheError> {
            self.call()?;
            self.inner.get(k).await
        }
        async fn set(
            &self,
            k: &str,
            v: &str,
            ttl: Option<Duration>,
        ) -> Result<(), crate::cache::CacheError> {
            self.call()?;
            self.inner.set(k, v, ttl).await
        }
        async fn add(
            &self,
            k: &str,
            v: &str,
            ttl: Option<Duration>,
        ) -> Result<bool, crate::cache::CacheError> {
            self.call()?;
            self.inner.add(k, v, ttl).await
        }
        async fn delete(&self, k: &str) -> Result<(), crate::cache::CacheError> {
            self.call()?;
            self.inner.delete(k).await
        }
        async fn exists(&self, k: &str) -> Result<bool, crate::cache::CacheError> {
            self.call()?;
            self.inner.exists(k).await
        }
        async fn clear(&self) -> Result<(), crate::cache::CacheError> {
            self.inner.clear().await
        }
        async fn incr(
            &self,
            k: &str,
            by: i64,
            ttl: Option<Duration>,
        ) -> Result<i64, crate::cache::CacheError> {
            self.call()?;
            self.inner.incr(k, by, ttl).await
        }
        async fn decr(
            &self,
            k: &str,
            by: i64,
            ttl: Option<Duration>,
        ) -> Result<i64, crate::cache::CacheError> {
            self.call()?;
            self.inner.decr(k, by, ttl).await
        }
    }

    /// `t` counting account locks in `cache` instead of the shared lockout.
    fn with_lockout(mut t: LoginThrottle, cache: Arc<CountingCalls>) -> LoginThrottle {
        let lockout = crate::account_lockout::Lockout::new(cache);
        Arc::get_mut(&mut t.0).expect("unshared").lockout = Some(Box::leak(Box::new(lockout)));
        t
    }

    /// A lockout store that errors counts nothing, so a known client's
    /// credential takes the full reservation, per-IP slot included.
    #[tokio::test]
    async fn an_uncounted_lock_slot_is_not_quiet() {
        let down = Arc::new(CountingCalls {
            fail: true,
            ..CountingCalls::default()
        });
        let t = with_lockout(
            LoginThrottle::new(LoginLimits {
                ip_limit: 1,
                ..LoginLimits::default()
            }),
            down,
        );
        let (s, client) = (
            LoginScope::TenantBasic("t-lock-down".into()),
            ip("10.9.70.7"),
        );
        t.begin(&s, &client, "fay").await.unwrap().succeeded().await;
        let a = t.begin_per_request(&s, &client, "fay").await.unwrap();
        assert!(matches!(a.held, PerRequestHeld::Reserved(_)));
        let bucket = s.ip_bucket("10.9.70.7");
        assert!(t.0.ip.peek(&bucket).await.is_err(), "no per-IP slot taken");
        a.settle(true).await;
    }

    /// A known client's right credential: few cache calls, and the slot
    /// comes back.
    #[tokio::test]
    async fn a_right_per_request_credential_costs_six_cache_calls() {
        let calls = Arc::new(CountingCalls::default());
        let cache: BoxedCache = calls.clone();
        let t = with_lockout(on_cache(&cache, LoginLimits::default()), calls.clone());
        let (s, client) = (LoginScope::TenantBasic("t-six".into()), ip("10.9.70.8"));
        t.begin(&s, &client, "gil").await.unwrap().succeeded().await;
        let before = calls.calls();
        let a = t.begin_per_request(&s, &client, "gil").await.unwrap();
        assert!(matches!(a.held, PerRequestHeld::Quiet(_)));
        a.settle(true).await;
        let spent = calls.calls() - before;
        assert!(spent <= 6, "{spent} cache calls");
        let key = account_key(&s.key(), "gil");
        assert_eq!(t.0.lockout().attempt_count(&key).await, 0);
    }

    /// A per-request credential waits at most one `hash_wait` before a 503.
    #[tokio::test]
    async fn a_per_request_credential_waits_one_deadline() {
        let t = Arc::new(LoginThrottle::new(LoginLimits::default()));
        let (s, client) = (
            LoginScope::TenantBasic("t-one-wait".into()),
            ip("10.9.70.4"),
        );
        let a = t.begin(&s, &client, "dan").await.unwrap();
        a.succeeded().await;
        let max = crate::account_lockout::shared().attempt_limit() as usize;
        let mut held = Vec::new();
        for _ in 0..max {
            held.push(t.begin_per_request(&s, &client, "dan").await.unwrap());
        }
        let wait = crate::passwords::hash_wait();
        let started = tokio::time::Instant::now();
        let late = {
            let (t, s, client) = (Arc::clone(&t), s.clone(), client.clone());
            tokio::spawn(async move { t.begin_per_request(&s, &client, "dan").await.is_ok() })
        };
        // Plain attempts fill the lock while the late one waits.
        tokio::time::sleep(wait / 2).await;
        let mut plain = Vec::new();
        for _ in 0..max {
            let (t, s, client) = (Arc::clone(&t), s.clone(), client.clone());
            plain.push(tokio::spawn(async move {
                let a = t.begin(&s, &client, "dan").await;
                tokio::time::sleep(wait * 3).await;
                drop(a);
            }));
        }
        tokio::time::sleep(wait * 2 / 5).await;
        drop(held.pop());
        let _ = late.await.unwrap();
        let took = started.elapsed();
        assert!(
            took < wait * 3 / 2,
            "waited {took:?} for a {wait:?} deadline"
        );
        drop(held);
        for p in plain {
            p.abort();
        }
    }
}