ruststream-fred 0.7.1

Redis / Valkey broker implementation (Streams) for the RustStream messaging framework, backed by fred.
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
//! The broker ladder: [`RedisBroker`] -> [`ConnectedRedisBroker`] -> [`ClosedRedisBroker`].
//!
//! Construction is synchronous and I/O-free: the named constructors only record the topology and
//! its options. All network work happens in the consuming [`Broker::connect`], and the connected
//! form owns the live `fred` pool, so subscriptions and publishers exist only once a connection
//! does.

use std::num::NonZeroUsize;
use std::sync::Arc;
#[cfg(feature = "testing")]
use std::sync::Mutex;

use fred::clients::{Client, Pool};
use fred::interfaces::{ClientLike, EventInterface, PubsubInterface, StreamsInterface};
#[cfg(feature = "credential-provider")]
use fred::types::config::CredentialProvider;
#[cfg(any(
    feature = "tls-rustls",
    feature = "tls-rustls-ring",
    feature = "tls-native-tls"
))]
use fred::types::config::TlsConfig;
use fred::types::config::{Config, PerformanceConfig, ReconnectPolicy, ServerConfig};
use fred::types::{ClientState, ClusterHash, CustomCommand, Message, Value, Version};
use fred::util::redis_keyslot;
#[cfg(feature = "testing")]
use ruststream::testing::{Coordinator, InProcess, TestableBroker};
use ruststream::{AddressedCopies, Broker, ConnectedBroker, DescribeServer, ServerSpec, Subscribe};
#[cfg(feature = "testing")]
use ruststream::{OutgoingMessage, RawMessage};
use tokio::runtime::Handle;
use tokio::sync::broadcast::Receiver;

use crate::address::{Address, redacted};
use crate::connection::{Closing, Connection, ReadConnection};
#[cfg(feature = "testing")]
use crate::convert::{fields_for_publish, parts_from_fields};
use crate::delay::DelayConfig;
#[cfg(feature = "testing")]
use crate::envelope::{frame, unframe};
#[cfg(feature = "testing")]
use crate::in_process::{self, Subscription, Write};
use crate::loopback::{Loopback, Tap};
use crate::recovery::RecoveryConfig;

use crate::{
    error::RedisError,
    list::{ListWire, RedisList, RedisListPublish, RedisListPublisher, RedisListSubscriber},
    pipeline::{Owner, Rounds},
    publisher::{RedisDefaultPublisher, RedisPublisher},
    pubsub::{
        PubSubMode, PubSubWire, RedisPubSub, RedisPubSubPattern, RedisPubSubPublish,
        RedisPubSubPublisher, RedisPubSubSubscriber, Resubscribe, Resubscribing,
    },
    route::{Recorded, Route, Routes},
    stream::{ReadMode, RedisStream},
    subscriber::RedisSubscriber,
};

/// Default `fred` connection-pool size when the caller does not set one.
const DEFAULT_POOL_SIZE: usize = 4;

/// How the connections this broker opens reconnect after the server drops them: a first attempt
/// after 100 ms, each next one twice as late, at most 10 s apart, and no limit on the attempts.
///
/// A service outlives a restart or a failover of its server, however long it takes; the quick
/// first attempt covers a dropped socket, the cap keeps a long outage from becoming a flood of
/// dials. Commands issued while the connection is down wait for it in `fred`'s queue.
fn reconnect_policy() -> ReconnectPolicy {
    ReconnectPolicy::new_exponential(0, 100, 10_000, 2)
}

/// The first Redis release whose `XREADGROUP` takes a `CLAIM` option.
const CLAIM_MIN_VERSION: Version = Version::new(8, 4, 0);

/// How the broker should connect, recorded synchronously and resolved into a `fred` config at
/// [`Broker::connect`] time so construction stays I/O- and failure-free.
#[derive(Debug, Clone)]
enum Topology {
    /// A single server, addressed by URL (`redis://host:port`).
    Standalone(Address),
    /// A Redis Cluster, addressed by one or more `host:port` seed nodes.
    Cluster(Vec<Address>),
    /// Sentinel-managed replication: the monitored primary's `service` name plus the `host:port`
    /// of each sentinel.
    Sentinel {
        service: String,
        hosts: Vec<Address>,
    },
    /// A pool the service built itself, supplied via [`RedisBroker::from_pool`]; `connect`
    /// adopts it, dialing it first when the service has not.
    Preconnected(Pool),
}

/// Parses a `host:port` address (tolerating a `redis://` / `rediss://` scheme prefix) into the
/// `(host, port)` pair `fred`'s server-config constructors expect. Falls back to `default_port`
/// when no port is given.
fn parse_server(addr: &str, default_port: u16) -> Result<(String, u16), RedisError> {
    let trimmed = addr
        .trim()
        .trim_start_matches("rediss://")
        .trim_start_matches("redis://");
    let (host, port) = match trimmed.rsplit_once(':') {
        Some((host, port)) => {
            let port = port.parse::<u16>().map_err(|_| {
                let addr = redacted(addr);
                RedisError::Connect(format!("invalid port in redis address `{addr}`").into())
            })?;
            (host, port)
        }
        None => (trimmed, default_port),
    };
    if host.is_empty() {
        return Err(RedisError::Connect(
            format!("missing host in redis address `{}`", redacted(addr)).into(),
        ));
    }
    Ok((host.to_owned(), port))
}

fn parse_servers(addrs: &[Address], default_port: u16) -> Result<Vec<(String, u16)>, RedisError> {
    if addrs.is_empty() {
        return Err(RedisError::Connect("no redis addresses provided".into()));
    }
    addrs
        .iter()
        .map(|addr| parse_server(addr, default_port))
        .collect()
}

/// Authentication and TLS settings recorded on the broker and folded into the `fred` [`Config`]
/// at connect time, on every topology. Fields with no value are left untouched, so credentials
/// supplied through a standalone `redis://user:pass@host` URL survive unless overridden here.
#[derive(Clone, Default)]
struct AuthConfig {
    /// ACL username for the data nodes (`Config.username`).
    username: Option<String>,
    /// Password for the data nodes (`Config.password`).
    password: Option<String>,
    /// ACL username for authenticating to the sentinels, distinct from the data-node username.
    #[cfg(feature = "sentinel-auth")]
    sentinel_username: Option<String>,
    /// Password for authenticating to the sentinels, distinct from the data-node password.
    #[cfg(feature = "sentinel-auth")]
    sentinel_password: Option<String>,
    /// Explicit TLS configuration (`Config.tls`).
    #[cfg(any(
        feature = "tls-rustls",
        feature = "tls-rustls-ring",
        feature = "tls-native-tls"
    ))]
    tls: Option<TlsConfig>,
    /// Dynamic/rotating credential provider (`Config.credential_provider`).
    #[cfg(feature = "credential-provider")]
    credential_provider: Option<Arc<dyn CredentialProvider>>,
}

// Redacts secrets: passwords never appear, and TLS / credential-provider show only presence. The
// usernames are identifiers (not secrets) and are kept to aid debugging.
impl std::fmt::Debug for AuthConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut s = f.debug_struct("AuthConfig");
        s.field("username", &self.username);
        s.field("password", &self.password.as_ref().map(|_| "<redacted>"));
        #[cfg(feature = "sentinel-auth")]
        {
            s.field("sentinel_username", &self.sentinel_username);
            s.field(
                "sentinel_password",
                &self.sentinel_password.as_ref().map(|_| "<redacted>"),
            );
        }
        #[cfg(any(
            feature = "tls-rustls",
            feature = "tls-rustls-ring",
            feature = "tls-native-tls"
        ))]
        s.field("tls", &self.tls.as_ref().map(|_| "<configured>"));
        #[cfg(feature = "credential-provider")]
        s.field(
            "credential_provider",
            &self.credential_provider.as_ref().map(|_| "<configured>"),
        );
        s.finish()
    }
}

