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
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
//! `ProxyManager`: unified proxy pool orchestrator.
//!
//! Assembles storage, rotation strategy, health checker, and per-proxy circuit
//! breakers into a single ergonomic API.
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
#[cfg(feature = "vendor-stickiness")]
use std::time::Duration;
use serde::Serialize;
use tokio::sync::RwLock;
use tokio::task::JoinHandle;
use tokio_util::sync::CancellationToken;
use uuid::Uuid;
use crate::circuit_breaker::CircuitBreaker;
use crate::error::{ProxyError, ProxyResult};
use crate::health::{HealthChecker, HealthMap};
#[cfg(feature = "coherence-validation")]
use crate::ports::coherence::{
BoxedCoherencePort, CoherenceContext, CoherencePolicy, CoherenceVerdict,
};
#[cfg(feature = "vendor-stickiness")]
use crate::session::SessionDecision;
use crate::session::{SessionMap, StickyPolicy};
#[cfg(feature = "vendor-stickiness")]
use crate::stickiness::VendorStickinessMap;
use crate::storage::ProxyStoragePort;
use crate::strategy::{
BoxedBayesianObserver, BoxedRotationStrategy, LeastUsedStrategy, NoopBayesianObserver,
ProxyCandidate, RandomStrategy, RoundRobinStrategy, WeightedStrategy,
capable_healthy_candidates,
};
#[cfg(feature = "vendor-stickiness")]
use crate::types::VendorId;
use crate::types::{CapabilityRequirement, Proxy, ProxyConfig};
// ─────────────────────────────────────────────────────────────────────────────
// PoolStats
// ─────────────────────────────────────────────────────────────────────────────
/// A snapshot of pool health at a point in time.
#[derive(Debug, Serialize)]
pub struct PoolStats {
/// Total proxies in the pool.
pub total: usize,
/// Proxies that passed the last health check.
pub healthy: usize,
/// Proxies whose circuit breaker is currently Open.
pub open: usize,
/// Active (non-expired) sticky sessions.
pub active_sessions: usize,
}
// ─────────────────────────────────────────────────────────────────────────────
// ProxyHandle
// ─────────────────────────────────────────────────────────────────────────────
/// RAII guard returned from [`ProxyManager::acquire_proxy`].
///
/// Call [`mark_success`](ProxyHandle::mark_success) once the request using
/// this proxy completes successfully. If the handle is dropped without a
/// success mark the circuit breaker is notified of a failure.
pub struct ProxyHandle {
/// URL of the selected proxy.
pub proxy_url: String,
circuit_breaker: Arc<CircuitBreaker>,
succeeded: AtomicBool,
/// Domain key to unbind from `sessions` on failure (sticky sessions only).
session_key: Option<String>,
sessions: Option<SessionMap>,
/// Stable proxy id — used to address the `observer` so a Bayesian
/// strategy can credit the right `Beta(α, β)` arm.
proxy_id: Uuid,
/// Optional observer receiving success/failure outcomes. The default is
/// [`NoopBayesianObserver`] (zero-cost no-op) so the manager can always
/// record outcomes without a feature check on the hot path.
observer: BoxedBayesianObserver,
}
impl ProxyHandle {
const fn new(
proxy_url: String,
circuit_breaker: Arc<CircuitBreaker>,
proxy_id: Uuid,
observer: BoxedBayesianObserver,
) -> Self {
Self {
proxy_url,
circuit_breaker,
succeeded: AtomicBool::new(false),
session_key: None,
sessions: None,
proxy_id,
observer,
}
}
const fn new_sticky(
proxy_url: String,
circuit_breaker: Arc<CircuitBreaker>,
session_key: String,
sessions: SessionMap,
proxy_id: Uuid,
observer: BoxedBayesianObserver,
) -> Self {
Self {
proxy_url,
circuit_breaker,
succeeded: AtomicBool::new(false),
session_key: Some(session_key),
sessions: Some(sessions),
proxy_id,
observer,
}
}
/// Create a no-proxy handle used when no proxy manager is configured.
///
/// The handle targets an empty URL and uses a noop circuit breaker that
/// can never trip; its Drop records a success so there are no false failures.
#[must_use]
pub fn direct() -> Self {
let noop_cb = Arc::new(CircuitBreaker::new(u32::MAX, u64::MAX));
let noop_observer: BoxedBayesianObserver = Arc::new(NoopBayesianObserver);
Self {
proxy_url: String::new(),
circuit_breaker: noop_cb,
succeeded: AtomicBool::new(true),
session_key: None,
sessions: None,
proxy_id: Uuid::nil(),
observer: noop_observer,
}
}
/// Signal that the request succeeded.
pub fn mark_success(&self) {
self.succeeded.store(true, Ordering::Release);
// Notify the observer after the circuit-breaker flag is set so
// the bookkeeping is consistent across both subsystems.
self.observer.observe(self.proxy_id, true);
}
}
impl std::fmt::Debug for ProxyHandle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ProxyHandle")
.field("proxy_url", &self.proxy_url)
.finish_non_exhaustive()
}
}
impl Drop for ProxyHandle {
fn drop(&mut self) {
if self.succeeded.load(Ordering::Acquire) {
self.circuit_breaker.record_success();
} else {
self.circuit_breaker.record_failure();
// Invalidate the sticky session so the next request picks a fresh proxy.
if let (Some(key), Some(sessions)) = (&self.session_key, &self.sessions) {
sessions.unbind(key);
}
self.observer.observe(self.proxy_id, false);
}
}
}
// ─────────────────────────────────────────────────────────────────────────────
// ProxyManager
// ─────────────────────────────────────────────────────────────────────────────
/// Unified proxy pool orchestrator.
///
/// Manage proxies via [`add_proxy`](ProxyManager::add_proxy) and
/// [`remove_proxy`](ProxyManager::remove_proxy), acquire one via
/// [`acquire_proxy`](ProxyManager::acquire_proxy), and start background
/// health checking with [`start`](ProxyManager::start).
///
/// # Quick start
///
/// ```rust,no_run
/// # async fn run() -> stygian_proxy::ProxyResult<()> {
/// use std::sync::Arc;
/// use stygian_proxy::{ProxyManager, ProxyConfig, Proxy, ProxyType};
/// use stygian_proxy::storage::MemoryProxyStore;
/// use stygian_proxy::types::{IpClass, ProxyCapabilities, TargetVendorCompatibility};
///
/// let storage = Arc::new(MemoryProxyStore::default());
/// let mgr = ProxyManager::with_round_robin(storage, ProxyConfig::default())?;
/// let (token, _handle) = mgr.start();
/// let proxy = mgr.add_proxy(Proxy {
/// url: "http://proxy.example.com:8080".into(),
/// proxy_type: ProxyType::Http,
/// username: None,
/// password: None,
/// weight: 1,
/// tags: vec![],
/// capabilities: ProxyCapabilities::default(),
/// ip_class: IpClass::Unknown,
/// target_compatibility: TargetVendorCompatibility::default(),
/// }).await?;
/// let handle = mgr.acquire_proxy().await?;
/// handle.mark_success();
/// token.cancel();
/// # Ok(())
/// # }
/// ```
pub struct ProxyManager {
storage: Arc<dyn ProxyStoragePort>,
strategy: BoxedRotationStrategy,
health_checker: HealthChecker,
circuit_breakers: Arc<RwLock<HashMap<Uuid, Arc<CircuitBreaker>>>>,
config: ProxyConfig,
/// Domain→proxy sticky session map (always present; logic depends on `config.sticky_policy`).
sessions: SessionMap,
/// Optional observer receiving success/failure outcomes. Wired by
/// [`ProxyManagerBuilder::with_thompson_sampling`] and other Bayesian
/// strategies. Defaults to [`NoopBayesianObserver`] so the hot path
/// does not branch on the feature.
observer: BoxedBayesianObserver,
/// Network-identity coherence validator used by
/// [`ProxyManager::acquire_proxy_with_coherence`]. Compiled to
/// `Option<…>` so the field exists uniformly with or without the
/// `coherence-validation` cargo feature; when the feature is off
/// the field is always `None` and the
/// `acquire_proxy_with_coherence` method is gated out of the public
/// surface entirely.
#[cfg(feature = "coherence-validation")]
coherence_validator: Option<BoxedCoherencePort>,
/// Per-vendor session stickiness policy consulted by
/// [`ProxyManager::acquire_for_domain_with_vendor`]. Compiled to a
/// concrete field (not `Option`) so the feature flag controls the
/// *integration surface* (the method and the builder step are
/// gated) without making the field polymorphic. Defaults to the
/// 2026 guide built-in policy matrix so the feature is on by
/// default in the `full` aggregator; operators can override via
/// [`ProxyManagerBuilder::stickiness_map`].
#[cfg(feature = "vendor-stickiness")]
stickiness_map: VendorStickinessMap,
}
impl ProxyManager {
/// Start a [`ProxyManagerBuilder`].
#[must_use]
pub fn builder() -> ProxyManagerBuilder {
ProxyManagerBuilder::default()
}
/// Convenience: round-robin rotation (default).
///
/// # Errors
///
/// Returns [`ProxyError::ConfigError`] when no storage is supplied to
/// the underlying builder.
pub fn with_round_robin(
storage: Arc<dyn ProxyStoragePort>,
config: ProxyConfig,
) -> ProxyResult<Self> {
Self::builder()
.storage(storage)
.strategy(Arc::new(RoundRobinStrategy::default()))
.config(config)
.build()
}
/// Convenience: random rotation.
///
/// # Errors
///
/// Returns [`ProxyError::ConfigError`] when no storage is supplied to
/// the underlying builder.
pub fn with_random(
storage: Arc<dyn ProxyStoragePort>,
config: ProxyConfig,
) -> ProxyResult<Self> {
Self::builder()
.storage(storage)
.strategy(Arc::new(RandomStrategy))
.config(config)
.build()
}
/// Convenience: weighted rotation.
///
/// # Errors
///
/// Returns [`ProxyError::ConfigError`] when no storage is supplied to
/// the underlying builder.
pub fn with_weighted(
storage: Arc<dyn ProxyStoragePort>,
config: ProxyConfig,
) -> ProxyResult<Self> {
Self::builder()
.storage(storage)
.strategy(Arc::new(WeightedStrategy))
.config(config)
.build()
}
/// Convenience: least-used rotation.
///
/// # Errors
///
/// Returns [`ProxyError::ConfigError`] when no storage is supplied to
/// the underlying builder.
pub fn with_least_used(
storage: Arc<dyn ProxyStoragePort>,
config: ProxyConfig,
) -> ProxyResult<Self> {
Self::builder()
.storage(storage)
.strategy(Arc::new(LeastUsedStrategy))
.config(config)
.build()
}
/// Convenience: Thompson-sampling Bayesian rotation.
///
/// The 2026 guide cites 76 % success with Thompson sampling vs 36 %
/// with round-robin on identical proxies and targets (L3018-3021).
/// `decay_interval` controls how often the per-proxy `Beta(α, β)`
/// counters are scaled down so non-stationary health is tracked over
/// time; the default of 5 minutes is a good fit for typical scrape
/// cadences.
///
/// Requires the `bayesian-rotation` cargo feature.
///
/// # Errors
///
/// Returns [`ProxyError::ConfigError`] when no storage is supplied to
/// the underlying builder.
#[cfg(feature = "bayesian-rotation")]
pub fn with_thompson_sampling(
storage: Arc<dyn ProxyStoragePort>,
config: ProxyConfig,
decay_interval: std::time::Duration,
) -> ProxyResult<Self> {
Self::builder()
.storage(storage)
.config(config)
.with_thompson_sampling(decay_interval)
.build()
}
// ── Pool mutations ────────────────────────────────────────────────────────
/// Add a proxy and register a circuit breaker for it. Returns the new ID.
///
/// The `circuit_breakers` write lock is held for the duration of the storage
/// write. This is intentional: [`acquire_proxy`](Self::acquire_proxy) holds
/// a read lock on the same map while it inspects candidates, so it cannot
/// proceed past that point until both the storage record *and* its CB entry
/// exist. Without this ordering a concurrent `acquire_proxy` could select
/// the new proxy before its CB was registered, breaking failure accounting.
///
/// # Errors
///
/// Returns [`ProxyError::StorageError`] when the underlying storage backend
/// rejects the new proxy record, or
/// [`ProxyError::InvalidGeoMetadata`]
/// when the proxy's geo-metadata fields fail ingest validation
/// (e.g. `asn = 0`, `city = ""`, `postal_code = ""`).
#[allow(clippy::significant_drop_tightening)]
pub async fn add_proxy(&self, proxy: Proxy) -> ProxyResult<Uuid> {
let mut cb_map = self.circuit_breakers.write().await;
let record = self.storage.add(proxy).await?;
cb_map.insert(
record.id,
Arc::new(CircuitBreaker::new(
self.config.circuit_open_threshold,
u64::try_from(self.config.circuit_half_open_after.as_millis()).unwrap_or(u64::MAX),
)),
);
Ok(record.id)
}
/// Remove a proxy from the pool and drop its circuit breaker.
///
/// # Errors
///
/// Returns [`ProxyError::StorageError`] when the underlying storage backend
/// reports the proxy as missing or the remove call fails.
pub async fn remove_proxy(&self, id: Uuid) -> ProxyResult<()> {
self.storage.remove(id).await?;
self.circuit_breakers.write().await.remove(&id);
Ok(())
}
/// Add a proxy with explicit geo metadata (ASN, city, postal code).
///
/// Convenience constructor for operator-curated pools that target
/// specific geographic or network ranges — the "Infatica-style
/// city, ZIP, and ASN filter" cited by the 2026 guide (L2837).
/// Constructs the [`Proxy`] and underlying
/// [`crate::ProxyCapabilities`] for the caller, populates the geo
/// fields, and runs the same ingest validation as
/// [`add_proxy`](Self::add_proxy) (so `asn = 0`, `city = ""`,
/// etc. are rejected with
/// [`ProxyError::InvalidGeoMetadata`]
/// before the record is stored).
///
/// The `proxy_type`, `username`, `password`, `weight`, `tags`, and
/// remaining `ProxyCapabilities` fields take their
/// `Default::default()` values; callers that need finer control
/// over those should build a [`Proxy`] directly and call
/// [`add_proxy`](Self::add_proxy) instead.
///
/// # Example
///
/// ```rust,no_run
/// # async fn run() -> stygian_proxy::ProxyResult<()> {
/// use std::sync::Arc;
/// use stygian_proxy::{ProxyManager, ProxyConfig};
/// use stygian_proxy::storage::MemoryProxyStore;
/// use stygian_proxy::types::well_known::KNOWN_ASN_CLOUDFLARE;
///
/// let store = Arc::new(MemoryProxyStore::default());
/// let mgr = ProxyManager::with_round_robin(store, ProxyConfig::default())?;
/// let _id = mgr.add_proxy_with_metadata(
/// "http://cf-exit.example.com:8080",
/// Some(KNOWN_ASN_CLOUDFLARE),
/// Some("San Francisco"),
/// Some("94110"),
/// ).await?;
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// Returns [`ProxyError::InvalidProxyUrl`]
/// when `url` is malformed, or
/// [`ProxyError::InvalidGeoMetadata`]
/// when any geo field fails the validation rules documented in
/// [`crate::types::validate_asn`], [`crate::types::validate_city`],
/// or [`crate::types::validate_postal_code`]. Storage failures
/// surface as [`ProxyError::StorageError`].
#[allow(clippy::significant_drop_tightening)]
pub async fn add_proxy_with_metadata(
&self,
url: &str,
asn: Option<u32>,
city: Option<&str>,
postal_code: Option<&str>,
) -> ProxyResult<Uuid> {
let capabilities = crate::types::ProxyCapabilities {
asn,
city: city.map(str::to_owned),
postal_code: postal_code.map(str::to_owned),
..Default::default()
};
let proxy = Proxy {
url: url.to_owned(),
proxy_type: crate::types::ProxyType::Http,
username: None,
password: None,
weight: 1,
tags: Vec::new(),
capabilities,
ip_class: crate::types::IpClass::Unknown,
target_compatibility: crate::types::TargetVendorCompatibility::default(),
};
self.add_proxy(proxy).await
}
// ── Background task ───────────────────────────────────────────────────────
/// Spawn the background health-check and session-purge tasks.
///
/// Returns a `(CancellationToken, JoinHandle)` pair. Cancel the token to
/// trigger a graceful shutdown; await the handle to ensure it finishes.
#[must_use]
pub fn start(&self) -> (CancellationToken, JoinHandle<()>) {
let token = CancellationToken::new();
let health_handle = self.health_checker.clone().spawn(token.clone());
let sessions = self.sessions.clone();
let purge_token = token.clone();
let purge_handle = tokio::spawn(async move {
let mut interval = tokio::time::interval(std::time::Duration::from_mins(1));
loop {
tokio::select! {
_ = interval.tick() => { let _ = sessions.purge_expired(); }
() = purge_token.cancelled() => break,
}
}
});
let combined = tokio::spawn(async move {
let _ = tokio::join!(health_handle, purge_handle);
});
(token, combined)
}
/// Pre-warm the Bayesian observer with a synthetic outcome for a proxy.
///
/// This is the same call that `ProxyHandle::mark_success` and the
/// `Drop` impl make at runtime, exposed publicly so callers can
/// pre-seed the bandit from a known-good (or known-bad) prior before
/// serving traffic. Most useful for tests and for warm-starting the
/// pool from an external health-check feed.
pub fn strategy_warmup_observe(&self, proxy_id: Uuid, success: bool) {
self.observer.observe(proxy_id, success);
}
/// Read-only view of the underlying proxy storage. Useful for
/// tests, MCP introspection, and warm-up helpers that need to map
/// `url → id` without traversing the public API surface.
#[must_use]
pub fn storage(&self) -> &Arc<dyn ProxyStoragePort> {
&self.storage
}
// ── Proxy selection ───────────────────────────────────────────────────────
/// Select one proxy via the rotation strategy, returning its URL, circuit
/// breaker, and ID. Used by both [`acquire_proxy`](Self::acquire_proxy) and
/// [`acquire_for_domain`](Self::acquire_for_domain).
#[allow(clippy::significant_drop_tightening)]
async fn select_proxy_inner(&self) -> ProxyResult<(String, Arc<CircuitBreaker>, Uuid)> {
let with_metrics = self.storage.list_with_metrics().await?;
if with_metrics.is_empty() {
return Err(ProxyError::PoolExhausted);
}
// Drop both read guards before the async `strategy.select` await to avoid holding
// locks across await points. After selection, re-acquire for a single O(1) lookup.
let candidates = {
let health_map_ref = Arc::clone(self.health_checker.health_map());
let health_map = health_map_ref.read().await;
let cb_map_ref = Arc::clone(&self.circuit_breakers);
let cb_map = cb_map_ref.read().await;
let candidates: Vec<ProxyCandidate> = with_metrics
.iter()
.map(|(record, metrics)| {
let healthy = health_map.get(&record.id).copied().unwrap_or(true);
let available = cb_map.get(&record.id).is_none_or(|cb| cb.is_available());
ProxyCandidate {
id: record.id,
weight: record.proxy.weight,
metrics: Arc::clone(metrics),
healthy: healthy && available,
capabilities: record.proxy.capabilities.clone(),
}
})
.collect();
candidates
// health_map and cb_map drop here
};
let selected = self.strategy.select(&candidates).await?;
let id = selected.id;
// Single O(1) lookup — re-acquire only after the await point.
let cb = self
.circuit_breakers
.read()
.await
.get(&id)
.cloned()
.ok_or(ProxyError::PoolExhausted)?;
let url = with_metrics
.iter()
.find(|(r, _)| r.id == id)
.map(|(r, _)| r.proxy.url.clone())
.unwrap_or_default();
Ok((url, cb, id))
}
/// Acquire a proxy from the pool.
///
/// Builds [`ProxyCandidate`] entries from current storage, consulting the
/// health map and each proxy's circuit breaker to set the `healthy` flag.
/// Delegates selection to the configured [`crate::strategy::RotationStrategy`].
///
/// # Errors
///
/// Returns [`ProxyError::StorageError`] when the storage backend cannot list
/// proxies, or [`ProxyError::NoCompatibleProxy`] when no healthy proxy
/// is available.
pub async fn acquire_proxy(&self) -> ProxyResult<ProxyHandle> {
let (url, cb, id) = self.select_proxy_inner().await?;
Ok(ProxyHandle::new(url, cb, id, Arc::clone(&self.observer)))
}
/// Acquire a proxy that satisfies `req` from the pool.
///
/// Filters the candidate list to healthy proxies whose
/// [`ProxyCapabilities`](crate::types::ProxyCapabilities) satisfy every
/// flag in `req`, then delegates to the configured rotation strategy.
///
/// Returns [`ProxyError::NoCompatibleProxy`] when no healthy proxy meets
/// the capability requirements.
///
/// # Example
/// ```rust,no_run
/// use stygian_proxy::{ProxyManager, ProxyManagerBuilder, CapabilityRequirement};
///
/// async fn example(manager: &ProxyManager) {
/// let req = CapabilityRequirement { require_https_connect: true, ..Default::default() };
/// let handle = manager.acquire_with_capabilities(&req).await.unwrap();
/// println!("url: {}", handle.proxy_url);
/// }
/// ```
///
/// # Errors
///
/// Returns [`ProxyError::StorageError`] when the storage backend cannot list
/// proxies, or [`ProxyError::NoCompatibleProxy`] when no healthy proxy
/// satisfies the supplied [`CapabilityRequirement`].
pub async fn acquire_with_capabilities(
&self,
req: &CapabilityRequirement,
) -> ProxyResult<ProxyHandle> {
let with_metrics = self.storage.list_with_metrics().await?;
if with_metrics.is_empty() {
return Err(ProxyError::PoolExhausted);
}
let candidates = {
let health_map_ref = Arc::clone(self.health_checker.health_map());
let health_map = health_map_ref.read().await;
let cb_map_ref = Arc::clone(&self.circuit_breakers);
let cb_map = cb_map_ref.read().await;
let candidates: Vec<ProxyCandidate> = with_metrics
.iter()
.map(|(record, metrics)| {
let healthy = health_map.get(&record.id).copied().unwrap_or(true);
let available = cb_map.get(&record.id).is_none_or(|cb| cb.is_available());
ProxyCandidate {
id: record.id,
weight: record.proxy.weight,
metrics: Arc::clone(metrics),
healthy: healthy && available,
capabilities: record.proxy.capabilities.clone(),
}
})
.collect();
candidates
};
// Filter to only those that satisfy the capability requirement.
let compatible: Vec<ProxyCandidate> = capable_healthy_candidates(&candidates, req)
.into_iter()
.cloned()
.collect();
if compatible.is_empty() {
return Err(ProxyError::NoCompatibleProxy);
}
let selected = self.strategy.select(&compatible).await?;
let id = selected.id;
let cb = self
.circuit_breakers
.read()
.await
.get(&id)
.cloned()
.ok_or(ProxyError::PoolExhausted)?;
let url = with_metrics
.iter()
.find(|(r, _)| r.id == id)
.map(|(r, _)| r.proxy.url.clone())
.unwrap_or_default();
Ok(ProxyHandle::new(url, cb, id, Arc::clone(&self.observer)))
}
/// Acquire a proxy for `domain`, honouring the configured sticky-session
/// policy.
///
/// - When [`StickyPolicy::Disabled`] is active, behaves identically to
/// [`acquire_proxy`](Self::acquire_proxy).
/// - When [`StickyPolicy::Domain`] is active and a fresh session exists
/// for `domain`, the **same proxy** is returned for the TTL duration.
/// - If the bound proxy's circuit breaker has tripped or the proxy has been
/// removed, the stale session is invalidated and a fresh proxy is acquired
/// and bound.
///
/// The returned [`ProxyHandle`] automatically invalidates the session on
/// drop if not marked as successful.
///
/// # Errors
///
/// Returns [`ProxyError::StorageError`] when the storage backend fails, or
/// [`ProxyError::NoCompatibleProxy`] when no healthy proxy is available
/// (including when a sticky-bound proxy is unhealthy and the fallback
/// also exhausts the pool).
pub async fn acquire_for_domain(&self, domain: &str) -> ProxyResult<ProxyHandle> {
let ttl = match &self.config.sticky_policy {
StickyPolicy::Disabled => return self.acquire_proxy().await,
StickyPolicy::Domain { ttl } => *ttl,
};
// Check for an active, non-expired session.
if let Some(proxy_id) = self.sessions.lookup(domain) {
let cb_map = self.circuit_breakers.read().await;
if let Some(cb) = cb_map.get(&proxy_id).cloned()
&& cb.is_available()
{
// Lookup proxy URL from storage.
let with_metrics = self.storage.list_with_metrics().await?;
if let Some((record, _)) = with_metrics.iter().find(|(r, _)| r.id == proxy_id) {
let url = record.proxy.url.clone();
drop(cb_map);
return Ok(ProxyHandle::new_sticky(
url,
cb,
domain.to_string(),
self.sessions.clone(),
proxy_id,
Arc::clone(&self.observer),
));
}
}
// CB tripped or proxy no longer in pool — invalidate.
drop(cb_map);
self.sessions.unbind(domain);
}
// No valid session: acquire fresh proxy via strategy and bind.
let (url, cb, proxy_id) = self.select_proxy_inner().await?;
self.sessions.bind(domain, proxy_id, ttl);
Ok(ProxyHandle::new_sticky(
url,
cb,
domain.to_string(),
self.sessions.clone(),
proxy_id,
Arc::clone(&self.observer),
))
}
/// Acquire a proxy for `(domain, vendor)`, honouring the
/// per-vendor stickiness policy from
/// [`VendorStickinessMap::with_builtin_defaults`].
///
/// Behind the `vendor-stickiness` cargo feature (off by default).
/// The default installed on the manager is the 2026 guide matrix;
/// operators can replace it via
/// [`ProxyManagerBuilder::stickiness_map`].
///
/// Behaviour per [`crate::stickiness::StickinessPolicy`]:
///
/// | Policy | Behaviour |
/// | -------------------------------------------- | ---------------------------------------------------------------------------------- |
/// | `StickyForever` / `StickyForTtl` | Reuse an existing binding when present; otherwise pick fresh via the strategy and bind for the policy TTL. |
/// | `FreshPerRequest` | Always pick a fresh proxy via the strategy. No binding is created. |
/// | `FreshPerDomain` | Always pick a fresh proxy and evict any existing binding for `domain`. |
/// | `StickyForRequestCount(_)` | Treated as `FreshPerRequest` at this layer (per-request counters are out of scope). |
/// | Unknown vendor (no entry in the map) | Falls back to `FreshPerRequest` — the safest default. |
///
/// If a bound proxy's circuit breaker has tripped or the proxy has
/// been removed, the stale binding is invalidated and a fresh
/// proxy is acquired (mirroring
/// [`acquire_for_domain`](Self::acquire_for_domain)).
///
/// # Errors
///
/// Returns [`ProxyError::StorageError`] when the storage backend
/// fails, or [`ProxyError::NoCompatibleProxy`] when no healthy proxy
/// is available (including when a sticky-bound proxy is unhealthy
/// and the fallback also exhausts the pool).
#[cfg(feature = "vendor-stickiness")]
pub async fn acquire_for_domain_with_vendor(
&self,
domain: &str,
vendor: VendorId,
) -> ProxyResult<ProxyHandle> {
let decision = self
.sessions
.acquire_session(domain, vendor, &self.stickiness_map);
match decision {
SessionDecision::UseSticky(proxy_id) => {
// Same fallback as `acquire_for_domain`: verify the
// binding is still valid (proxy in pool + CB available)
// before handing back the handle. Any failure here falls
// through to acquire-and-bind fresh.
let cb = self.lookup_validated_cb(proxy_id).await?;
if let Some(cb) = cb {
let url = self.lookup_url(proxy_id).await?;
if let Some(url) = url {
return Ok(ProxyHandle::new_sticky(
url,
cb,
domain.to_string(),
self.sessions.clone(),
proxy_id,
Arc::clone(&self.observer),
));
}
}
// Stale binding: drop it and fall through to fresh acquisition.
self.sessions.unbind(domain);
let (url, cb, proxy_id) = self.select_proxy_inner().await?;
// Look up the policy TTL again so we bind with the right
// value — `policy_map.for_vendor(vendor)` is cheap
// (`BTreeMap::get`) and the cache hit on this branch is
// the common case.
let ttl = self.stickiness_ttl(vendor);
self.sessions.bind(domain, proxy_id, ttl);
Ok(ProxyHandle::new_sticky(
url,
cb,
domain.to_string(),
self.sessions.clone(),
proxy_id,
Arc::clone(&self.observer),
))
}
SessionDecision::AcquireFresh => self.acquire_proxy().await,
SessionDecision::AcquireAndBind(ttl) => {
let (url, cb, proxy_id) = self.select_proxy_inner().await?;
self.sessions.bind(domain, proxy_id, ttl);
Ok(ProxyHandle::new_sticky(
url,
cb,
domain.to_string(),
self.sessions.clone(),
proxy_id,
Arc::clone(&self.observer),
))
}
}
}
/// Look up the per-vendor TTL to use when binding. Falls back to
/// `Duration::from_mins(30)` (the Akamai default) for any policy
/// that is not sticky — the call sites always check `policy_map`
/// before binding so the fallback is unreachable in practice, but a
/// defined default keeps the API total.
#[cfg(feature = "vendor-stickiness")]
fn stickiness_ttl(&self, vendor: VendorId) -> Duration {
use crate::stickiness::StickinessPolicy;
match self.stickiness_map.for_vendor(vendor) {
StickinessPolicy::StickyForever => Duration::MAX,
StickinessPolicy::StickyForTtl { ttl } => ttl,
// Non-sticky policies never reach the `AcquireAndBind` branch.
_ => Duration::from_mins(30),
}
}
/// Look up the circuit breaker for `proxy_id` and confirm it is
/// still `available`. Returns `Ok(None)` when the CB is missing,
/// tripped, or absent from the pool.
#[cfg(feature = "vendor-stickiness")]
#[allow(clippy::significant_drop_tightening)]
async fn lookup_validated_cb(
&self,
proxy_id: Uuid,
) -> ProxyResult<Option<Arc<CircuitBreaker>>> {
let cb_map = self.circuit_breakers.read().await;
let Some(cb) = cb_map.get(&proxy_id).cloned() else {
return Ok(None);
};
if !cb.is_available() {
return Ok(None);
}
Ok(Some(cb))
}
/// Look up the proxy URL for `proxy_id` from storage. Returns
/// `Ok(None)` when the proxy has been removed.
#[cfg(feature = "vendor-stickiness")]
async fn lookup_url(&self, proxy_id: Uuid) -> ProxyResult<Option<String>> {
let with_metrics = self.storage.list_with_metrics().await?;
Ok(with_metrics
.iter()
.find(|(r, _)| r.id == proxy_id)
.map(|(r, _)| r.proxy.url.clone()))
}
// ── Stats ─────────────────────────────────────────────────────────────────
/// Return a health snapshot of the pool.
///
/// # Errors
///
/// Returns [`ProxyError::StorageError`] when the storage backend cannot list
/// proxies, or when the internal lock is poisoned.
pub async fn pool_stats(&self) -> ProxyResult<PoolStats> {
let records = self.storage.list().await?;
let total = records.len();
let health_map = self.health_checker.health_map().read().await;
let cb_map = self.circuit_breakers.read().await;
let mut healthy = 0usize;
let mut open = 0usize;
for r in &records {
if health_map.get(&r.id).copied().unwrap_or(true) {
healthy += 1;
}
if cb_map.get(&r.id).is_some_and(|cb| !cb.is_available()) {
open += 1;
}
}
drop(health_map);
drop(cb_map);
Ok(PoolStats {
total,
healthy,
open,
active_sessions: self.sessions.active_count(),
})
}
/// Acquire a proxy from the pool and run the network-identity
/// coherence check before returning the handle.
///
/// Behind the `coherence-validation` cargo feature (off by
/// default). The default validator is
/// [`crate::adapters::coherence::DefaultCoherenceValidator`];
/// operators can plug in their own implementation via
/// [`ProxyManagerBuilder::coherence_validator`].
///
/// Mismatch handling follows [`CoherencePolicy`]:
///
/// - [`CoherenceVerdict::Coherent`] — proxy is returned.
/// - [`CoherenceVerdict::Mismatch`] on a `Hard` field that
/// `policy.hard_fail_on` covers — returns
/// [`ProxyError::CoherenceMismatch`].
/// - [`CoherenceVerdict::Mismatch`] on any other field — logged
/// (advisory) and the proxy is returned.
/// - [`CoherenceVerdict::Unknown`] — logged at `debug` level and
/// the proxy is returned (operators opt into hard-fail by
/// registering specific fields).
///
/// # Errors
///
/// Returns:
/// - [`ProxyError::StorageError`] when the storage backend cannot
/// list proxies.
/// - [`ProxyError::NoCompatibleProxy`] when no healthy proxy is
/// available.
/// - [`ProxyError::ConfigError`] when the manager has no coherence
/// validator wired in (build with `coherence-validation` or
/// call [`ProxyManagerBuilder::coherence_validator`]).
/// - [`ProxyError::CoherenceMismatch`] when the policy hard-fails
/// on the offending field.
#[cfg(feature = "coherence-validation")]
pub async fn acquire_proxy_with_coherence(
&self,
ctx: &CoherenceContext,
policy: &CoherencePolicy,
) -> ProxyResult<ProxyHandle> {
let validator = self.coherence_validator.as_ref().ok_or_else(|| {
ProxyError::ConfigError(
"ProxyManager::acquire_proxy_with_coherence: no coherence_validator wired in; enable the `coherence-validation` cargo feature or call ProxyManagerBuilder::coherence_validator(...)"
.into(),
)
})?;
let handle = self.acquire_proxy().await?;
let verdict = validator.evaluate(ctx);
match verdict {
CoherenceVerdict::Coherent => Ok(handle),
CoherenceVerdict::Mismatch { field, severity } => {
if policy.is_hard_fail(field) && severity.is_hard() {
Err(ProxyError::CoherenceMismatch { field, severity })
} else {
tracing::warn!(
target: "stygian_proxy::coherence",
field = %field,
severity = %severity,
"coherence mismatch (advisory) — proceeding with the selected proxy"
);
Ok(handle)
}
}
CoherenceVerdict::Unknown(reason) => {
tracing::debug!(
target: "stygian_proxy::coherence",
reason = %reason,
"coherence verdict unknown — proceeding with the selected proxy"
);
Ok(handle)
}
}
}
}
// ─────────────────────────────────────────────────────────────────────────────
// ProxyManagerBuilder
// ─────────────────────────────────────────────────────────────────────────────
/// Fluent builder for [`ProxyManager`].
#[derive(Default)]
pub struct ProxyManagerBuilder {
storage: Option<Arc<dyn ProxyStoragePort>>,
strategy: Option<BoxedRotationStrategy>,
config: Option<ProxyConfig>,
/// Optional observer that receives success/failure outcomes. Defaults
/// to [`NoopBayesianObserver`] when no strategy wires a real one in.
observer: Option<BoxedBayesianObserver>,
/// Optional network-identity coherence validator. When unset and the
/// `coherence-validation` cargo feature is enabled, the default
/// [`crate::adapters::coherence::DefaultCoherenceValidator`] is
/// wired in at build time.
#[cfg(feature = "coherence-validation")]
coherence_validator: Option<BoxedCoherencePort>,
/// Optional per-vendor stickiness map. When unset and the
/// `vendor-stickiness` cargo feature is enabled, the default
/// [`VendorStickinessMap::with_builtin_defaults`] is wired in at
/// build time so the 2026 guide matrix is active by default.
#[cfg(feature = "vendor-stickiness")]
stickiness_map: Option<VendorStickinessMap>,
}
impl ProxyManagerBuilder {
#[must_use]
pub fn storage(mut self, s: Arc<dyn ProxyStoragePort>) -> Self {
self.storage = Some(s);
self
}
#[must_use]
pub fn strategy(mut self, s: BoxedRotationStrategy) -> Self {
self.strategy = Some(s);
self
}
#[must_use]
pub fn config(mut self, c: ProxyConfig) -> Self {
self.config = Some(c);
self
}
/// Set a custom [`crate::BayesianObserver`]. Mostly useful for tests and for
/// callers who want to plug in their own bandit algorithm without
/// using [`with_thompson_sampling`](Self::with_thompson_sampling).
#[must_use]
pub fn observer(mut self, o: BoxedBayesianObserver) -> Self {
self.observer = Some(o);
self
}
/// Install a custom network-identity coherence validator.
///
/// When unset (and the `coherence-validation` cargo feature is
/// enabled) the build step installs
/// [`crate::adapters::coherence::DefaultCoherenceValidator`] as
/// the default.
#[cfg(feature = "coherence-validation")]
#[must_use]
pub fn coherence_validator(mut self, validator: BoxedCoherencePort) -> Self {
self.coherence_validator = Some(validator);
self
}
/// Wire a [`ThompsonStrategy`](crate::strategy::ThompsonStrategy) into
/// the manager and use it as both the rotation strategy *and* the
/// observer so success/failure outcomes are recorded back into the
/// bandit on every [`ProxyHandle`]
/// drop. Mirrors `with_random` / `with_weighted` etc. on the
/// convenience constructor side; requires the `bayesian-rotation`
/// cargo feature.
#[cfg(feature = "bayesian-rotation")]
#[must_use]
pub fn with_thompson_sampling(mut self, decay_interval: std::time::Duration) -> Self {
let strategy = Arc::new(crate::strategy::ThompsonStrategy::with_decay(
decay_interval,
crate::strategy::thompson::DEFAULT_DECAY_FACTOR,
));
// The same `Arc` plays both roles — the strategy *is* the
// observer. Storing the same Arc twice would create a reference
// cycle on drop; cloning the Arc and keeping one reference in
// each field is fine because `Arc` releases the heap on the last
// `Drop` (which only happens when both the strategy field and the
// observer field have been released).
let observer: BoxedBayesianObserver = Arc::clone(&strategy) as BoxedBayesianObserver;
self.strategy = Some(strategy);
self.observer = Some(observer);
self
}
/// Install a custom per-vendor stickiness map.
///
/// When unset (and the `vendor-stickiness` cargo feature is enabled)
/// the build step installs
/// [`VendorStickinessMap::with_builtin_defaults`] as the default,
/// matching the 2026 guide matrix (Akamai → 30min sticky,
/// `DataDome` → fresh per request, etc.). Pass
/// [`VendorStickinessMap::new`] to opt out of built-in defaults and
/// use "fresh for every vendor" as the safe baseline.
#[cfg(feature = "vendor-stickiness")]
#[must_use]
pub fn stickiness_map(mut self, map: VendorStickinessMap) -> Self {
self.stickiness_map = Some(map);
self
}
/// Build the [`ProxyManager`].
///
/// Defaults: strategy = `RoundRobinStrategy`, config = `ProxyConfig::default()`,
/// observer = `NoopBayesianObserver`.
///
/// Returns an error if no storage was set.
///
/// # Errors
///
/// Returns [`ProxyError::ConfigError`] when no `storage` was supplied to
/// the builder.
pub fn build(self) -> ProxyResult<ProxyManager> {
let storage = self.storage.ok_or_else(|| {
ProxyError::ConfigError("ProxyManagerBuilder: storage is required".into())
})?;
let strategy = self
.strategy
.unwrap_or_else(|| Arc::new(RoundRobinStrategy::default()));
let observer = self
.observer
.unwrap_or_else(|| Arc::new(NoopBayesianObserver));
let config = self.config.unwrap_or_default();
let health_map: HealthMap = Arc::new(RwLock::new(HashMap::new()));
let checker = HealthChecker::new(
config.clone(),
Arc::clone(&storage),
Arc::clone(&health_map),
);
#[cfg(feature = "tls-profiled")]
let health_checker = if let Some(mode) = config.profiled_request_mode {
checker.with_profiled_mode(mode)?
} else {
checker
};
#[cfg(not(feature = "tls-profiled"))]
let health_checker = checker;
Ok(ProxyManager {
storage,
strategy,
health_checker,
circuit_breakers: Arc::new(RwLock::new(HashMap::new())),
config,
sessions: SessionMap::new(),
observer,
#[cfg(feature = "coherence-validation")]
coherence_validator: self.coherence_validator.or_else(|| {
Some(std::sync::Arc::new(
crate::adapters::coherence::DefaultCoherenceValidator,
))
}),
#[cfg(feature = "vendor-stickiness")]
stickiness_map: self
.stickiness_map
.unwrap_or_else(VendorStickinessMap::with_builtin_defaults),
})
}
}
// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────
#[cfg(test)]
#[allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::significant_drop_tightening,
clippy::manual_let_else,
clippy::panic,
clippy::indexing_slicing
)]
mod tests {
use std::collections::HashSet;
use std::time::Duration;
use super::*;
use crate::circuit_breaker::{STATE_CLOSED, STATE_OPEN};
use crate::storage::MemoryProxyStore;
use crate::types::ProxyType;
fn make_proxy(url: &str) -> Proxy {
Proxy {
url: url.into(),
proxy_type: ProxyType::Http,
username: None,
password: None,
weight: 1,
tags: vec![],
capabilities: crate::types::ProxyCapabilities::default(),
ip_class: crate::types::IpClass::Unknown,
target_compatibility: crate::types::TargetVendorCompatibility::default(),
}
}
fn storage() -> Arc<MemoryProxyStore> {
Arc::new(MemoryProxyStore::default())
}
/// Round-robin across 3 proxies × 10 acquisitions should hit all three.
#[tokio::test]
async fn round_robin_distribution() {
let store = storage();
let mgr = ProxyManager::with_round_robin(store.clone(), ProxyConfig::default()).unwrap();
mgr.add_proxy(make_proxy("http://a.test:8080"))
.await
.unwrap();
mgr.add_proxy(make_proxy("http://b.test:8080"))
.await
.unwrap();
mgr.add_proxy(make_proxy("http://c.test:8080"))
.await
.unwrap();
let mut seen = HashSet::new();
for _ in 0..10 {
let h = mgr.acquire_proxy().await.unwrap();
h.mark_success();
seen.insert(h.proxy_url.clone());
}
assert_eq!(seen.len(), 3, "all three proxies should have been selected");
}
/// T95 hot-path budget: 1 000 sequential healthy-pool acquisitions
/// (with the new `ip_class` + `target_compatibility` field-compare
/// added) must finish well under 1 s. The `crates/stygian-proxy/AGENTS.md`
/// hot-path target is 1 µs per acquire; 1 000 acquisitions should
/// stay under 100 ms in the common (healthy pool) case on any modern
/// laptop. We assert a generous 1 s budget to keep the test robust
/// under CI load while still catching a 100× regression.
#[tokio::test]
async fn acquire_proxy_hot_path_budget() {
let store = storage();
let mgr = ProxyManager::with_round_robin(store.clone(), ProxyConfig::default()).unwrap();
mgr.add_proxy(make_proxy("http://a.test:8080"))
.await
.unwrap();
mgr.add_proxy(make_proxy("http://b.test:8080"))
.await
.unwrap();
mgr.add_proxy(make_proxy("http://c.test:8080"))
.await
.unwrap();
let start = std::time::Instant::now();
for _ in 0..1_000 {
let h = mgr.acquire_proxy().await.unwrap();
h.mark_success();
}
let elapsed = start.elapsed();
assert!(
elapsed < std::time::Duration::from_secs(1),
"1000 acquisitions took {elapsed:?}; hot-path budget violated"
);
}
/// T95 hot-path budget: 1 000 capability-aware acquisitions with the
/// new `require_ip_class` / `target_vendor` filter (which clone the
/// `TargetVendorCompatibility` map and look up a `BTreeMap` entry on
/// every candidate) must also stay under 1 s. The `BTreeMap` clone
/// is empty for default-tagged proxies so the cost stays dominated
/// by the per-candidate field-compare.
#[tokio::test]
async fn acquire_with_capabilities_hot_path_budget() {
let store = storage();
let mgr = ProxyManager::with_round_robin(store.clone(), ProxyConfig::default()).unwrap();
mgr.add_proxy(make_proxy("http://a.test:8080"))
.await
.unwrap();
mgr.add_proxy(make_proxy("http://b.test:8080"))
.await
.unwrap();
mgr.add_proxy(make_proxy("http://c.test:8080"))
.await
.unwrap();
// Empty requirement — the new IP-class + target-vendor branches
// short-circuit immediately.
let req = crate::types::CapabilityRequirement::default();
let start = std::time::Instant::now();
for _ in 0..1_000 {
let h = mgr.acquire_with_capabilities(&req).await.unwrap();
h.mark_success();
}
let elapsed = start.elapsed();
assert!(
elapsed < std::time::Duration::from_secs(1),
"1000 capability-aware acquisitions took {elapsed:?}; hot-path budget violated"
);
}
/// When all circuit breakers are open the manager returns `AllProxiesUnhealthy`.
#[tokio::test]
async fn all_open_returns_error() {
let store = storage();
let mgr = ProxyManager::with_round_robin(
store.clone(),
ProxyConfig {
circuit_open_threshold: 1,
..ProxyConfig::default()
},
)
.unwrap();
let id = mgr
.add_proxy(make_proxy("http://x.test:8080"))
.await
.unwrap();
// Manually trip the circuit breaker.
{
let map = mgr.circuit_breakers.read().await;
let cb = map.get(&id).unwrap();
cb.record_failure();
}
let err = mgr.acquire_proxy().await.unwrap_err();
assert!(
matches!(err, ProxyError::AllProxiesUnhealthy),
"expected AllProxiesUnhealthy, got {err:?}"
);
}
/// Dropping a handle without `mark_success` records a failure.
#[tokio::test]
async fn handle_drop_records_failure() {
let store = storage();
let mgr = ProxyManager::with_round_robin(
store.clone(),
ProxyConfig {
circuit_open_threshold: 1,
..ProxyConfig::default()
},
)
.unwrap();
let id = mgr
.add_proxy(make_proxy("http://y.test:8080"))
.await
.unwrap();
{
let _h = mgr.acquire_proxy().await.unwrap();
// drop without mark_success → failure recorded
}
let cb_map = mgr.circuit_breakers.read().await;
let cb = cb_map.get(&id).unwrap();
assert_eq!(cb.state(), STATE_OPEN);
}
/// A handle marked as successful keeps the circuit breaker Closed.
#[tokio::test]
async fn handle_success_keeps_closed() {
let store = storage();
let mgr = ProxyManager::with_round_robin(store.clone(), ProxyConfig::default()).unwrap();
let id = mgr
.add_proxy(make_proxy("http://z.test:8080"))
.await
.unwrap();
let h = mgr.acquire_proxy().await.unwrap();
h.mark_success();
drop(h);
let cb_map = mgr.circuit_breakers.read().await;
let cb = cb_map.get(&id).unwrap();
assert_eq!(cb.state(), STATE_CLOSED);
}
/// `start()` launches the health checker and `cancel` causes clean exit.
#[tokio::test]
async fn start_and_graceful_shutdown() {
let store = storage();
let mgr = ProxyManager::with_round_robin(
store,
ProxyConfig {
health_check_interval: Duration::from_hours(1),
..ProxyConfig::default()
},
)
.unwrap();
let (token, handle) = mgr.start();
token.cancel();
let result = tokio::time::timeout(Duration::from_secs(1), handle).await;
assert!(result.is_ok(), "health checker task should exit within 1s");
}
#[cfg(feature = "tls-profiled")]
#[tokio::test]
async fn builder_accepts_profiled_request_mode_preset() {
let store = storage();
let cfg = ProxyConfig {
profiled_request_mode: Some(crate::types::ProfiledRequestMode::Preset),
..ProxyConfig::default()
};
let result = ProxyManager::builder()
.storage(store)
.strategy(Arc::new(RoundRobinStrategy::default()))
.config(cfg)
.build();
assert!(
result.is_ok(),
"builder should accept profiled preset mode: {:?}",
result.err()
);
}
#[cfg(feature = "tls-profiled")]
#[tokio::test]
async fn builder_rejects_profiled_request_mode_strict_all_for_chrome() {
let store = storage();
let cfg = ProxyConfig {
profiled_request_mode: Some(crate::types::ProfiledRequestMode::StrictAll),
..ProxyConfig::default()
};
let result = ProxyManager::builder()
.storage(store)
.strategy(Arc::new(RoundRobinStrategy::default()))
.config(cfg)
.build();
let Err(err) = result else {
panic!("strict_all should fail for default Chrome baseline profile")
};
assert!(
matches!(err, ProxyError::ConfigError(_)),
"expected ConfigError, got {err:?}"
);
}
// ── sticky session tests ─────────────────────────────────────────────────
fn sticky_config() -> ProxyConfig {
use crate::session::StickyPolicy;
ProxyConfig {
sticky_policy: StickyPolicy::domain_default(),
..ProxyConfig::default()
}
}
/// Two consecutive `acquire_for_domain` calls return the same proxy.
#[tokio::test]
async fn sticky_same_domain_returns_same_proxy() {
let store = storage();
let mgr = ProxyManager::with_round_robin(store, sticky_config()).unwrap();
mgr.add_proxy(make_proxy("http://p1.test:8080"))
.await
.unwrap();
mgr.add_proxy(make_proxy("http://p2.test:8080"))
.await
.unwrap();
let h1 = mgr.acquire_for_domain("example.com").await.unwrap();
let url1 = h1.proxy_url.clone();
h1.mark_success();
let h2 = mgr.acquire_for_domain("example.com").await.unwrap();
let url2 = h2.proxy_url.clone();
h2.mark_success();
assert_eq!(url1, url2, "same domain should return the same proxy");
}
/// Different domains each get their own proxy (when enough proxies exist).
#[tokio::test]
async fn sticky_different_domains_may_differ() {
let store = storage();
let mgr = ProxyManager::with_round_robin(store, sticky_config()).unwrap();
mgr.add_proxy(make_proxy("http://pa.test:8080"))
.await
.unwrap();
mgr.add_proxy(make_proxy("http://pb.test:8080"))
.await
.unwrap();
let ha = mgr.acquire_for_domain("a.com").await.unwrap();
let url_a = ha.proxy_url.clone();
ha.mark_success();
let hb = mgr.acquire_for_domain("b.com").await.unwrap();
let url_b = hb.proxy_url.clone();
hb.mark_success();
// With round-robin and two proxies the second domain gets the other one.
assert_ne!(
url_a, url_b,
"different domains should get different proxies"
);
}
/// After TTL expiry the session is treated as gone; a (possibly different)
/// proxy is re-acquired and the basic contract (no panic) still holds.
#[tokio::test]
async fn sticky_expired_session_re_acquires() {
use crate::session::StickyPolicy;
let store = storage();
let mgr = ProxyManager::with_round_robin(
store,
ProxyConfig {
sticky_policy: StickyPolicy::domain(Duration::from_millis(1)),
..ProxyConfig::default()
},
)
.unwrap();
mgr.add_proxy(make_proxy("http://x.test:8080"))
.await
.unwrap();
let h1 = mgr.acquire_for_domain("expired.com").await.unwrap();
h1.mark_success();
// Let the session expire.
tokio::time::sleep(Duration::from_millis(5)).await;
// Re-acquiring should not panic or error.
let h2 = mgr.acquire_for_domain("expired.com").await.unwrap();
h2.mark_success();
}
/// When the bound proxy's CB trips, the session is invalidated and a new
/// proxy is acquired on next call.
#[tokio::test]
async fn sticky_cb_trip_invalidates_session() {
let store = storage();
let mgr = ProxyManager::with_round_robin(
store,
ProxyConfig {
circuit_open_threshold: 1,
sticky_policy: sticky_config().sticky_policy,
..ProxyConfig::default()
},
)
.unwrap();
mgr.add_proxy(make_proxy("http://q1.test:8080"))
.await
.unwrap();
mgr.add_proxy(make_proxy("http://q2.test:8080"))
.await
.unwrap();
// First acquire: bind "cb.com" to a proxy.
let h1 = mgr.acquire_for_domain("cb.com").await.unwrap();
let url1 = h1.proxy_url.clone();
// Drop without mark_success → circuit breaker trips + session unbinds.
drop(h1);
// Give the tokio runtime a moment to process.
tokio::task::yield_now().await;
// The tripped proxy is no longer available; next acquire should succeed
// from the remaining healthy proxy (or error if only one).
// We just verify no panic and the handle is valid.
let _h2 = mgr.acquire_for_domain("cb.com").await;
// url may differ from url1 or error if all CBs open — either is acceptable.
let _ = url1;
}
/// `purge_expired()` removes stale sessions from the map.
#[tokio::test]
async fn sticky_purge_expired() {
use crate::session::StickyPolicy;
let store = storage();
let mgr = ProxyManager::with_round_robin(
store,
ProxyConfig {
sticky_policy: StickyPolicy::domain(Duration::from_millis(1)),
..ProxyConfig::default()
},
)
.unwrap();
mgr.add_proxy(make_proxy("http://r.test:8080"))
.await
.unwrap();
let h = mgr.acquire_for_domain("purge.com").await.unwrap();
h.mark_success();
assert_eq!(mgr.sessions.active_count(), 1);
// Expire and purge.
tokio::time::sleep(Duration::from_millis(5)).await;
let _ = mgr.sessions.purge_expired();
assert_eq!(mgr.sessions.active_count(), 0);
}
/// `pool_stats` includes `active_sessions`.
#[tokio::test]
async fn pool_stats_includes_sessions() {
let store = storage();
let mgr = ProxyManager::with_round_robin(store, sticky_config()).unwrap();
mgr.add_proxy(make_proxy("http://s.test:8080"))
.await
.unwrap();
let stats = mgr.pool_stats().await.unwrap();
assert_eq!(stats.active_sessions, 0);
let h = mgr.acquire_for_domain("stats.com").await.unwrap();
h.mark_success();
let stats = mgr.pool_stats().await.unwrap();
assert_eq!(stats.active_sessions, 1);
}
// ── Thompson sampling integration tests (T96) ───────────────────────────
/// `with_thompson_sampling` builds a manager whose strategy and
/// observer are wired together. After marking some handles successful
/// and dropping others, the strategy's Beta state should reflect the
/// observed outcomes.
#[cfg(feature = "bayesian-rotation")]
#[tokio::test]
async fn thompson_observer_records_outcomes_through_manager() {
use crate::strategy::{BayesianObserver, ThompsonStrategy};
use std::time::Duration;
let store = storage();
let strategy = Arc::new(ThompsonStrategy::with_decay(Duration::from_hours(1), 0.99));
let observer: BoxedBayesianObserver = Arc::clone(&strategy) as BoxedBayesianObserver;
// We need to know which proxy IDs to query after the manager has
// assigned them. Use the storage's `list()` for that.
let mgr = ProxyManager::builder()
.storage(store.clone())
.strategy(Arc::clone(&strategy) as BoxedRotationStrategy)
.observer(observer)
.config(ProxyConfig::default())
.build()
.unwrap();
mgr.add_proxy(make_proxy("http://alpha.test:8080"))
.await
.unwrap();
mgr.add_proxy(make_proxy("http://beta.test:8080"))
.await
.unwrap();
let records = store.list().await.unwrap();
let mut by_url: std::collections::HashMap<String, Uuid> = std::collections::HashMap::new();
for r in &records {
by_url.insert(r.proxy.url.clone(), r.id);
}
let alpha_id = *by_url
.get("http://alpha.test:8080")
.expect("alpha proxy should be in storage");
let beta_id = *by_url
.get("http://beta.test:8080")
.expect("beta proxy should be in storage");
// Mark alpha 8 times successful, beta 8 times failed.
for _ in 0..8 {
// Force a specific proxy by acquiring repeatedly (round-robin
// over two proxies) and selectively marking the right handle.
// The simpler approach: directly call the observer (which is
// what mark_success / drop do internally).
strategy.observe(alpha_id, true);
strategy.observe(beta_id, false);
}
let (alpha_succ, alpha_fail) = strategy.counts_for(alpha_id);
let (beta_succ, beta_fail) = strategy.counts_for(beta_id);
assert!(
alpha_succ >= 8,
"alpha should have many successes (got {alpha_succ})"
);
assert!(
alpha_fail < 5,
"alpha should have few failures (got {alpha_fail})"
);
assert!(
beta_succ < 5,
"beta should have few successes (got {beta_succ})"
);
assert!(
beta_fail >= 8,
"beta should have many failures (got {beta_fail})"
);
}
/// Thompson sampling manager survives 1 000 acquire+`mark_success`
/// round-trips in well under 1 s (the hot-path budget). The observer
/// is wired in via `with_thompson_sampling` so this is a full
/// end-to-end timing test.
#[cfg(feature = "bayesian-rotation")]
#[tokio::test]
async fn thompson_manager_hot_path_budget() {
use std::time::Duration;
let store = storage();
let mgr = ProxyManager::with_thompson_sampling(
store.clone(),
ProxyConfig::default(),
Duration::from_hours(1),
)
.unwrap();
mgr.add_proxy(make_proxy("http://p1.test:8080"))
.await
.unwrap();
mgr.add_proxy(make_proxy("http://p2.test:8080"))
.await
.unwrap();
mgr.add_proxy(make_proxy("http://p3.test:8080"))
.await
.unwrap();
// Warm up
for _ in 0..10 {
let h = mgr.acquire_proxy().await.unwrap();
h.mark_success();
}
let start = std::time::Instant::now();
for _ in 0..1_000 {
let h = mgr.acquire_proxy().await.unwrap();
h.mark_success();
}
let elapsed = start.elapsed();
assert!(
elapsed < std::time::Duration::from_secs(1),
"1 000 Thompson manager round-trips took {elapsed:?}; hot-path budget violated"
);
}
/// On a poisoned pool (50/50 alive/dead), Thompson sampling should
/// route the overwhelming majority of traffic to the alive proxies
/// after a brief warm-up — same shape as the unit-level
/// `synthetic_poisoned_pool_concentrates_traffic_on_alive_proxies`
/// test but driven through the full manager.
#[cfg(feature = "bayesian-rotation")]
#[tokio::test]
async fn thompson_outperforms_round_robin_on_poisoned_pool() {
use std::time::Duration;
// Each manager owns its own storage so the two pools are
// independent (round-robin and Thompson otherwise race over the
// same proxy IDs).
let store_rr = storage();
let store_th = storage();
let mgr_rr = ProxyManager::with_round_robin(store_rr, ProxyConfig::default()).unwrap();
let mgr_th = ProxyManager::with_thompson_sampling(
store_th,
ProxyConfig::default(),
Duration::from_hours(1),
)
.unwrap();
// 5 alive + 5 dead proxies.
let mut alive_urls: Vec<String> = Vec::new();
let mut dead_urls: Vec<String> = Vec::new();
for i in 0..5 {
let url = format!("http://alive{i}.test:8080");
mgr_rr.add_proxy(make_proxy(&url)).await.unwrap();
mgr_th.add_proxy(make_proxy(&url)).await.unwrap();
alive_urls.push(url);
}
for i in 0..5 {
let url = format!("http://dead{i}.test:8080");
mgr_rr.add_proxy(make_proxy(&url)).await.unwrap();
mgr_th.add_proxy(make_proxy(&url)).await.unwrap();
dead_urls.push(url);
}
// Pre-warm the Thompson strategy: observe 5 successes for every
// alive URL and 5 failures for every dead URL. The strategy maps
// URLs to ids internally, so we use the warm-up helper.
let records = mgr_th.storage().list().await.unwrap();
for r in &records {
if alive_urls.iter().any(|u| u == &r.proxy.url) {
for _ in 0..5 {
mgr_th.strategy_warmup_observe(r.id, true);
}
} else if dead_urls.iter().any(|u| u == &r.proxy.url) {
for _ in 0..5 {
mgr_th.strategy_warmup_observe(r.id, false);
}
}
}
// Round-robin: 200 acquisitions. Mark alive-URL handles as
// successful (simulating healthy traffic); drop dead-URL handles
// without marking (simulating failed requests). Track which
// fraction of selections went to the dead subset.
let mut rr_alive = 0_u64;
let mut rr_dead = 0_u64;
for _ in 0..200 {
let h = mgr_rr.acquire_proxy().await.unwrap();
let url = h.proxy_url.clone();
if url.contains("alive") {
h.mark_success();
rr_alive += 1;
} else {
drop(h);
rr_dead += 1;
}
}
// Counts are bounded (≤ 200) so the `as f64` conversion is
// lossless — `f64`'s 53-bit mantissa comfortably represents
// every value the test ever sees.
#[allow(clippy::cast_precision_loss)]
let rr_dead_share = (rr_dead as f64) / ((rr_alive + rr_dead) as f64);
// Thompson: same 200-acquisition pattern. The warm-up means the
// bandit already knows the dead URLs are bad and should route
// almost all traffic to the alive set.
let mut th_alive = 0_u64;
let mut th_dead = 0_u64;
for _ in 0..200 {
let h = mgr_th.acquire_proxy().await.unwrap();
let url = h.proxy_url.clone();
if url.contains("alive") {
h.mark_success();
th_alive += 1;
} else {
drop(h);
th_dead += 1;
}
}
#[allow(clippy::cast_precision_loss)]
let th_dead_share = (th_dead as f64) / ((th_alive + th_dead) as f64);
// Thompson should route much less traffic to the dead proxies
// than round-robin does, capturing the "more than double the
// success rate" claim from the 2026 guide.
assert!(
th_dead_share < rr_dead_share,
"Thompson dead-share ({th_dead_share:.3}) should be less than round-robin ({rr_dead_share:.3})"
);
// And the relative improvement should exceed 50 %.
let improvement = (rr_dead_share - th_dead_share) / rr_dead_share;
assert!(
improvement > 0.50,
"expected >50% relative improvement in dead-share reduction (got {:.1}%)",
improvement * 100.0
);
}
// ── Coherence integration tests (T97) ────────────────────────────────
/// Helper: build a clean US context for the coherence integration
/// tests. Mirrors the helper used by the adapter unit tests so the
/// matrix stays aligned with the spec.
#[cfg(feature = "coherence-validation")]
fn clean_us_context() -> crate::ports::coherence::CoherenceContext {
use crate::ports::coherence::{AcceptLanguage, CoherenceContext, IsoCountry, Locale, Tz};
use std::net::IpAddr;
use std::str::FromStr;
CoherenceContext {
proxy_geo_country: Some(IsoCountry::new("US").unwrap()),
dns_resolver_country: Some(IsoCountry::new("US").unwrap()),
browser_locale: Locale::new("en-US").unwrap(),
browser_timezone: Tz::new("America/New_York").unwrap(),
accept_language: AcceptLanguage::new("en-US,en;q=0.9").unwrap(),
webrtc_local_ip: None,
webrtc_public_ip: Some(IpAddr::from_str("192.0.2.42").unwrap()),
proxy_ip: Some(IpAddr::from_str("192.0.2.7").unwrap()),
}
}
/// `acquire_proxy_with_coherence` on a clean US context returns the
/// same proxy as `acquire_proxy` would — the validator says
/// `Coherent` and the manager does not block the request.
#[cfg(feature = "coherence-validation")]
#[tokio::test]
async fn acquire_with_coherence_coherent_returns_proxy() {
let store = storage();
let mgr = ProxyManager::with_round_robin(store, ProxyConfig::default()).unwrap();
mgr.add_proxy(make_proxy("http://a.test:8080"))
.await
.unwrap();
let ctx = clean_us_context();
let policy = crate::ports::coherence::CoherencePolicy::advisory();
let handle = mgr
.acquire_proxy_with_coherence(&ctx, &policy)
.await
.unwrap();
assert_eq!(handle.proxy_url, "http://a.test:8080");
handle.mark_success();
}
/// Mismatch on a `Hard` field registered for hard-fail produces
/// [`ProxyError::CoherenceMismatch`].
#[cfg(feature = "coherence-validation")]
#[tokio::test]
async fn acquire_with_coherence_hard_fail_returns_error() {
use crate::ports::coherence::{
AcceptLanguage, CoherenceContext, IsoCountry, Locale, MismatchField, MismatchSeverity,
Tz,
};
let store = storage();
let mgr = ProxyManager::with_round_robin(store, ProxyConfig::default()).unwrap();
mgr.add_proxy(make_proxy("http://a.test:8080"))
.await
.unwrap();
// PK DNS forces a Hard mismatch on ProxyGeoVsDns.
let ctx = CoherenceContext {
dns_resolver_country: Some(IsoCountry::new("PK").unwrap()),
..clean_us_context()
};
let policy =
crate::ports::coherence::CoherencePolicy::hard_fail_on(MismatchField::ProxyGeoVsDns);
let err = mgr
.acquire_proxy_with_coherence(&ctx, &policy)
.await
.unwrap_err();
match err {
crate::error::ProxyError::CoherenceMismatch { field, severity } => {
assert_eq!(field, MismatchField::ProxyGeoVsDns);
assert_eq!(severity, MismatchSeverity::Hard);
}
other => panic!("expected CoherenceMismatch, got {other:?}"),
}
// Suppress unused-import warnings on the Local variants that
// are referenced by name only in `match` arms above.
let _ = Locale::new("en-US").unwrap();
let _ = AcceptLanguage::new("en-US").unwrap();
let _ = Tz::new("America/New_York").unwrap();
}
/// An Advisory mismatch (Europe/London TZ with a US proxy) does
/// **not** fail the acquisition — the proxy is returned and the
/// mismatch is logged.
#[cfg(feature = "coherence-validation")]
#[tokio::test]
async fn acquire_with_coherence_advisory_mismatch_logs_and_returns_proxy() {
use crate::ports::coherence::{CoherenceContext, Tz};
let store = storage();
let mgr = ProxyManager::with_round_robin(store, ProxyConfig::default()).unwrap();
mgr.add_proxy(make_proxy("http://a.test:8080"))
.await
.unwrap();
let ctx = CoherenceContext {
browser_timezone: Tz::new("Europe/London").unwrap(),
..clean_us_context()
};
// Default advisory policy: nothing is hard-failed.
let policy = crate::ports::coherence::CoherencePolicy::advisory();
let handle = mgr
.acquire_proxy_with_coherence(&ctx, &policy)
.await
.unwrap();
assert_eq!(handle.proxy_url, "http://a.test:8080");
handle.mark_success();
}
/// Custom validator wiring: the builder's `coherence_validator`
/// step replaces the default and is consulted on every
/// `acquire_proxy_with_coherence` call. The test uses an
/// always-Coherent stub so the proxy is returned even when the
/// spec test fixtures (timezone / DNS) would otherwise disagree.
#[cfg(feature = "coherence-validation")]
#[tokio::test]
async fn acquire_with_coherence_custom_validator_is_wired() {
use crate::ports::coherence::{
BoxedCoherencePort, CoherenceContext, CoherencePort, CoherenceVerdict,
};
#[derive(Debug)]
struct AlwaysCoherent;
impl CoherencePort for AlwaysCoherent {
fn evaluate(&self, _: &CoherenceContext) -> CoherenceVerdict {
CoherenceVerdict::Coherent
}
}
let store = storage();
let mgr = ProxyManager::builder()
.storage(store)
.coherence_validator(std::sync::Arc::new(AlwaysCoherent) as BoxedCoherencePort)
.build()
.unwrap();
mgr.add_proxy(make_proxy("http://a.test:8080"))
.await
.unwrap();
// Any context — the stub says Coherent regardless.
let ctx = clean_us_context();
let policy = crate::ports::coherence::CoherencePolicy::advisory();
let handle = mgr
.acquire_proxy_with_coherence(&ctx, &policy)
.await
.unwrap();
assert_eq!(handle.proxy_url, "http://a.test:8080");
handle.mark_success();
}
/// 1 000 sequential `acquire_proxy_with_coherence` calls stay under
/// the 1 s hot-path budget. The validator is O(1) + stateless, so
/// the integration is just as fast as the plain `acquire_proxy`
/// budget test from T95.
#[cfg(feature = "coherence-validation")]
#[tokio::test]
async fn acquire_with_coherence_hot_path_budget() {
let store = storage();
let mgr = ProxyManager::with_round_robin(store, ProxyConfig::default()).unwrap();
mgr.add_proxy(make_proxy("http://a.test:8080"))
.await
.unwrap();
mgr.add_proxy(make_proxy("http://b.test:8080"))
.await
.unwrap();
mgr.add_proxy(make_proxy("http://c.test:8080"))
.await
.unwrap();
let ctx = clean_us_context();
let policy = crate::ports::coherence::CoherencePolicy::advisory();
let start = std::time::Instant::now();
for _ in 0..1_000 {
let h = mgr
.acquire_proxy_with_coherence(&ctx, &policy)
.await
.unwrap();
h.mark_success();
}
let elapsed = start.elapsed();
assert!(
elapsed < std::time::Duration::from_secs(1),
"1000 coherence-gated acquisitions took {elapsed:?}; hot-path budget violated"
);
}
// ── T99: per-vendor stickiness integration tests ───────────────────────
/// Two consecutive `acquire_for_domain_with_vendor` calls for an
/// `Akamai` target return the same proxy (sticky 30 min per the
/// 2026 guide built-in default).
#[cfg(feature = "vendor-stickiness")]
#[tokio::test]
async fn acquire_with_vendor_akamai_is_sticky() {
let store = storage();
let mgr = ProxyManager::with_round_robin(store, ProxyConfig::default()).unwrap();
mgr.add_proxy(make_proxy("http://p1.test:8080"))
.await
.unwrap();
mgr.add_proxy(make_proxy("http://p2.test:8080"))
.await
.unwrap();
let h1 = mgr
.acquire_for_domain_with_vendor("example.com", crate::types::VendorId::Akamai)
.await
.unwrap();
let url1 = h1.proxy_url.clone();
h1.mark_success();
let h2 = mgr
.acquire_for_domain_with_vendor("example.com", crate::types::VendorId::Akamai)
.await
.unwrap();
let url2 = h2.proxy_url.clone();
h2.mark_success();
assert_eq!(
url1, url2,
"Akamai sticky policy should return the same proxy across calls"
);
}
/// `DataDome` is `FreshPerRequest` per the 2026 guide. Each call
/// should pick a fresh proxy via the rotation strategy.
#[cfg(feature = "vendor-stickiness")]
#[tokio::test]
async fn acquire_with_vendor_data_dome_is_fresh_per_request() {
let store = storage();
let mgr = ProxyManager::with_round_robin(store, ProxyConfig::default()).unwrap();
mgr.add_proxy(make_proxy("http://p1.test:8080"))
.await
.unwrap();
mgr.add_proxy(make_proxy("http://p2.test:8080"))
.await
.unwrap();
let h1 = mgr
.acquire_for_domain_with_vendor("example.com", crate::types::VendorId::DataDome)
.await
.unwrap();
let url1 = h1.proxy_url.clone();
h1.mark_success();
let h2 = mgr
.acquire_for_domain_with_vendor("example.com", crate::types::VendorId::DataDome)
.await
.unwrap();
let url2 = h2.proxy_url.clone();
h2.mark_success();
assert_ne!(
url1, url2,
"DataDome fresh-per-request policy should yield different proxies"
);
}
/// `PerimeterX` is `FreshPerDomain` per the 2026 guide. Each call
/// should pick a fresh proxy and evict any prior binding.
#[cfg(feature = "vendor-stickiness")]
#[tokio::test]
async fn acquire_with_vendor_perimeter_x_is_fresh_per_domain() {
let store = storage();
let mgr = ProxyManager::with_round_robin(store, ProxyConfig::default()).unwrap();
mgr.add_proxy(make_proxy("http://p1.test:8080"))
.await
.unwrap();
mgr.add_proxy(make_proxy("http://p2.test:8080"))
.await
.unwrap();
let h1 = mgr
.acquire_for_domain_with_vendor("example.com", crate::types::VendorId::PerimeterX)
.await
.unwrap();
let url1 = h1.proxy_url.clone();
h1.mark_success();
let h2 = mgr
.acquire_for_domain_with_vendor("example.com", crate::types::VendorId::PerimeterX)
.await
.unwrap();
let url2 = h2.proxy_url.clone();
h2.mark_success();
assert_ne!(
url1, url2,
"PerimeterX fresh-per-domain policy should yield different proxies"
);
}
/// Unknown vendors default to `FreshPerRequest` — every call
/// picks a fresh proxy.
#[cfg(feature = "vendor-stickiness")]
#[tokio::test]
async fn acquire_with_vendor_unknown_defaults_to_fresh() {
let store = storage();
let mgr = ProxyManager::with_round_robin(store, ProxyConfig::default()).unwrap();
mgr.add_proxy(make_proxy("http://p1.test:8080"))
.await
.unwrap();
mgr.add_proxy(make_proxy("http://p2.test:8080"))
.await
.unwrap();
let h1 = mgr
.acquire_for_domain_with_vendor("example.com", crate::types::VendorId::Unknown)
.await
.unwrap();
let url1 = h1.proxy_url.clone();
h1.mark_success();
let h2 = mgr
.acquire_for_domain_with_vendor("example.com", crate::types::VendorId::Unknown)
.await
.unwrap();
let url2 = h2.proxy_url.clone();
h2.mark_success();
assert_ne!(url1, url2, "Unknown vendor should default to fresh");
}
/// After the bound proxy's circuit breaker trips the sticky
/// binding is invalidated and a fresh proxy is acquired on the
/// next call.
#[cfg(feature = "vendor-stickiness")]
#[tokio::test]
async fn acquire_with_vendor_sticky_binding_reacquires_after_failure() {
let store = storage();
let mgr = ProxyManager::with_round_robin(
store,
ProxyConfig {
circuit_open_threshold: 1,
..ProxyConfig::default()
},
)
.unwrap();
mgr.add_proxy(make_proxy("http://p1.test:8080"))
.await
.unwrap();
mgr.add_proxy(make_proxy("http://p2.test:8080"))
.await
.unwrap();
// First call: bind a proxy on `Akamai`. Drop without
// `mark_success` so the CB trips and the binding is unbound.
let h1 = mgr
.acquire_for_domain_with_vendor("stale.com", crate::types::VendorId::Akamai)
.await
.unwrap();
drop(h1);
tokio::task::yield_now().await;
// Second call: CB is now tripped, manager must invalidate the
// binding and pick a fresh proxy. Either a different URL or an
// `AllProxiesUnhealthy` error is acceptable.
let result = mgr
.acquire_for_domain_with_vendor("stale.com", crate::types::VendorId::Akamai)
.await;
match result {
Ok(_h) => {} // manager recovered with a fresh proxy
Err(crate::error::ProxyError::AllProxiesUnhealthy) => {} // pool exhausted
Err(e) => panic!("unexpected error after stale binding: {e:?}"),
}
}
/// `acquire_for_domain_with_vendor` honours a custom override
/// installed via `ProxyManagerBuilder::stickiness_map`.
#[cfg(feature = "vendor-stickiness")]
#[tokio::test]
async fn builder_stickiness_map_override_replaces_builtins() {
use crate::stickiness::StickinessPolicy;
let store = storage();
// Override `Akamai` to `StickyForever` and `DataDome` to
// `StickyForTtl 60s`.
let custom = crate::stickiness::VendorStickinessMap::new()
.with_override(
crate::types::VendorId::Akamai,
StickinessPolicy::StickyForever,
)
.with_override(
crate::types::VendorId::DataDome,
StickinessPolicy::StickyForTtl {
ttl: Duration::from_mins(1),
},
);
let mgr = ProxyManager::builder()
.storage(store)
.config(ProxyConfig::default())
.stickiness_map(custom)
.build()
.unwrap();
mgr.add_proxy(make_proxy("http://p1.test:8080"))
.await
.unwrap();
mgr.add_proxy(make_proxy("http://p2.test:8080"))
.await
.unwrap();
// Akamai: StickyForever — same proxy across two calls.
let h1 = mgr
.acquire_for_domain_with_vendor("example.com", crate::types::VendorId::Akamai)
.await
.unwrap();
let url1 = h1.proxy_url.clone();
h1.mark_success();
let h2 = mgr
.acquire_for_domain_with_vendor("example.com", crate::types::VendorId::Akamai)
.await
.unwrap();
let url2 = h2.proxy_url.clone();
h2.mark_success();
assert_eq!(
url1, url2,
"custom StickyForever should keep the same proxy"
);
// DataDome: StickyForTtl 1 min — same proxy across two calls
// because we made them within the TTL window.
let h1 = mgr
.acquire_for_domain_with_vendor("dd.com", crate::types::VendorId::DataDome)
.await
.unwrap();
let url1 = h1.proxy_url.clone();
h1.mark_success();
let h2 = mgr
.acquire_for_domain_with_vendor("dd.com", crate::types::VendorId::DataDome)
.await
.unwrap();
let url2 = h2.proxy_url.clone();
h2.mark_success();
assert_eq!(url1, url2, "custom StickyForTtl should keep the same proxy");
// Hcaptcha (no override, no built-in) defaults to FreshPerRequest.
let h1 = mgr
.acquire_for_domain_with_vendor("hc.com", crate::types::VendorId::Hcaptcha)
.await
.unwrap();
let url1 = h1.proxy_url.clone();
h1.mark_success();
let h2 = mgr
.acquire_for_domain_with_vendor("hc.com", crate::types::VendorId::Hcaptcha)
.await
.unwrap();
let url2 = h2.proxy_url.clone();
h2.mark_success();
assert_ne!(
url1, url2,
"Hcaptcha should default to fresh when no override is installed"
);
}
/// 1 000 sequential `acquire_for_domain_with_vendor` calls stay
/// under the 1 s hot-path budget. The per-vendor policy lookup is
/// a `BTreeMap::get` (O(log n)) so the integration adds no
/// measurable overhead vs `acquire_for_domain`.
#[cfg(feature = "vendor-stickiness")]
#[tokio::test]
async fn acquire_with_vendor_hot_path_budget() {
let store = storage();
let mgr = ProxyManager::with_round_robin(store, ProxyConfig::default()).unwrap();
mgr.add_proxy(make_proxy("http://a.test:8080"))
.await
.unwrap();
mgr.add_proxy(make_proxy("http://b.test:8080"))
.await
.unwrap();
mgr.add_proxy(make_proxy("http://c.test:8080"))
.await
.unwrap();
let start = std::time::Instant::now();
for _ in 0..1_000 {
let h = mgr
.acquire_for_domain_with_vendor("example.com", crate::types::VendorId::Akamai)
.await
.unwrap();
h.mark_success();
}
let elapsed = start.elapsed();
assert!(
elapsed < std::time::Duration::from_secs(1),
"1000 per-vendor acquisitions took {elapsed:?}; hot-path budget violated"
);
}
// ── T98: add_proxy_with_metadata ──────────────────────────────────────
/// `add_proxy_with_metadata` constructs and stores a proxy with
/// `asn`, `city`, and `postal_code` populated.
#[tokio::test]
async fn add_proxy_with_metadata_stores_geo_fields() -> crate::error::ProxyResult<()> {
let store = storage();
let mgr = ProxyManager::with_round_robin(store.clone(), ProxyConfig::default())?;
mgr.add_proxy_with_metadata(
"http://cf-sf.test:8080",
Some(13_335),
Some("San Francisco"),
Some("94110"),
)
.await?;
let records = store.list().await?;
assert_eq!(records.len(), 1);
let record = records.first().expect("one record");
assert_eq!(record.proxy.capabilities.asn, Some(13_335));
assert_eq!(
record.proxy.capabilities.city.as_deref(),
Some("San Francisco")
);
assert_eq!(
record.proxy.capabilities.postal_code.as_deref(),
Some("94110")
);
Ok(())
}
/// `add_proxy_with_metadata` rejects malformed values via the
/// same path as `add_proxy` (no special-cased API).
#[tokio::test]
async fn add_proxy_with_metadata_rejects_invalid_geo() {
let store = storage();
let mgr = ProxyManager::with_round_robin(store.clone(), ProxyConfig::default()).unwrap();
let err = mgr
.add_proxy_with_metadata(
"http://cf-sf.test:8080",
Some(0), // reserved
Some("San Francisco"),
Some("94110"),
)
.await
.expect_err("asn=0 must be rejected");
assert!(matches!(
err,
crate::error::ProxyError::InvalidGeoMetadata { ref field, .. } if field == "asn"
));
let err = mgr
.add_proxy_with_metadata(
"http://cf-sf.test:8080",
Some(13_335),
Some(""), // empty
Some("94110"),
)
.await
.expect_err("empty city must be rejected");
assert!(matches!(
err,
crate::error::ProxyError::InvalidGeoMetadata { ref field, .. } if field == "city"
));
}
/// Round-trip: a proxy added via `add_proxy_with_metadata`
/// satisfies a `require_asn` capability filter.
#[tokio::test]
async fn add_proxy_with_metadata_round_trips_capability_filter() -> crate::error::ProxyResult<()>
{
let store = storage();
let mgr = ProxyManager::with_round_robin(store.clone(), ProxyConfig::default())?;
mgr.add_proxy_with_metadata(
"http://cf-sf.test:8080",
Some(13_335),
Some("San Francisco"),
Some("94110"),
)
.await?;
let req = crate::types::CapabilityRequirement {
require_asn: Some(13_335),
..Default::default()
};
let handle = mgr.acquire_with_capabilities(&req).await?;
assert!(
handle.proxy_url.contains("cf-sf.test"),
"got url: {}",
handle.proxy_url
);
handle.mark_success();
Ok(())
}
}