/// An unconnected Redis broker: the recorded topology and its options, no I/O performed yet.
///
/// Build it with [`standalone`](Self::standalone), [`cluster`](Self::cluster),
/// [`sentinel`](Self::sentinel), or [`from_pool`](Self::from_pool). All four are synchronous and
/// perform no I/O, so a Redis service composes with the synchronous `#[ruststream::app]` builder;
/// the runtime dials once at startup through the consuming [`Broker::connect`], which yields the
/// [`ConnectedRedisBroker`] every subscription and publisher is reached from.
///
/// # Examples
///
/// ```
/// # mod demo {
/// use ruststream_fred::stream::prelude::*;
/// use serde::Deserialize;
///
/// #[derive(Deserialize)]
/// struct Order {
///     id: u64,
/// }
///
/// #[subscriber(RedisStream::new("orders").group("workers"))]
/// async fn handle(order: &Order) -> HandlerOutcome {
///     tracing::info!(order.id, "order");
///     HandlerOutcome::ack()
/// }
///
/// // The broker is a plain value here; the runtime connects it at startup and shuts it down
/// // when the service stops.
/// #[ruststream::app]
/// fn app() -> impl App {
///     RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(
///         RedisBroker::standalone("redis://localhost:6379"),
///         |b| {
///             b.include(handle);
///         },
///     )
/// }
/// # }
/// # fn main() {}
/// ```
///
/// # Testing
///
/// A test hands [`TestApp`](https://docs.rs/ruststream/latest/ruststream/testing/struct.TestApp.html) the app `main` runs and addresses the
/// broker by this type; the crate's `testing` feature in `[dev-dependencies]` connects it to a
/// Redis modelled in process.
///
/// ```
/// # use std::error::Error;
/// # #[cfg(feature = "testing")]
/// # mod demo {
/// use std::error::Error;
///
/// use ruststream::testing::TestApp;
/// use ruststream_fred::stream::prelude::*;
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Debug, Deserialize, Outgoing, PartialEq, Serialize)]
/// struct Payment {
///     id: u64,
///     amount: u64,
/// }
///
/// #[subscriber(RedisStream::new("payments").group("workers"))]
/// async fn process(payment: &Payment) -> HandlerOutcome {
///     if payment.amount == 0 {
///         return HandlerOutcome::drop();
///     }
///     HandlerOutcome::ack()
/// }
///
/// /// The app `main` runs, and the one the tests hand the harness.
/// pub fn app() -> impl App<State = ()> {
///     RustStream::new(AppInfo::new("payments", "0.1.0"))
///         .with_broker(RedisBroker::standalone("redis://localhost:6379"), |b| {
///             b.include(process);
///         })
/// }
///
/// pub async fn a_paid_payment_is_acknowledged() -> Result<(), Box<dyn Error>> {
///     // The broker connects in process: nothing dials the address, and a publish is driven to
///     // quiescence, so the assertion after it needs no waiting.
///     let tb = TestApp::start(app()).await?;
///     let payment = Payment { id: 1, amount: 100 };
///     tb.broker::<RedisBroker>()
///         .message(&payment)
///         .to("payments")
///         .publish()
///         .await?;
///
///     tb.broker::<RedisBroker>()
///         .subscriber("payments")
///         .assert_called_once()
///         .with(&payment)
///         .settled(HandlerOutcome::ack());
///     Ok(())
/// }
/// # }
/// # #[cfg(feature = "testing")]
/// # fn main() -> Result<(), Box<dyn Error>> {
/// #     tokio::runtime::Runtime::new()?.block_on(demo::a_paid_payment_is_acknowledged())
/// # }
/// # #[cfg(not(feature = "testing"))]
/// # fn main() {}
/// ```
#[derive(Debug, Clone)]
#[must_use]
pub struct RedisBroker {
    topology: Topology,
    pool_size: usize,
    default_group: Option<String>,
    auth: AuthConfig,
}

impl RedisBroker {
    /// Creates a standalone-topology broker that connects to `url` when [`Broker::connect`] runs.
    ///
    /// The broker's `Debug` output shows the URL with its password, and any `sentinelPassword`
    /// parameter, as `***`; the seed addresses of [`cluster`](Self::cluster) and
    /// [`sentinel`](Self::sentinel) print the same way.
    pub fn standalone(url: impl Into<String>) -> Self {
        Self::with_topology(Topology::Standalone(Address::new(url.into())))
    }

    /// Creates a Redis Cluster broker from one or more `host:port` seed nodes.
    ///
    /// Only one reachable node is needed; `fred` discovers the rest of the cluster on connect.
    pub fn cluster(nodes: impl IntoIterator<Item = impl Into<String>>) -> Self {
        Self::with_topology(Topology::Cluster(
            nodes
                .into_iter()
                .map(|node| Address::new(node.into()))
                .collect(),
        ))
    }

    /// Creates a Sentinel-backed broker that tracks the primary named `service`, discovering it
    /// through the given sentinel `host:port` addresses.
    pub fn sentinel(
        service: impl Into<String>,
        sentinels: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        Self::with_topology(Topology::Sentinel {
            service: service.into(),
            hosts: sentinels
                .into_iter()
                .map(|host| Address::new(host.into()))
                .collect(),
        })
    }

    /// Wraps a `fred` pool the service built itself, for a setting the other constructors do not
    /// reach (a reconnection policy, performance tuning, a custom connection config).
    ///
    /// The pool is built in the synchronous app builder and not connected there:
    /// [`Broker::connect`] dials it at startup, and an unreachable server fails startup as it does
    /// for an address. A pool the service has already connected is adopted as it is. The config and the
    /// reconnection policy the pool was built from are reused for the dedicated connections
    /// subscriptions read on.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # mod demo {
    /// use std::time::Duration;
    ///
    /// use fred::clients::Pool;
    /// use fred::types::config::{Config, ConnectionConfig, ReconnectPolicy, ServerConfig};
    /// use ruststream_fred::stream::prelude::*;
    /// use serde::Deserialize;
    ///
    /// #[derive(Deserialize)]
    /// struct Order {
    ///     id: u64,
    /// }
    ///
    /// #[subscriber(RedisStream::new("orders").group("workers"))]
    /// async fn handle(order: &Order) -> HandlerOutcome {
    ///     tracing::info!(order = order.id, "order received");
    ///     HandlerOutcome::ack()
    /// }
    ///
    /// #[ruststream::app]
    /// fn app() -> impl App {
    ///     let config = Config {
    ///         server: ServerConfig::new_centralized("localhost", 6379),
    ///         ..Config::default()
    ///     };
    ///     let connection = ConnectionConfig {
    ///         connection_timeout: Duration::from_secs(2),
    ///         ..ConnectionConfig::default()
    ///     };
    ///     // Reconnect forever, backing off from 100 ms up to 10 s between attempts.
    ///     let policy = ReconnectPolicy::new_exponential(0, 100, 10_000, 2);
    ///     let pool = Pool::new(config, None, Some(connection), Some(policy), 8)
    ///         .expect("a pool of eight connections");
    ///
    ///     RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(
    ///         RedisBroker::from_pool(pool),
    ///         |b| {
    ///             b.include(handle);
    ///         },
    ///     )
    /// }
    /// # }
    /// # fn main() {}
    /// ```
    pub fn from_pool(pool: Pool) -> Self {
        Self::with_topology(Topology::Preconnected(pool))
    }

    fn with_topology(topology: Topology) -> Self {
        Self {
            topology,
            pool_size: DEFAULT_POOL_SIZE,
            default_group: None,
            auth: AuthConfig::default(),
        }
    }

    /// Sets the connection-pool size: the connections publishes, settlements and handler commands
    /// share. Defaults to 4. Each subscription reads on a connection of its own besides.
    pub const fn pool(mut self, size: usize) -> Self {
        self.pool_size = size;
        self
    }

    /// Sets a broker-wide default consumer group, enabling the bare-string `#[subscriber("key")]`
    /// form (Redis Streams always read through a group). Without it a bare-string subscription
    /// returns [`RedisError::InvalidOptions`]; name the group per subscription with
    /// [`RedisStream::group`] instead.
    pub fn default_group(mut self, group: impl Into<String>) -> Self {
        self.default_group = Some(group.into());
        self
    }

    /// Sets the ACL `username` and `password` used to authenticate on connect, applied on every
    /// topology (standalone, cluster, sentinel).
    ///
    /// This maps onto `fred`'s `Config.username` / `Config.password`, so authentication works
    /// beyond the standalone `redis://user:pass@host` URL, which the bare `cluster` / `sentinel`
    /// seed lists cannot express. Credentials set here override any in a standalone URL.
    ///
    /// For a password-only `AUTH` (the legacy `requirepass`, no ACL user) use
    /// [`password`](Self::password).
    ///
    /// # Examples
    ///
    /// ```
    /// # mod demo {
    /// use ruststream_fred::stream::prelude::*;
    /// # use serde::Deserialize;
    /// # #[derive(Deserialize)]
    /// # struct Order {
    /// #     id: u64,
    /// # }
    /// #
    /// # #[subscriber(RedisStream::new("orders").group("workers"))]
    /// # async fn handle(order: &Order) -> HandlerOutcome {
    /// #     tracing::info!(order.id, "order");
    /// #     HandlerOutcome::ack()
    /// # }
    ///
    /// #[ruststream::app]
    /// fn app() -> impl App {
    ///     RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(
    ///         RedisBroker::cluster(["10.0.0.1:6379"]).credentials("worker", "s3cr3t"),
    ///         |b| {
    ///             b.include(handle);
    ///         },
    ///     )
    /// }
    /// # }
    /// # fn main() {}
    /// ```
    pub fn credentials(mut self, username: impl Into<String>, password: impl Into<String>) -> Self {
        self.auth.username = Some(username.into());
        self.auth.password = Some(password.into());
        self
    }

    /// Sets a password-only `AUTH` (no ACL username; the legacy `requirepass` form), on every
    /// topology. Use [`credentials`](Self::credentials) for an ACL user plus password.
    ///
    /// # Examples
    ///
    /// ```
    /// # mod demo {
    /// use ruststream_fred::stream::prelude::*;
    /// # use serde::Deserialize;
    /// # #[derive(Deserialize)]
    /// # struct Order {
    /// #     id: u64,
    /// # }
    /// #
    /// # #[subscriber(RedisStream::new("orders").group("workers"))]
    /// # async fn handle(order: &Order) -> HandlerOutcome {
    /// #     tracing::info!(order.id, "order");
    /// #     HandlerOutcome::ack()
    /// # }
    ///
    /// #[ruststream::app]
    /// fn app() -> impl App {
    ///     RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(
    ///         RedisBroker::sentinel("mymaster", ["10.0.0.1:26379"]).password("s3cr3t"),
    ///         |b| {
    ///             b.include(handle);
    ///         },
    ///     )
    /// }
    /// # }
    /// # fn main() {}
    /// ```
    pub fn password(mut self, password: impl Into<String>) -> Self {
        self.auth.password = Some(password.into());
        self
    }

    /// Sets the TLS configuration used on connect, on every topology. Accepts a `fred`
    /// [`TlsConfig`] or anything convertible into one (for example a `TlsConnector`).
    ///
    /// Available behind the `tls-rustls`, `tls-rustls-ring`, or `tls-native-tls` feature; a
    /// standalone broker can also enable TLS through a `rediss://` / `valkeys://` URL. The
    /// `fred` re-exports [`TlsConfig`](crate::TlsConfig) / [`TlsConnector`](crate::TlsConnector)
    /// provide `default_rustls()` / `default_native_tls()` shorthands for system-trust setups.
    ///
    /// # Examples
    ///
    /// ```
    /// # #[cfg(feature = "tls-rustls")]
    /// # mod demo {
    /// use std::error::Error;
    ///
    /// use ruststream_fred::stream::prelude::*;
    /// # use serde::Deserialize;
    /// # #[derive(Deserialize)]
    /// # struct Order {
    /// #     id: u64,
    /// # }
    /// #
    /// # #[subscriber(RedisStream::new("orders").group("workers"))]
    /// # async fn handle(order: &Order) -> HandlerOutcome {
    /// #     tracing::info!(order.id, "order");
    /// #     HandlerOutcome::ack()
    /// # }
    ///
    /// pub async fn serve() -> Result<(), Box<dyn Error>> {
    ///     // System trust roots, no client certificate. The same connector works on every topology.
    ///     let tls = TlsConnector::default_rustls()?;
    ///     let broker = RedisBroker::cluster(["10.0.0.1:6379"])
    ///         .credentials("worker", "s3cr3t")
    ///         .tls(tls);
    ///     RustStream::new(AppInfo::new("orders", "0.1.0"))
    ///         .with_broker(broker, |b| {
    ///             b.include(handle);
    ///         })
    ///         .run()
    ///         .await?;
    ///     Ok(())
    /// }
    /// # }
    /// # fn main() {}
    /// ```
    #[cfg(any(
        feature = "tls-rustls",
        feature = "tls-rustls-ring",
        feature = "tls-native-tls"
    ))]
    pub fn tls(mut self, tls: impl Into<TlsConfig>) -> Self {
        self.auth.tls = Some(tls.into());
        self
    }

    /// Sets distinct credentials for authenticating to the sentinel nodes, separate from the
    /// data-node [`credentials`](Self::credentials). Only meaningful on the sentinel topology.
    ///
    /// Available behind the `sentinel-auth` feature.
    ///
    /// # Examples
    ///
    /// ```
    /// # mod demo {
    /// use ruststream_fred::stream::prelude::*;
    /// # use serde::Deserialize;
    /// # #[derive(Deserialize)]
    /// # struct Order {
    /// #     id: u64,
    /// # }
    /// #
    /// # #[subscriber(RedisStream::new("orders").group("workers"))]
    /// # async fn handle(order: &Order) -> HandlerOutcome {
    /// #     tracing::info!(order.id, "order");
    /// #     HandlerOutcome::ack()
    /// # }
    ///
    /// #[ruststream::app]
    /// fn app() -> impl App {
    ///     // The data nodes and the sentinels each check their own pair.
    ///     let broker = RedisBroker::sentinel("mymaster", ["10.0.0.1:26379"])
    ///         .credentials("worker", "data-pass")
    ///         .sentinel_credentials("sentinel-user", "sentinel-pass");
    ///     RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(
    ///         broker,
    ///         |b| {
    ///             b.include(handle);
    ///         },
    ///     )
    /// }
    /// # }
    /// # fn main() {}
    /// ```
    #[cfg(feature = "sentinel-auth")]
    pub fn sentinel_credentials(
        mut self,
        username: impl Into<String>,
        password: impl Into<String>,
    ) -> Self {
        self.auth.sentinel_username = Some(username.into());
        self.auth.sentinel_password = Some(password.into());
        self
    }

    /// Sets a password-only credential for authenticating to the sentinel nodes. Use
    /// [`sentinel_credentials`](Self::sentinel_credentials) for an ACL user plus password.
    ///
    /// Available behind the `sentinel-auth` feature.
    ///
    /// # Examples
    ///
    /// ```
    /// # mod demo {
    /// use ruststream_fred::stream::prelude::*;
    /// # use serde::Deserialize;
    /// # #[derive(Deserialize)]
    /// # struct Order {
    /// #     id: u64,
    /// # }
    /// #
    /// # #[subscriber(RedisStream::new("orders").group("workers"))]
    /// # async fn handle(order: &Order) -> HandlerOutcome {
    /// #     tracing::info!(order.id, "order");
    /// #     HandlerOutcome::ack()
    /// # }
    ///
    /// #[ruststream::app]
    /// fn app() -> impl App {
    ///     let broker = RedisBroker::sentinel("mymaster", ["10.0.0.1:26379"])
    ///         .password("data-pass")
    ///         .sentinel_password("sentinel-pass");
    ///     RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(
    ///         broker,
    ///         |b| {
    ///             b.include(handle);
    ///         },
    ///     )
    /// }
    /// # }
    /// # fn main() {}
    /// ```
    #[cfg(feature = "sentinel-auth")]
    pub fn sentinel_password(mut self, password: impl Into<String>) -> Self {
        self.auth.sentinel_password = Some(password.into());
        self
    }

    /// Sets a dynamic credential provider that supplies (and can rotate) the username/password on
    /// each `AUTH` / `HELLO`, for IAM-style auth. Takes precedence over static
    /// [`credentials`](Self::credentials).
    ///
    /// Available behind the `credential-provider` feature.
    ///
    /// # Examples
    ///
    /// ```
    /// # mod demo {
    /// use std::sync::Arc;
    ///
    /// use ruststream_fred::stream::prelude::*;
    /// # use serde::Deserialize;
    /// # use fred::error::Error as FredError;
    /// # use fred::types::config::Server;
    /// # use ruststream_fred::CredentialProvider;
    /// # #[derive(Debug)]
    /// # struct IamTokens;
    /// # #[async_trait::async_trait]
    /// # impl CredentialProvider for IamTokens {
    /// #     async fn fetch(
    /// #         &self,
    /// #         _server: Option<&Server>,
    /// #     ) -> Result<(Option<String>, Option<String>), FredError> {
    /// #         Ok((Some("worker".to_owned()), Some("token".to_owned())))
    /// #     }
    /// # }
    /// # #[derive(Deserialize)]
    /// # struct Order {
    /// #     id: u64,
    /// # }
    /// #
    /// # #[subscriber(RedisStream::new("orders").group("workers"))]
    /// # async fn handle(order: &Order) -> HandlerOutcome {
    /// #     tracing::info!(order.id, "order");
    /// #     HandlerOutcome::ack()
    /// # }
    ///
    /// #[ruststream::app]
    /// fn app() -> impl App {
    ///     // `IamTokens` fetches a fresh token from the identity service on every `AUTH`.
    ///     let broker = RedisBroker::standalone("redis://localhost:6379")
    ///         .credential_provider(Arc::new(IamTokens));
    ///     RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(
    ///         broker,
    ///         |b| {
    ///             b.include(handle);
    ///         },
    ///     )
    /// }
    /// # }
    /// # fn main() {}
    /// ```
    #[cfg(feature = "credential-provider")]
    pub fn credential_provider(mut self, provider: Arc<dyn CredentialProvider>) -> Self {
        self.auth.credential_provider = Some(provider);
        self
    }

    /// Builds the `fred` config for this broker's topology, then folds in the auth/TLS settings.
    fn build_config(&self) -> Result<Config, RedisError> {
        let mut config = match &self.topology {
            Topology::Standalone(url) => {
                Config::from_url(url).map_err(|err| RedisError::Connect(Box::new(err)))?
            }
            Topology::Cluster(nodes) => {
                let hosts = parse_servers(nodes, 6379)?;
                Config {
                    server: ServerConfig::new_clustered(hosts),
                    ..Config::default()
                }
            }
            Topology::Sentinel { service, hosts } => {
                let hosts = parse_servers(hosts, 26379)?;
                Config {
                    server: ServerConfig::new_sentinel(hosts, service.clone()),
                    ..Config::default()
                }
            }
            // A caller-supplied pool carries the config it was built from; reusing it keeps the
            // Pub/Sub path (which dials a dedicated client per subscription) available.
            Topology::Preconnected(pool) => pool.next().client_config(),
        };
        self.apply_auth(&mut config);
        Ok(config)
    }

    /// Folds the recorded auth/TLS settings into `config`. Each setting is applied only when set,
    /// so credentials carried by a standalone URL survive unless explicitly overridden.
    fn apply_auth(&self, config: &mut Config) {
        if self.auth.username.is_some() {
            config.username.clone_from(&self.auth.username);
        }
        if self.auth.password.is_some() {
            config.password.clone_from(&self.auth.password);
        }
        #[cfg(any(
            feature = "tls-rustls",
            feature = "tls-rustls-ring",
            feature = "tls-native-tls"
        ))]
        if self.auth.tls.is_some() {
            config.tls.clone_from(&self.auth.tls);
        }
        #[cfg(feature = "credential-provider")]
        if self.auth.credential_provider.is_some() {
            config
                .credential_provider
                .clone_from(&self.auth.credential_provider);
        }
        #[cfg(feature = "sentinel-auth")]
        if let ServerConfig::Sentinel {
            username, password, ..
        } = &mut config.server
        {
            if self.auth.sentinel_username.is_some() {
                username.clone_from(&self.auth.sentinel_username);
            }
            if self.auth.sentinel_password.is_some() {
                password.clone_from(&self.auth.sentinel_password);
            }
        }
    }
}

impl Broker for RedisBroker {
    type Error = RedisError;
    type Connected = ConnectedRedisBroker;

    /// Opens (or adopts) the connection pool, consuming the unconnected form.
    ///
    /// # Errors
    ///
    /// Returns [`RedisError::Connect`] when the recorded topology cannot be turned into a `fred`
    /// config or the pool cannot reach the server.
    async fn connect(self) -> Result<Self::Connected, Self::Error> {
        let config = self.build_config()?;
        // Read off the resolved config rather than the recorded topology, so a caller-supplied
        // pool that is itself clustered is treated as the cluster it is.
        let clustered = config.server.is_clustered();
        let (pool, reconnect) = if let Topology::Preconnected(pool) = &self.topology {
            // A pool built in the synchronous app builder has not dialed yet, and this is the
            // first async step it reaches. fred reports a client that was never started and one
            // waiting between reconnection attempts with the same `Disconnected` state, so a pool
            // with no client up is dialed here either way: its server is unreachable now, and
            // startup fails as it does for a pool built from an address.
            let started = pool
                .clients()
                .iter()
                .any(|client| client.state() != ClientState::Disconnected);
            if !started {
                pool.init()
                    .await
                    .map_err(|err| RedisError::Connect(Box::new(err)))?;
            }
            // The subscriptions' own connections reconnect the way the caller's pool does.
            (pool.clone(), pool.next().client_reconnect_policy())
        } else {
            let reconnect = reconnect_policy();
            let pool = Pool::new(
                config.clone(),
                None,
                None,
                Some(reconnect.clone()),
                self.pool_size,
            )
            .map_err(|err| RedisError::Connect(Box::new(err)))?;
            pool.init()
                .await
                .map_err(|err| RedisError::Connect(Box::new(err)))?;
            (pool, Some(reconnect))
        };
        Ok(ConnectedRedisBroker {
            core: Arc::new(RedisCore {
                connection: Connection::new(pool),
                closing: Arc::default(),
                runtime: Handle::current(),
                config,
                reconnect,
                default_group: self.default_group,
                clustered,
                routes: Routes::default(),
                rounds: Arc::default(),
                loopback: Loopback::default(),
                #[cfg(feature = "testing")]
                opened: Mutex::default(),
            }),
        })
    }
}

/// The in-process mode: the production connected broker over an in-process Redis, the transport
/// a test runs the service's own app against.
///
/// Every setting of this broker is read the way [`Broker::connect`] reads it: the topology's
/// address is parsed (a URL `connect` refuses is refused here), the pool keeps its size, the
/// default group and the cluster flag carry over, and a cluster topology refuses what spans hash
/// slots. What the model answers is described in the crate's testing overview.
#[cfg(feature = "testing")]
impl InProcess for RedisBroker {
    async fn connect_in_process(self) -> Result<Self::Connected, Self::Error> {
        let config = self.build_config()?;
        let clustered = config.server.is_clustered();
        let pool_size = match &self.topology {
            Topology::Preconnected(pool) => pool.size(),
            _ => self.pool_size,
        };
        let (pool, config, server) = in_process::connect(&config, pool_size, clustered).await?;
        Ok(ConnectedRedisBroker {
            core: Arc::new(RedisCore {
                connection: Connection::new(pool),
                closing: Arc::default(),
                runtime: Handle::current(),
                config,
                // The in-process server never drops a connection.
                reconnect: None,
                default_group: self.default_group,
                clustered,
                routes: Routes::default(),
                rounds: Arc::default(),
                loopback: Loopback::to(server),
                opened: Mutex::default(),
            }),
        })
    }
}

#[cfg(feature = "testing")]
ruststream::register_testable_broker!(RedisBroker);

/// `DescribeServer` reports the host and port a client dials (the first seed for cluster and
/// sentinel). Nothing else a configured address may carry reaches the description: a broker built
/// from an already-connected pool describes no host at all.
impl DescribeServer for RedisBroker {
    fn describe_server(&self) -> ServerSpec {
        let host = match &self.topology {
            Topology::Standalone(url) => describe_address(url, 6379),
            Topology::Cluster(nodes) => nodes
                .first()
                .map_or_else(String::new, |node| describe_address(node, 6379)),
            Topology::Sentinel { hosts, .. } => hosts
                .first()
                .map_or_else(String::new, |host| describe_address(host, 26379)),
            // A caller-supplied pool carries its address inside `fred`'s own config, which this
            // description does not reach into.
            Topology::Preconnected(_) => String::new(),
        };
        let mut spec = ServerSpec::new(host, "redis");
        // An address this broker cannot name is left out of the document rather than published as
        // an empty string, which a reader takes for a coordinate and tries to dial.
        if spec.host.as_deref() == Some("") {
            spec.host = None;
        }
        spec
    }
}

/// The `host:port` a client dials, with everything else an address may carry stripped: the scheme,
/// any `user:password@`, the database path and the query.
///
/// [`ServerSpec::host_from_url`] does the stripping, so the rule that keeps a credential out of a
/// published `AsyncAPI` document lives in one place for every broker. What is added here is the
/// Redis default port, which the core cannot know.
fn describe_address(addr: &str, default_port: u16) -> String {
    let host = ServerSpec::host_from_url(addr.trim());
    if host.is_empty() {
        return String::new();
    }
    if has_port(&host) {
        host
    } else {
        format!("{host}:{default_port}")
    }
}

/// Whether an address already names a port, allowing for a bracketed IPv6 literal (`[::1]:6379`).
fn has_port(host: &str) -> bool {
    host.rsplit_once(']').map_or_else(
        || {
            host.rsplit_once(':')
                .is_some_and(|(_, port)| !port.is_empty())
        },
        |(_, after_bracket)| after_bracket.starts_with(':'),
    )
}

/// The live connection shared by the connected broker and every handle derived from it.
pub(crate) struct RedisCore {
    /// The pool, and whether [`ConnectedBroker::shutdown`] closed it. The ladder makes owner-side
    /// misuse a compile error, but publishers and deliveries handed out before the shutdown alias
    /// the connection and outlive it, so they ask it before issuing a command.
    connection: Arc<Connection>,
    /// The closes of the subscriptions' read connections still running, which the shutdown
    /// waits for before it closes the pool.
    closing: Arc<Closing>,
    /// The runtime `connect` ran on. Every task the broker starts for itself is spawned here, so
    /// a call from a dedicated thread's runtime (a handler under `threads(n)`) leaves no task
    /// behind that dies when that runtime stops.
    runtime: Handle,
    /// The config the pool was built from; Pub/Sub subscriptions dial their dedicated client
    /// from it.
    config: Config,
    /// How the dedicated clients of subscriptions reconnect: the pool's own policy.
    reconnect: Option<ReconnectPolicy>,
    default_group: Option<String>,
    /// Whether the connection is to a cluster, which is where a command spanning two keys needs
    /// them on one hash slot: a `MULTI` block cannot span slots at all, and a reliable list's
    /// claim is a `BLMOVE` between its queue and its processing list.
    clustered: bool,
    /// How each name this connection's subscriptions read or dead-letter to is written, for the
    /// default publisher.
    routes: Routes,
    /// The rounds of the deliveries in flight on this connection's pipelined subscriptions, which
    /// a reply or a bound slot publish joins.
    rounds: Arc<Rounds>,
    /// The in-process server, on a connection the test harness made in process.
    loopback: Loopback,
    /// The subscriptions opened on this connection, which the test harness asks where a publish
    /// is delivered.
    #[cfg(feature = "testing")]
    opened: Mutex<Vec<Opened>>,
}

/// What one subscription reads, for the harness's routing question.
#[cfg(feature = "testing")]
#[derive(Debug, Clone)]
enum Opened {
    /// A consumer of `group` on the stream `key`.
    Stream { key: String, group: String },
    /// A consumer of the list `key`.
    List { key: String },
    /// A subscription to one channel.
    Channel { name: String, sharded: bool },
    /// A subscription to every channel a glob matches.
    Pattern { glob: String },
}

impl RedisCore {
    /// The rounds a publish of this connection may join.
    pub(crate) fn rounds(&self) -> &Arc<Rounds> {
        &self.rounds
    }

    /// The route table the default publisher reads.
    pub(crate) const fn routes(&self) -> &Routes {
        &self.routes
    }

    /// Records a subscription's own name and its dead-letter destination with `route`.
    ///
    /// # Errors
    ///
    /// Returns [`RedisError::InvalidOptions`] when either name is already written another way.
    pub(crate) fn record_routes(
        &self,
        name: &str,
        dead_letter: Option<&str>,
        route: &Route,
    ) -> Result<Recorded<'_>, RedisError> {
        self.routes.record_subscription(name, dead_letter, route)
    }

    /// The live pool, or [`RedisError::ShutDown`] once the connection was torn down.
    pub(crate) fn pool(&self) -> Result<Pool, RedisError> {
        self.connection.live().cloned()
    }

    /// The shared connection a subscription hands its deliveries, or [`RedisError::ShutDown`]
    /// once it was torn down.
    pub(crate) fn connection(&self) -> Result<Arc<Connection>, RedisError> {
        self.connection.live()?;
        Ok(Arc::clone(&self.connection))
    }

    /// Cluster cannot offer multi-key transactions, because buffered keys may hash to different
    /// nodes, so its publishers reject `begin_transaction`.
    pub(crate) const fn transactions_supported(&self) -> bool {
        !self.clustered
    }

    pub(crate) const fn clustered(&self) -> bool {
        self.clustered
    }

    /// The in-process server, when the connection is one.
    pub(crate) const fn loopback(&self) -> &Loopback {
        &self.loopback
    }

    #[cfg(feature = "testing")]
    fn opened(&self, subscription: Opened) {
        self.opened
            .lock()
            .expect("redis subscription record poisoned")
            .push(subscription);
    }

    /// The version the server reported, or the in-process server's own.
    #[cfg_attr(
        not(feature = "testing"),
        allow(
            clippy::unused_self,
            reason = "the connection is read under `testing` only"
        )
    )]
    fn server_version(&self, pool: &Pool) -> Option<Version> {
        #[cfg(feature = "testing")]
        if self.loopback.server().is_some() {
            return Some(in_process::VERSION);
        }
        pool.server_version()
    }
}

impl std::fmt::Debug for RedisCore {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RedisCore")
            .field("pool", self.connection.pool())
            .field("closed", &self.connection.is_closed())
            .finish_non_exhaustive()
    }
}

/// The typed witness that [`Broker::connect`] succeeded: it owns the live `fred` pool.
///
/// Every subscription and every publisher is reached from here, so "not connected" is not
/// representable. [`ConnectedBroker::shutdown`] consumes it, which makes a publish or subscribe
/// after shutdown a compile error for the owner of the handle.
#[derive(Debug)]
pub struct ConnectedRedisBroker {
    core: Arc<RedisCore>,
}

impl ConnectedRedisBroker {
    /// Opens a stream subscription described by `def`.
    ///
    /// Ensures the consumer group exists (`XGROUP CREATE ... MKSTREAM`, ignoring an
    /// already-existing group) before returning the subscriber.
    ///
    /// # Errors
    ///
    /// Returns [`RedisError::ShutDown`] when the connection was already torn down,
    /// [`RedisError::InvalidOptions`] when `def` names no consumer group, or
    /// [`RedisError::Subscribe`] when the group cannot be created.
    pub async fn subscribe(&self, def: RedisStream) -> Result<RedisSubscriber, RedisError> {
        let connection = self.core.connection()?;
        let pool = connection.pool();
        let group = def.group_or_err()?.to_owned();
        let consumer = def.consumer_or_auto();
        if let ReadMode::Claiming { .. } = def.mode() {
            require_claim_support(self.core.server_version(pool).as_ref(), def.key())?;
        }
        let recorded = self
            .core
            .record_routes(def.key(), def.dead_letter(), &Route::Stream)?;
        ensure_group(pool, def.key(), &group, def.start().as_id()).await?;
        let reader = self.read_connection().await?;
        let delay = def.delay_config().map(|cfg| cfg.on(self.core.loopback()));
        let tap = self.stream_tap(&def, &group, &consumer, delay.as_ref());
        recorded.keep();
        Ok(RedisSubscriber::new(
            connection,
            reader,
            def.key().to_owned(),
            group,
            consumer,
            def.block_or_default(),
            def.mode(),
            delay,
            tap,
        ))
    }

    /// Registers a stream subscription with the in-process server, and records it for the
    /// harness's routing question.
    #[cfg_attr(
        not(feature = "testing"),
        allow(
            clippy::unused_self,
            reason = "the connection is read under `testing` only"
        )
    )]
    fn stream_tap(
        &self,
        def: &RedisStream,
        group: &str,
        consumer: &str,
        delay: Option<&DelayConfig>,
    ) -> Tap {
        #[cfg(feature = "testing")]
        {
            self.core.opened(Opened::Stream {
                key: def.key().to_owned(),
                group: group.to_owned(),
            });
            if let Some(server) = self.core.loopback().server() {
                let (claim, fresh) = match def.mode() {
                    ReadMode::Fresh => (None, true),
                    ReadMode::Reclaim { min_idle } => (Some(min_idle), false),
                    ReadMode::Claiming { min_idle } => (Some(min_idle), true),
                };
                let reader = server.attach_stream(
                    def.key(),
                    group,
                    consumer,
                    claim,
                    fresh,
                    delay.map(DelayConfig::zset_key),
                );
                return Tap::registered(Arc::clone(server), reader);
            }
            Tap::default()
        }
        #[cfg(not(feature = "testing"))]
        {
            let _ = (def, group, consumer, delay);
            Tap::default()
        }
    }

    /// Opens a Pub/Sub subscription on one channel, described by `def`, on a dedicated client.
    ///
    /// # Errors
    ///
    /// Returns [`RedisError::ShutDown`] when the connection was already torn down,
    /// [`RedisError::Connect`] when the dedicated client cannot connect, or
    /// [`RedisError::Subscribe`] when the subscribe command fails.
    pub async fn subscribe_pubsub(
        &self,
        def: RedisPubSub,
    ) -> Result<RedisPubSubSubscriber, RedisError> {
        Ok(RedisPubSubSubscriber::new(self.open_pubsub(def).await?))
    }

    /// Subscribes a dedicated client to the channel `def` names and hands back its wire.
    pub(crate) async fn open_pubsub(&self, def: RedisPubSub) -> Result<PubSubWire, RedisError> {
        let codec = def.codec_handle();
        let recorded = self
            .core
            .record_routes(def.channel(), def.dead_letter(), &def.route())?;
        let client = self.new_client(def.buffer_size()).await?;
        // Opened before the subscribe, because the messages arrive over a broadcast channel whose
        // receiver sees only what is sent after it exists. The connection is dedicated to this one
        // subscription, so opening the stream early can pick up nothing else.
        let rx = client.message_rx();
        let reconnects = client.reconnect_rx();
        let channel = def.channel().to_owned();
        let sharded = def.delivery_mode() == PubSubMode::Sharded;
        let target = if sharded {
            Resubscribe::ShardedChannel(channel.clone())
        } else {
            Resubscribe::Channel(channel.clone())
        };
        match def.delivery_mode() {
            PubSubMode::Classic => {
                client
                    .subscribe(channel)
                    .await
                    .map_err(RedisError::subscribe)?;
                confirm_subscribed(&client).await?;
            }
            // `SSUBSCRIBE` is answered by the server, so `fred` waits for it and there is nothing
            // left to confirm here.
            PubSubMode::Sharded => {
                client
                    .ssubscribe(channel)
                    .await
                    .map_err(RedisError::subscribe)?;
            }
        }
        let (rx, tap) = self.pubsub_tap(
            rx,
            PubSubTarget::Channel {
                name: def.channel(),
                sharded,
            },
            def.buffer_size(),
        );
        let pool = self.core.pool()?;
        recorded.keep();
        let client = Resubscribing::new(client, reconnects, target, self.runtime());
        Ok(PubSubWire::new(
            def.channel().to_owned(),
            client,
            rx,
            codec,
            pool,
            tap,
            self.runtime().clone(),
        ))
    }

    /// Registers a Pub/Sub subscription with the in-process server, whose messages then arrive
    /// on a channel of its own instead of the client's, and records it for the harness's routing
    /// question.
    #[cfg_attr(
        not(feature = "testing"),
        allow(
            clippy::unused_self,
            reason = "the connection is read under `testing` only"
        )
    )]
    fn pubsub_tap(
        &self,
        rx: Receiver<Message>,
        target: PubSubTarget<'_>,
        buffer: Option<NonZeroUsize>,
    ) -> (Receiver<Message>, Tap) {
        #[cfg(feature = "testing")]
        {
            let (opened, subscription) = match target {
                PubSubTarget::Channel { name, sharded } => {
                    let bytes = bytes::Bytes::copy_from_slice(name.as_bytes());
                    (
                        Opened::Channel {
                            name: name.to_owned(),
                            sharded,
                        },
                        if sharded {
                            Subscription::Sharded(bytes)
                        } else {
                            Subscription::Channel(bytes)
                        },
                    )
                }
                PubSubTarget::Pattern(glob) => (
                    Opened::Pattern {
                        glob: glob.to_owned(),
                    },
                    Subscription::Pattern(bytes::Bytes::copy_from_slice(glob.as_bytes())),
                ),
            };
            self.core.opened(opened);
            if let Some(server) = self.core.loopback().server() {
                let (rx, reader) = server.attach_pubsub(subscription, buffer);
                return (rx, Tap::registered(Arc::clone(server), reader));
            }
            (rx, Tap::default())
        }
        #[cfg(not(feature = "testing"))]
        {
            let _ = (target, buffer);
            (rx, Tap::default())
        }
    }

    /// Opens a Pub/Sub subscription on a glob (`PSUBSCRIBE`), described by `def`, on a dedicated
    /// client.
    ///
    /// # Errors
    ///
    /// Returns [`RedisError::ShutDown`] when the connection was already torn down,
    /// [`RedisError::Connect`] when the dedicated client cannot connect, or
    /// [`RedisError::Subscribe`] when the subscribe command fails.
    pub async fn subscribe_pubsub_pattern(
        &self,
        def: RedisPubSubPattern,
    ) -> Result<RedisPubSubSubscriber, RedisError> {
        Ok(RedisPubSubSubscriber::new(
            self.open_pubsub_pattern(def).await?,
        ))
    }

    /// Subscribes a dedicated client to the glob `def` names and hands back its wire.
    pub(crate) async fn open_pubsub_pattern(
        &self,
        def: RedisPubSubPattern,
    ) -> Result<PubSubWire, RedisError> {
        let codec = def.codec_handle();
        let client = self.new_client(def.buffer_size()).await?;
        // Opened before the subscribe, for the reason `subscribe_pubsub` gives.
        let rx = client.message_rx();
        let reconnects = client.reconnect_rx();
        client
            .psubscribe(def.pattern().to_owned())
            .await
            .map_err(RedisError::subscribe)?;
        confirm_subscribed(&client).await?;
        let (rx, tap) =
            self.pubsub_tap(rx, PubSubTarget::Pattern(def.pattern()), def.buffer_size());
        let target = Resubscribe::Pattern(def.pattern().to_owned());
        let client = Resubscribing::new(client, reconnects, target, self.runtime());
        Ok(PubSubWire::new(
            def.pattern().to_owned(),
            client,
            rx,
            codec,
            self.core.pool()?,
            tap,
            self.runtime().clone(),
        ))
    }

    /// Opens a list (work-queue) subscription described by `def`.
    ///
    /// # Errors
    ///
    /// Returns [`RedisError::ShutDown`] when the connection was already torn down,
    /// [`RedisError::InvalidOptions`] when `def` names a recovery ZSET without a `min_idle`, or
    /// [`RedisError::Connect`] when the connection the subscription reads on cannot connect.
    pub async fn subscribe_list(&self, def: RedisList) -> Result<RedisListSubscriber, RedisError> {
        self.open_list(def).await.map(RedisListSubscriber::new)
    }

    /// Validates `def` against this connection and hands back its wire.
    pub(crate) async fn open_list(&self, def: RedisList) -> Result<ListWire, RedisError> {
        let connection = self.core.connection()?;
        let recovery = def
            .recovery_config()?
            .map(|cfg| cfg.on(self.core.loopback()));
        let reliable = def.is_reliable();
        let processing = def.processing_or_default();
        if reliable && self.core.clustered() {
            require_one_slot(def.key(), &processing)?;
        }
        self.core
            .record_routes(def.key(), def.dead_letter(), &def.route())?
            .keep();
        let block = def.block_or_default();
        let codec = def.codec_handle();
        let reader = self.read_connection().await?;
        let tap = self.list_tap(def.key(), recovery.as_ref());
        Ok(ListWire::new(
            connection,
            reader,
            def.into_key(),
            reliable,
            processing,
            block,
            codec,
            recovery,
            tap,
        ))
    }

    /// Registers a list subscription with the in-process server, and records it for the
    /// harness's routing question.
    #[cfg_attr(
        not(feature = "testing"),
        allow(
            clippy::unused_self,
            reason = "the connection is read under `testing` only"
        )
    )]
    fn list_tap(&self, key: &str, recovery: Option<&RecoveryConfig>) -> Tap {
        #[cfg(feature = "testing")]
        {
            self.core.opened(Opened::List {
                key: key.to_owned(),
            });
            if let Some(server) = self.core.loopback().server() {
                let reader = server.attach_list(
                    key,
                    recovery.map(|cfg| (cfg.zset_key.as_str(), cfg.min_idle)),
                );
                return Tap::registered(Arc::clone(server), reader);
            }
            Tap::default()
        }
        #[cfg(not(feature = "testing"))]
        {
            let _ = (key, recovery);
            Tap::default()
        }
    }

    /// Returns a stream publisher (`XADD`) bound to this connection.
    #[must_use]
    pub fn publisher(&self) -> RedisPublisher {
        RedisPublisher::new(Arc::clone(&self.core))
    }

    /// Returns the publisher of the broker's default policy, [`RedisDefaultPublish`], which
    /// writes each name the way this connection's subscriptions read it.
    ///
    /// [`RedisDefaultPublish`]: crate::RedisDefaultPublish
    #[must_use]
    pub fn default_publisher(&self) -> RedisDefaultPublisher {
        RedisDefaultPublisher::new(Arc::clone(&self.core))
    }

    /// Returns a Pub/Sub publisher configured by `publish` (mode and envelope codec).
    #[must_use]
    pub fn pubsub_publisher(&self, publish: RedisPubSubPublish) -> RedisPubSubPublisher {
        RedisPubSubPublisher::new(Arc::clone(&self.core), publish)
    }

    /// Returns a list publisher (`LPUSH`) configured by `publish` (envelope codec and key TTL).
    #[must_use]
    pub fn list_publisher(&self, publish: RedisListPublish) -> RedisListPublisher {
        RedisListPublisher::new(Arc::clone(&self.core), publish)
    }

    /// The rounds a publish of this connection may join.
    pub(crate) fn rounds(&self) -> &Arc<Rounds> {
        self.core.rounds()
    }

    /// The runtime this connection was opened on, where the broker spawns its own tasks.
    pub(crate) fn runtime(&self) -> &Handle {
        &self.core.runtime
    }

    /// What a pipelined subscription's window takes from this connection, or
    /// [`RedisError::ShutDown`] once it was torn down.
    pub(crate) fn window_owner(&self) -> Result<Owner, RedisError> {
        Ok(Owner {
            rounds: Arc::clone(self.rounds()),
            runtime: self.runtime().clone(),
            connection: self.core.connection()?,
        })
    }

    /// Returns a clone of the underlying pool, for advanced operations not covered by the
    /// wrapper.
    ///
    /// # Errors
    ///
    /// Returns [`RedisError::ShutDown`] once the connection was torn down.
    pub fn pool_handle(&self) -> Result<Pool, RedisError> {
        self.core.pool()
    }

    /// The connection a stream or list subscription reads on, apart from the pool its publishes
    /// and settlements go out on.
    async fn read_connection(&self) -> Result<ReadConnection, RedisError> {
        #[cfg(feature = "testing")]
        let blocks = self.core.loopback().server().is_none();
        #[cfg(not(feature = "testing"))]
        let blocks = true;
        Ok(ReadConnection::new(
            self.new_client(None).await?,
            self.runtime().clone(),
            Arc::clone(&self.core.closing),
            blocks,
        ))
    }

    /// Builds and connects a dedicated `fred` client (used for Pub/Sub, which needs an isolated
    /// message stream and channel state per subscriber, and for the reads of a stream or list
    /// subscription).
    ///
    /// `buffer` is how many messages its Pub/Sub channel holds for a reader that has not taken them
    /// yet; `None` leaves `fred`'s own setting.
    async fn new_client(&self, buffer: Option<NonZeroUsize>) -> Result<Client, RedisError> {
        // The dedicated client is a second connection to the same server: refuse to dial one for
        // a connection whose owner already shut down.
        let _ = self.core.pool()?;
        let performance = buffer.map(|buffer| PerformanceConfig {
            broadcast_channel_capacity: buffer.get(),
            ..PerformanceConfig::default()
        });
        let client = Client::new(
            self.core.config.clone(),
            performance,
            None,
            self.core.reconnect.clone(),
        );
        // `init` spawns the task that drives the connection on the runtime it is polled on; it is
        // polled on the broker's own runtime, so the connection outlives a caller's runtime. The
        // price is one task spawned per subscription opened.
        let connecting = client.clone();
        self.core
            .runtime
            .spawn(async move { connecting.init().await })
            .await
            .map_err(|err| RedisError::Connect(Box::new(err)))?
            .map_err(|err| RedisError::Connect(Box::new(err)))?;
        Ok(client)
    }
}

impl ConnectedBroker for ConnectedRedisBroker {
    type Error = RedisError;
    type Closed = ClosedRedisBroker;

    /// Closes every pooled connection and marks the shared connection dead, so publishers handed
    /// out earlier report [`RedisError::ShutDown`] instead of running against a closed pool.
    ///
    /// # Errors
    ///
    /// Returns [`RedisError::Connect`] when the `QUIT` roundtrip fails.
    async fn shutdown(self) -> Result<Self::Closed, Self::Error> {
        self.core.connection.close();
        self.core.closing.finish().await;
        let connections_closed = self.core.connection.pool().clients().len();
        self.core
            .connection
            .pool()
            .quit()
            .await
            .map_err(|err| RedisError::Connect(Box::new(err)))?;
        // The in-process server goes down with the connection, after the `QUIT` it answers.
        #[cfg(feature = "testing")]
        if let Some(server) = self.core.loopback().server() {
            server.close();
        }
        Ok(ClosedRedisBroker { connections_closed })
    }
}

/// What a Pub/Sub subscription listens to, as its registration names it.
#[cfg_attr(
    not(feature = "testing"),
    allow(dead_code, reason = "read by the in-process registration only")
)]
#[derive(Clone, Copy)]
enum PubSubTarget<'a> {
    Channel { name: &'a str, sharded: bool },
    Pattern(&'a str),
}

/// The harness's view of the connection: the in-process server it injects into and reads the
/// writes of, and, in both modes, where a publish is delivered.
///
/// # Panics
///
/// `inject` and `published` panic on a broker connected with `connect`: the harness drives only
/// the connection `connect_in_process` produced, and a live connection has no log to read and no
/// synchronous way to take a message.
#[cfg(feature = "testing")]
impl TestableBroker for ConnectedRedisBroker {
    fn install_coordinator(&self, coordinator: Coordinator) {
        if let Some(server) = self.core.loopback().server() {
            server.install(coordinator);
        }
    }

    /// Writes the message the way the default publisher writes its name: an `XADD`, or the
    /// `LPUSH` or `PUBLISH` of the subscription that reads it, in that subscription's framing. A
    /// name only pattern subscriptions read goes out with a raw `PUBLISH`, as the producers they
    /// listen to publish it: an `XADD` would reach none of them.
    fn inject(&self, message: OutgoingMessage<'_>) {
        let server = self.in_process("inject");
        let name = message.name();
        let (payload, headers) = (message.payload(), message.headers());
        let written = match self.core.routes().route(name) {
            Route::Stream if self.read_by_patterns_alone(name) => {
                server.inject_publish(name, frame(None, payload, headers), false)
            }
            Route::Stream => {
                server.inject_stream(name, fields_for_publish(payload.to_vec(), headers))
            }
            Route::List { envelope } => {
                server.inject_list(name, frame(envelope.as_ref(), payload, headers))
            }
            Route::Channel { mode, envelope } => server.inject_publish(
                name,
                frame(envelope.as_ref(), payload, headers),
                mode == PubSubMode::Sharded,
            ),
        };
        if let Err(err) = written {
            panic!("the injected message to {name:?} is not one Redis takes: {err}");
        }
    }

    /// Every write to `name`, decoded the way the subscription reading it decodes: an entry's
    /// fields, or a list element or a channel message in its subscription's framing. A requeue's
    /// copy and a delayed entry the sweep adds back are writes too.
    fn published(&self, name: &str) -> Vec<RawMessage> {
        let server = self.in_process("published");
        let envelope = match self.core.routes().route(name) {
            Route::Stream => None,
            Route::List { envelope } | Route::Channel { envelope, .. } => envelope,
        };
        server
            .published(name)
            .into_iter()
            .map(|write| {
                let (payload, headers) = match write {
                    Write::Fields(fields) => parts_from_fields(
                        fields
                            .into_iter()
                            .map(|(field, value)| {
                                (String::from_utf8_lossy(&field).into_owned(), value.to_vec())
                            })
                            .collect(),
                    ),
                    Write::Body(body) => unframe(envelope.as_ref(), &body),
                };
                RawMessage::new(name.to_owned(), payload).with_headers(headers)
            })
            .collect()
    }

    /// Redis's routing: a stream entry is delivered once to every consumer group reading the
    /// stream (to one consumer of each), a list element to one consumer, and a channel message to
    /// every subscription of the channel and every pattern subscription whose glob matches it. A
    /// name no subscription reads exactly is reached by the pattern subscriptions alone.
    ///
    /// Subscriptions sharing a name are told apart by nothing but the name, so a publish is owed
    /// by the first position of each name as many times as that name receives it. In process
    /// that is where it goes: a group's new entries go to its earliest open subscription.
    fn routes(&self, destination: &str, subscriptions: &[&str]) -> Vec<usize> {
        let opened = self
            .core
            .opened
            .lock()
            .expect("redis subscription record poisoned")
            .clone();
        let mut owed = Vec::new();
        let mut owe = |name: &str, times: usize| {
            if let Some(at) = subscriptions.iter().position(|held| *held == name) {
                owed.extend(std::iter::repeat_n(at, times));
            }
        };
        let groups: std::collections::BTreeSet<&str> = opened
            .iter()
            .filter_map(|sub| match sub {
                Opened::Stream { key, group } if key == destination => Some(group.as_str()),
                _ => None,
            })
            .collect();
        let patterns = match self.core.routes().route(destination) {
            Route::Stream => {
                owe(destination, groups.len());
                groups.is_empty()
            }
            Route::List { .. } => {
                let read = opened
                    .iter()
                    .any(|sub| matches!(sub, Opened::List { key } if key == destination));
                owe(destination, usize::from(read));
                false
            }
            Route::Channel { mode, .. } => {
                let sharded = mode == PubSubMode::Sharded;
                let reached = opened
                    .iter()
                    .filter(|sub| {
                        matches!(sub, Opened::Channel { name, sharded: held }
                            if name == destination && *held == sharded)
                    })
                    .count();
                owe(destination, reached);
                !sharded
            }
        };
        if patterns {
            for sub in &opened {
                if let Opened::Pattern { glob } = sub
                    && in_process::glob_matches(glob.as_bytes(), destination.as_bytes())
                {
                    owe(glob, 1);
                }
            }
        }
        owed
    }
}

#[cfg(feature = "testing")]
impl ConnectedRedisBroker {
    /// Whether `name` is read by a pattern subscription and by no stream group: what the harness
    /// then publishes to the channel instead of adding to a stream.
    fn read_by_patterns_alone(&self, name: &str) -> bool {
        let opened = self
            .core
            .opened
            .lock()
            .expect("redis subscription record poisoned");
        let grouped = opened
            .iter()
            .any(|sub| matches!(sub, Opened::Stream { key, .. } if key == name));
        !grouped
            && opened.iter().any(|sub| {
                matches!(sub, Opened::Pattern { glob }
                    if in_process::glob_matches(glob.as_bytes(), name.as_bytes()))
            })
    }

    /// The in-process server, which is all the harness injects into and reads.
    fn in_process(&self, what: &str) -> &Arc<in_process::Server> {
        self.core.loopback().server().unwrap_or_else(|| {
            panic!(
                "TestableBroker::{what} reached a broker connected with `connect`; the harness \
                 drives the connection `connect_in_process` produces"
            )
        })
    }
}

/// The terminal witness returned by shutting down a [`ConnectedRedisBroker`].
///
/// It has no publish or subscribe surface; it carries the teardown diagnostics as plain data.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ClosedRedisBroker {
    connections_closed: usize,
}

impl ClosedRedisBroker {
    /// How many pooled connections the teardown closed.
    #[must_use]
    pub const fn connections_closed(&self) -> usize {
        self.connections_closed
    }
}

// By-name subscription capability for the bare string `#[subscriber("key")]` form. Redis Streams
// always read through a consumer group, so this requires a broker-wide default group.
#[allow(
    clippy::use_self,
    reason = "the type name disambiguates the inherent subscribe from this trait method"
)]
impl Subscribe for ConnectedRedisBroker {
    type Subscriber = RedisSubscriber;
    /// A bare name opens a consumer group over that stream key, and an `XADD` there is read by the
    /// group again, so the name is both ends and a retry copy needs nothing from the mount site.
    type Copies = AddressedCopies;

    async fn subscribe(&self, name: &str) -> Result<Self::Subscriber, Self::Error> {
        let group = self
            .core
            .default_group
            .clone()
            .ok_or_else(|| missing_default_group(name, "RedisBroker"))?;
        ConnectedRedisBroker::subscribe(self, RedisStream::new(name).group(group)).await
    }
}

/// The refusal of a bare-string subscription on a broker with no default group: Redis Streams read
/// through a consumer group, and a bare name names none. `broker` is the type whose
/// `default_group` setting supplies one.
pub(crate) fn missing_default_group(name: &str, broker: &str) -> RedisError {
    RedisError::InvalidOptions(format!(
        "bare-string subscription on `{name}` needs a broker-wide default group: call \
         {broker}::default_group(name), or subscribe with RedisStream::new(name).group(group)"
    ))
}

/// Refuses a [`RedisStream::claiming`] subscription the connected server cannot serve.
///
/// The version is the one the `fred` client read from `INFO server` during its handshake, so the
/// check costs no round trip and runs once, where the subscription resolves against the connected
/// broker. Without it the mistake surfaces on the first read, as a bare syntax error from a server
/// that does not know the option.
///
/// A server that reports no version is refused the same way: the option either exists or the
/// service does not start, and a guess is what the check is here to avoid.
fn require_claim_support(version: Option<&Version>, key: &str) -> Result<(), RedisError> {
    match version {
        Some(version) if *version >= CLAIM_MIN_VERSION => Ok(()),
        Some(version) => Err(RedisError::ServerTooOld(format!(
            "RedisStream::claiming on {key:?} needs Redis {CLAIM_MIN_VERSION} or later \
             (XREADGROUP CLAIM); the connected server is {version}"
        ))),
        None => Err(RedisError::ServerTooOld(format!(
            "RedisStream::claiming on {key:?} needs Redis {CLAIM_MIN_VERSION} or later \
             (XREADGROUP CLAIM); the connected server reports no version"
        ))),
    }
}

/// Waits for the server to apply the `SUBSCRIBE` or `PSUBSCRIBE` the client just issued.
///
/// `fred` answers both the moment the frame is queued: Redis confirms them out of band, on the
/// message stream, and the client drops that confirmation without surfacing it. A publish issued
/// the instant `subscribe(..)` returned therefore reaches a server that has not routed the channel
/// here yet, and Pub/Sub keeps nothing for a subscriber who is not there: the message is gone,
/// with no error on either side. That is a lost delivery on a subscription the caller was told was
/// open, so it is worth the round trip this costs once per subscription.
///
/// Redis serves one connection's commands in order, so a reply to a command sent after the
/// subscribe cannot come back before the subscribe was applied. The `PING` carries the hash `fred`
/// pins the legacy Pub/Sub commands to, so on a cluster it is answered by the node the subscribe
/// reached rather than by another one.
async fn confirm_subscribed(client: &Client) -> Result<(), RedisError> {
    let node = ClusterHash::Custom(redis_keyslot(client.id().as_bytes()));
    let _: Value = client
        .custom(CustomCommand::new("PING", node, false), Vec::<Value>::new())
        .await
        .map_err(RedisError::subscribe)?;
    Ok(())
}

/// Refuses a reliable list on a cluster whose two keys live on different nodes.
///
/// A claim is a `BLMOVE` from the queue to the processing list, and Redis refuses a command that
/// spans hash slots. Nothing says so until the first delivery, so the subscription is refused
/// where it is opened and the message names the hash tag that puts both keys on one node.
fn require_one_slot(key: &str, processing: &str) -> Result<(), RedisError> {
    if redis_keyslot(key.as_bytes()) == redis_keyslot(processing.as_bytes()) {
        return Ok(());
    }
    Err(RedisError::InvalidOptions(format!(
        "a reliable list on `{key}` cannot run on a cluster while its processing list \
         `{processing}` hashes to another slot: a claim moves the entry between the two in one \
         command. Put both on one slot with a hash tag, as in RedisList::new(\"{{{key}}}\"), which \
         the default processing key follows"
    )))
}

/// Creates the consumer group, treating an already-existing group as success.
pub(crate) async fn ensure_group(
    pool: &Pool,
    key: &str,
    group: &str,
    start_id: &str,
) -> Result<(), RedisError> {
    let result: Result<String, fred::error::Error> =
        pool.xgroup_create(key, group, start_id, true).await;
    match result {
        Ok(_) => Ok(()),
        // BUSYGROUP: the group already exists, which is the steady-state case.
        Err(err) if err.details().contains("BUSYGROUP") => Ok(()),
        Err(err) => Err(RedisError::subscribe(err)),
    }
}

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

    /// The refusal names the subscription, the mode and both versions, so the operator reads
    /// what to change without reaching for the source.
    #[test]
    fn a_claiming_subscription_names_both_versions_when_it_refuses() {
        let err = require_claim_support(Some(&Version::new(7, 4, 2)), "orders")
            .expect_err("a server below 8.4.0 must be refused");
        assert_eq!(
            err.to_string(),
            "RedisStream::claiming on \"orders\" needs Redis 8.4.0 or later (XREADGROUP CLAIM); \
             the connected server is 7.4.2"
        );
    }

    /// A server that reports no version is refused too: the option either exists or the service
    /// does not start.
    #[test]
    fn a_server_with_no_version_is_refused() {
        let err = require_claim_support(None, "orders")
            .expect_err("a server reporting no version must be refused");
        assert!(
            err.to_string().contains("reports no version"),
            "the refusal has to say the version is missing: {err}"
        );
    }

    /// 8.4.0 is the floor, not a version to be above, and a later release keeps the option.
    #[test]
    fn the_floor_release_and_later_are_accepted() {
        for version in [
            Version::new(8, 4, 0),
            Version::new(8, 4, 6),
            Version::new(9, 0, 0),
        ] {
            assert!(
                require_claim_support(Some(&version), "orders").is_ok(),
                "{version} has XREADGROUP CLAIM"
            );
        }
        for version in [Version::new(8, 3, 9), Version::new(7, 4, 2)] {
            assert!(
                require_claim_support(Some(&version), "orders").is_err(),
                "{version} does not have XREADGROUP CLAIM"
            );
        }
    }

    #[test]
    fn describe_server_reports_redis() {
        let broker = RedisBroker::standalone("redis://localhost:6379");
        let spec = broker.describe_server();
        assert_eq!(spec.protocol, "redis");
        assert_eq!(spec.host.as_deref(), Some("localhost:6379"));
    }

    /// Every address shape this crate accepts reduces to the host and the port a client dials.
    #[test]
    fn a_description_is_the_host_and_port_of_every_address_shape() {
        let cases = [
            ("redis://localhost:6379", "localhost:6379"),
            ("rediss://redis.example.com:6380", "redis.example.com:6380"),
            ("redis://localhost", "localhost:6379"),
            ("redis://localhost:6379/3", "localhost:6379"),
            ("redis://localhost:6379/?timeout=5s", "localhost:6379"),
            (
                "valkeys://valkey.example.com:6380",
                "valkey.example.com:6380",
            ),
            ("10.0.0.1:6379", "10.0.0.1:6379"),
            ("  redis://localhost:6379  ", "localhost:6379"),
            ("redis://[::1]:6379", "[::1]:6379"),
            ("redis://[::1]", "[::1]:6379"),
        ];

        for (url, expected) in cases {
            let spec = RedisBroker::standalone(url).describe_server();
            assert_eq!(
                spec.host.as_deref(),
                Some(expected),
                "the description of {url:?} is wrong"
            );
        }

        let cluster = RedisBroker::cluster(["10.0.0.1", "10.0.0.2:7001"]).describe_server();
        assert_eq!(cluster.host.as_deref(), Some("10.0.0.1:6379"));

        let sentinel = RedisBroker::sentinel("mymaster", ["10.0.0.9"]).describe_server();
        assert_eq!(sentinel.host.as_deref(), Some("10.0.0.9:26379"));
    }

    /// The description ends up in the generated `AsyncAPI` document, which teams publish, so a URL
    /// carrying credentials must leave neither the user name nor the password behind. The password
    /// here holds an `@` of its own, the case a split on the first separator gets wrong.
    #[test]
    fn a_description_never_carries_the_credentials_of_a_url() {
        let cases = [
            (
                RedisBroker::standalone("redis://alice:p@ss@redis.example.com:6379/0"),
                "redis.example.com:6379",
            ),
            (
                RedisBroker::cluster(["redis://alice:p@ss@10.0.0.1:7000"]),
                "10.0.0.1:7000",
            ),
            (
                RedisBroker::sentinel("mymaster", ["redis://alice:p@ss@10.0.0.9:26379"]),
                "10.0.0.9:26379",
            ),
        ];

        for (broker, expected) in cases {
            let host = broker.describe_server().host.expect("a described host");
            assert_eq!(host, expected, "the description kept more than the address");
            assert!(
                !host.contains('@'),
                "{host:?} still carries a userinfo part"
            );
            assert!(
                !host.contains("alice"),
                "{host:?} still carries the user name"
            );
            assert!(
                !host.contains("p@ss"),
                "{host:?} still carries the password"
            );
        }
    }

    // Credentials must reach the fred config on every topology, not just the standalone URL.
    #[test]
    fn credentials_apply_to_all_topologies() {
        let brokers = [
            RedisBroker::standalone("redis://localhost:6379").credentials("alice", "s3cr3t"),
            RedisBroker::cluster(["127.0.0.1:7000"]).credentials("alice", "s3cr3t"),
            RedisBroker::sentinel("mymaster", ["127.0.0.1:26379"]).credentials("alice", "s3cr3t"),
        ];
        for broker in brokers {
            let config = broker.build_config().expect("config builds");
            assert_eq!(config.username.as_deref(), Some("alice"));
            assert_eq!(config.password.as_deref(), Some("s3cr3t"));
        }
    }

    #[test]
    fn password_only_sets_password_without_username() {
        let config = RedisBroker::cluster(["127.0.0.1:7000"])
            .password("requirepass")
            .build_config()
            .expect("config builds");
        assert_eq!(config.username, None);
        assert_eq!(config.password.as_deref(), Some("requirepass"));
    }

    // Programmatic credentials win over a standalone URL's userinfo.
    #[test]
    fn programmatic_credentials_override_standalone_url() {
        let config = RedisBroker::standalone("redis://urluser:urlpass@localhost:6379")
            .credentials("acluser", "aclpass")
            .build_config()
            .expect("config builds");
        assert_eq!(config.username.as_deref(), Some("acluser"));
        assert_eq!(config.password.as_deref(), Some("aclpass"));
    }

    // Without an override the URL's credentials are left untouched.
    #[test]
    fn url_credentials_preserved_without_override() {
        let config = RedisBroker::standalone("redis://urluser:urlpass@localhost:6379")
            .build_config()
            .expect("config builds");
        assert_eq!(config.username.as_deref(), Some("urluser"));
        assert_eq!(config.password.as_deref(), Some("urlpass"));
    }

    #[test]
    fn debug_redacts_password() {
        let broker =
            RedisBroker::standalone("redis://localhost:6379").credentials("alice", "s3cr3t");
        let rendered = format!("{broker:?}");
        assert!(
            !rendered.contains("s3cr3t"),
            "password must not appear in Debug output: {rendered}"
        );
        // The username is an identifier, not a secret, and is kept for debugging.
        assert!(
            rendered.contains("alice"),
            "expected username in: {rendered}"
        );
    }

    /// A connect error is logged, so the address it names carries no password.
    #[test]
    fn an_address_error_names_no_password() {
        for broker in [
            RedisBroker::cluster(["redis://bob:seed-secret@node1:notaport"]),
            RedisBroker::sentinel("mymaster", ["bob:seed-secret@sentinel1:notaport"]),
        ] {
            let error = broker.build_config().expect_err("the address is invalid");
            let printed = error.to_string();
            assert!(printed.contains("bob"), "the address is named: {printed}");
            assert!(
                !printed.contains("seed-secret"),
                "the password leaks: {printed}"
            );
        }
    }

    #[cfg(feature = "sentinel-auth")]
    #[test]
    fn sentinel_credentials_apply_to_sentinel_server() {
        let config = RedisBroker::sentinel("mymaster", ["127.0.0.1:26379"])
            .credentials("datauser", "datapass")
            .sentinel_credentials("sentineluser", "sentinelpass")
            .build_config()
            .expect("config builds");
        // Data-node credentials sit on the top-level config.
        assert_eq!(config.username.as_deref(), Some("datauser"));
        let ServerConfig::Sentinel {
            username, password, ..
        } = &config.server
        else {
            panic!("expected a sentinel server config");
        };
        assert_eq!(username.as_deref(), Some("sentineluser"));
        assert_eq!(password.as_deref(), Some("sentinelpass"));
    }

    #[cfg(feature = "credential-provider")]
    #[derive(Debug)]
    struct StaticCredentials;

    #[cfg(feature = "credential-provider")]
    #[async_trait::async_trait]
    impl CredentialProvider for StaticCredentials {
        async fn fetch(
            &self,
            _server: Option<&fred::types::config::Server>,
        ) -> Result<(Option<String>, Option<String>), fred::error::Error> {
            Ok((Some("rotating".into()), Some("token".into())))
        }
    }

    #[cfg(feature = "credential-provider")]
    #[test]
    fn credential_provider_is_applied() {
        let provider: Arc<dyn CredentialProvider> = Arc::new(StaticCredentials);
        let config = RedisBroker::cluster(["127.0.0.1:7000"])
            .credential_provider(provider)
            .build_config()
            .expect("config builds");
        assert!(config.credential_provider.is_some());
    }
}