popsub_sdk 0.1.0

Rust client for PopSub: reconnection, durable sessions, acknowledgements and deduplication
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
//! The driver: one task per `Client` that owns the WebSocket and does
//! everything the application shouldn't have to.
//!
//! It connects, logs in and authenticates, then reads frames and the
//! client's commands until the connection ends. When it ends, the close code
//! decides what happens next: reconnect with jittered backoff, or stop for
//! good (`SessionTakenOver`, `UnsupportedProtocol`, rejected credentials).
//! After reconnecting it resubscribes every topic from the next `seq` it
//! expects (topics a durable session restored on its own are left to the
//! server), and resends confirmed publishes that never got their
//! `published`, under the same `message_id` so the server stores them once.
//!
//! Every message is checked against the next `seq` its subscription
//! expects, so a retry or a replay after reconnecting never reaches the
//! application twice. A duplicate the application already acknowledged (or
//! any duplicate in `AckMode::Auto`) is acknowledged again, so a lost ack
//! can't stall the subscription.
//!
//! A wildcard subscription (`orders/#`) is one `Pattern`; each topic the
//! server joins for it becomes a `Topic` owned by that pattern, with its
//! own next expected `seq`, so deduplication and gap-free resubscription
//! work per topic exactly as for a subscription by name. Messages from
//! every owned topic go to the pattern's one `Subscription`.
//!
//! A shared subscription (a topic read through a `group`) gets only some
//! of the topic's messages, out of order when another member's are handed
//! over, so it remembers the `seq`s it has delivered instead of a next
//! expected one, and after a reconnect rejoins the group with `resume`: the
//! group's position lives on the server.
//!
//! When the server ends a subscription on its own (an admin revoked the
//! user's access), it sends `unsubscribed` with no id. The driver then
//! drops the topic, which ends its `Subscription`, and reports
//! `Event::Unsubscribed`. Every unsubscribe the driver sends carries an id,
//! so its reply is never mistaken for that.
//!
//! While a subscription's buffer is full the driver waits, which stops it
//! reading the socket; the server then paces delivery for this connection.

use std::collections::{HashMap, HashSet, VecDeque};
use std::sync::{Arc, OnceLock};
use std::time::Duration;

use futures_util::{SinkExt, StreamExt};
use popsub_protocol::{
    ClientFrame, ClientMessage, CloseCode, Delivery, Encoding, ErrorCode, Limits, PROTOCOL_VERSION,
    RequestId, ServerFrame, ServerMessage, StartFrom, Subscribed,
};
use tokio::net::TcpStream;
use tokio::sync::{broadcast, mpsc, oneshot};
use tokio::time::Instant;
use tokio_tungstenite::tungstenite::Message as WsMessage;
use tokio_tungstenite::{
    Connector, MaybeTlsStream, WebSocketStream, connect_async_tls_with_config,
};
use tracing::{debug, info, warn};

use crate::config::{AckMode, ClientConfig, Credentials};
use crate::error::Error;
use crate::message::{Event, Message};

/// The client side of a WebSocket connection.
type Socket = WebSocketStream<MaybeTlsStream<TcpStream>>;

/// How many acknowledged `seq`s a subscription remembers, to re-acknowledge
/// duplicates the application already acknowledged.
const ACK_MEMORY: usize = 4096;

/// How many QoS 0 publishes are held while disconnected; older ones are
/// dropped first.
const OFFLINE_PUBLISHES: usize = 10_000;

/// How long the driver waits for something from the server before deciding
/// the connection is dead, before the server has announced its own limits.
const DEFAULT_SILENCE: Duration = Duration::from_secs(120);

/// The result of a confirmed publish.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Published {
    /// The message's position in the topic.
    pub seq: u64,
    /// The id it was stored under.
    pub message_id: String,
}

/// What the `Client` asks the driver to do.
#[derive(Debug)]
pub(crate) enum Command {
    /// Subscribe and report the `subscribed` frame.
    Subscribe {
        /// The topic.
        topic: String,
        /// Where replay starts.
        from: StartFrom,
        /// The shared subscription to join, if any.
        group: Option<String>,
        /// Identifies this subscription, so a stale `Forget` can't remove a newer one.
        generation: u64,
        /// Where the topic's messages go.
        buffer: mpsc::Sender<Message>,
        /// Where the outcome goes.
        reply: oneshot::Sender<Result<Subscribed, Error>>,
    },
    /// Unsubscribe and report `unsubscribed`.
    Unsubscribe {
        /// The topic.
        topic: String,
        /// Where the outcome goes.
        reply: oneshot::Sender<Result<(), Error>>,
    },
    /// Unsubscribe without waiting, when a `Subscription` is dropped.
    Forget {
        /// The topic.
        topic: String,
        /// The subscription being dropped.
        generation: u64,
    },
    /// Publish a message.
    Publish {
        /// The topic.
        topic: String,
        /// The body.
        payload: String,
        /// How the body encodes the message's bytes; `None` means it is text.
        encoding: Option<Encoding>,
        /// For a confirmed (QoS 1) publish, the idempotency key and where the
        /// outcome goes. `None` is a QoS 0 publish.
        confirm: Option<(String, oneshot::Sender<Result<Published, Error>>)>,
    },
    /// Acknowledge a message (from `Message::ack`).
    Ack {
        /// The topic.
        topic: String,
        /// The message's `seq`.
        seq: u64,
    },
    /// Send a request that is neither a subscribe, a publish nor an ack
    /// (the admin requests), and report its reply. Never queued while
    /// disconnected and never resent, because it may not be safe to repeat.
    Request {
        /// The request.
        message: ClientMessage,
        /// Where the reply goes; an `error` frame arrives as `Error::Server`.
        reply: oneshot::Sender<Result<ServerMessage, Error>>,
    },
    /// Close the connection and stop.
    Close,
}

/// The `seq`s a subscription has acknowledged recently.
#[derive(Debug, Default)]
struct AckMemory {
    /// The acknowledged `seq`s.
    seen: HashSet<u64>,
    /// The same `seq`s, oldest first, for forgetting the oldest.
    order: VecDeque<u64>,
}

impl AckMemory {
    /// Remembers an acknowledged `seq`, forgetting the oldest beyond `ACK_MEMORY`.
    ///
    /// Args:
    ///     seq: The acknowledged message.
    fn insert(&mut self, seq: u64) {
        if self.seen.insert(seq) {
            self.order.push_back(seq);
            if self.order.len() > ACK_MEMORY
                && let Some(oldest) = self.order.pop_front()
            {
                self.seen.remove(&oldest);
            }
        }
    }

    /// Whether a `seq` was acknowledged.
    ///
    /// Args:
    ///     seq: The message.
    ///
    /// Returns:
    ///     True if it is remembered as acknowledged.
    fn contains(&self, seq: u64) -> bool {
        self.seen.contains(&seq)
    }
}

/// One subscribed topic.
#[derive(Debug)]
struct Topic {
    /// Where its messages go.
    buffer: mpsc::Sender<Message>,
    /// Where replay started when the application subscribed.
    from: StartFrom,
    /// The next `seq` the application should see. Anything lower is a duplicate.
    next_expected: Option<u64>,
    /// Which subscription this is.
    generation: u64,
    /// What it has acknowledged.
    acked: AckMemory,
    /// The wildcard subscription this topic was joined through, if any.
    owner: Option<String>,
    /// The shared subscription this topic is read through, if any. Its
    /// messages arrive out of order and with gaps (other members get the
    /// rest), so duplicates are found with `seen` instead of `next_expected`.
    group: Option<String>,
    /// The `seq`s already handed to the application, for a shared subscription.
    seen: AckMemory,
    /// Whether the server held this topic for the session before the
    /// application subscribed, and the `subscribed` that answers the
    /// application's subscribe hasn't arrived yet. Until it does, messages
    /// on the topic are from the server's own resume: they are dropped
    /// without an acknowledgement, because the subscribe replays them.
    claiming: bool,
}

/// One wildcard subscription. The topics it brings in are `Topic`s owned by it.
#[derive(Debug)]
struct Pattern {
    /// Where the messages of every topic it matches go.
    buffer: mpsc::Sender<Message>,
    /// Which subscription this is.
    generation: u64,
}

/// A request waiting for its reply, by request id.
#[derive(Debug)]
enum Pending {
    /// A subscribe; the waiter is in `Driver::subscribe_waiters`.
    Subscribe {
        /// The topic.
        topic: String,
    },
    /// An unsubscribe.
    Unsubscribe {
        /// Where the outcome goes.
        reply: oneshot::Sender<Result<(), Error>>,
    },
    /// A confirmed publish; its frame is in `Driver::unconfirmed` for resending.
    Publish {
        /// Where the outcome goes.
        reply: oneshot::Sender<Result<Published, Error>>,
    },
    /// A request from `Command::Request`.
    Request {
        /// Where the reply goes.
        reply: oneshot::Sender<Result<ServerMessage, Error>>,
    },
    /// A login to refresh the token on the current connection.
    Relogin,
    /// The `auth` that installs the refreshed token.
    Reauth,
}

/// How a connection ended.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Ending {
    /// The application closed the client.
    Stop,
    /// The connection was lost, with the server's close code if it sent one.
    Lost(Option<CloseCode>),
}

/// Why connecting failed.
#[derive(Debug)]
struct ConnectFailure {
    /// The error to report.
    error: Error,
    /// Whether retrying could help.
    retry: bool,
}

/// The driver's state.
pub(crate) struct Driver {
    /// How to connect.
    config: ClientConfig,
    /// Commands from the `Client`.
    commands: mpsc::UnboundedReceiver<Command>,
    /// A weak handle on the command queue, for `Message::ack`. Weak, so the
    /// driver stops when every `Client` and `Subscription` is dropped.
    acker: mpsc::WeakUnboundedSender<Command>,
    /// Where events go.
    events: broadcast::Sender<Event>,
    /// Why the client stopped, once it has.
    closed: Arc<OnceLock<Option<CloseCode>>>,
    /// Subscribed topics.
    topics: HashMap<String, Topic>,
    /// Wildcard subscriptions, by pattern.
    patterns: HashMap<String, Pattern>,
    /// Topics the server restored for the session that the application
    /// hasn't subscribed to in this process. Their messages are dropped
    /// without an acknowledgement, so the server keeps them for the session.
    unclaimed: HashSet<String>,
    /// Whether the server left the session's topics unattached on this
    /// connection (`authenticated.held`). Then nothing arrives on a topic
    /// until this client subscribes to it, so nothing is unclaimed, and
    /// `restore` subscribes to the resumed topics itself.
    held: bool,
    /// Subscribes waiting for `subscribed`, by topic.
    subscribe_waiters: HashMap<String, oneshot::Sender<Result<Subscribed, Error>>>,
    /// Requests waiting for a reply, by id.
    pending: HashMap<u64, Pending>,
    /// Confirmed publishes not yet confirmed, by request id, for resending.
    unconfirmed: HashMap<u64, ClientMessage>,
    /// Frames to send once connected (QoS 0 publishes, unsubscribes).
    offline: VecDeque<ClientMessage>,
    /// The next request id.
    next_id: u64,
    /// Whether a connection has ever succeeded, so `clean_start` is only sent once.
    connected_once: bool,
    /// How long to wait for anything from the server before giving up on
    /// the connection.
    silence: Duration,
    /// The TLS setup for `wss://` URLs, built once.
    tls: Connector,
}

impl Driver {
    /// Builds a driver that hasn't connected yet.
    ///
    /// Args:
    ///     config: How to connect.
    ///     commands: Commands from the `Client`.
    ///     acker: A weak handle on the command queue.
    ///     events: Where events go.
    ///     closed: Set to the final close code when the client stops.
    ///
    /// Returns:
    ///     The driver.
    pub(crate) fn new(
        config: ClientConfig,
        commands: mpsc::UnboundedReceiver<Command>,
        acker: mpsc::WeakUnboundedSender<Command>,
        events: broadcast::Sender<Event>,
        closed: Arc<OnceLock<Option<CloseCode>>>,
    ) -> Self {
        Self {
            commands,
            acker,
            events,
            closed,
            topics: HashMap::new(),
            patterns: HashMap::new(),
            unclaimed: HashSet::new(),
            held: false,
            subscribe_waiters: HashMap::new(),
            pending: HashMap::new(),
            unconfirmed: HashMap::new(),
            offline: VecDeque::new(),
            next_id: 1,
            connected_once: false,
            silence: DEFAULT_SILENCE,
            tls: tls_connector(&config.extra_roots),
            config,
        }
    }

    /// Runs until the client stops: connects, serves the connection,
    /// reconnects with backoff, and reports the first connection's outcome.
    ///
    /// Args:
    ///     ready: Gets `Ok` when the first connection is up, or the error
    ///         that stopped the client before it ever connected.
    pub(crate) async fn run(mut self, ready: oneshot::Sender<Result<(), Error>>) {
        let mut ready = Some(ready);
        let mut attempt: u32 = 0;
        let mut last_error: Option<Error> = None;
        let final_code = loop {
            match self.connect().await {
                Ok((socket, resumed)) => {
                    attempt = 0;
                    self.connected_once = true;
                    if let Some(ready) = ready.take() {
                        let _ = ready.send(Ok(()));
                    }
                    let _ = self.events.send(Event::Connected {
                        resumed: resumed.clone(),
                    });
                    match self.serve(socket, resumed).await {
                        Ending::Stop => break None,
                        Ending::Lost(code) => {
                            self.fail_requests();
                            let reconnecting = self.should_reconnect(code);
                            let _ = self.events.send(Event::Disconnected { code, reconnecting });
                            if !reconnecting {
                                break code;
                            }
                        }
                    }
                }
                Err(failure) => {
                    warn!(error = %failure.error, "connecting failed");
                    last_error = Some(failure.error.clone());
                    if !failure.retry {
                        if let Some(ready) = ready.take() {
                            let _ = ready.send(Err(failure.error));
                        }
                        break None;
                    }
                }
            }
            attempt += 1;
            if self
                .config
                .reconnect
                .max_attempts
                .is_some_and(|max| attempt > max)
            {
                if let Some(ready) = ready.take() {
                    let reason = last_error
                        .as_ref()
                        .map_or_else(String::new, |e| format!("; the last attempt failed: {e}"));
                    let _ = ready.send(Err(Error::Connect(format!(
                        "ran out of reconnect attempts{reason}"
                    ))));
                }
                break None;
            }
            if !self.wait_before_retry(attempt).await {
                break None;
            }
        };
        self.stop(final_code);
    }

    /// Decides whether to reconnect after a connection was lost.
    ///
    /// Args:
    ///     code: The server's close code, if it sent one.
    ///
    /// Returns:
    ///     True to reconnect. An expired token only allows it with password
    ///     credentials, which log in again.
    fn should_reconnect(&self, code: Option<CloseCode>) -> bool {
        match code {
            None => true,
            Some(CloseCode::TokenExpired) => {
                matches!(self.config.credentials, Credentials::Password { .. })
            }
            Some(code) => code.should_reconnect(),
        }
    }

    /// Waits before the next connection attempt, queueing commands meanwhile.
    ///
    /// Args:
    ///     attempt: How many attempts in a row have failed.
    ///
    /// Returns:
    ///     False when the application closed the client while waiting.
    async fn wait_before_retry(&mut self, attempt: u32) -> bool {
        let ceiling = self.config.reconnect.ceiling(attempt);
        let delay = ceiling.mul_f64(rand::random::<f64>());
        debug!(?delay, attempt, "waiting before reconnecting");
        let until = Instant::now() + delay;
        loop {
            tokio::select! {
                _ = tokio::time::sleep_until(until) => return true,
                command = self.commands.recv() => match command {
                    None | Some(Command::Close) => return false,
                    Some(command) => self.queue_offline(command),
                },
            }
        }
    }

    /// Fails every `Command::Request` still waiting when a connection is
    /// lost. The server may or may not have carried it out.
    fn fail_requests(&mut self) {
        let waiting: Vec<u64> = self
            .pending
            .iter()
            .filter(|(_, pending)| matches!(pending, Pending::Request { .. }))
            .map(|(id, _)| *id)
            .collect();
        for id in waiting {
            if let Some(Pending::Request { reply }) = self.pending.remove(&id) {
                let _ = reply.send(Err(Error::Connect(
                    "the connection dropped before the reply; the request may or may not have been carried out".to_string(),
                )));
            }
        }
    }

    /// Takes a request id for a frame the driver sends on its own.
    ///
    /// An unsubscribe always gets one, so its `unsubscribed` reply can't be
    /// mistaken for the server ending the subscription, which it announces
    /// with no id.
    ///
    /// Args:
    ///     message: The frame.
    ///
    /// Returns:
    ///     An id for an unsubscribe, otherwise `None`.
    fn quiet_id(&mut self, message: &ClientMessage) -> Option<u64> {
        matches!(message, ClientMessage::Unsubscribe { .. }).then(|| self.take_id())
    }

    /// Takes a request id.
    ///
    /// Returns:
    ///     A new id.
    fn take_id(&mut self) -> u64 {
        let id = self.next_id;
        self.next_id += 1;
        id
    }

    /// Opens a connection, logs in if needed, and authenticates.
    ///
    /// Returns:
    ///     The socket and the topics the server restored for the session.
    ///
    /// Raises:
    ///     ConnectFailure: With `retry` false for rejected credentials, an
    ///         unsupported protocol or a bad session id, and true otherwise.
    async fn connect(&mut self) -> Result<(Socket, Vec<String>), ConnectFailure> {
        let retry = |error: Error| ConnectFailure { error, retry: true };
        let (mut socket, _) = connect_async_tls_with_config(
            self.config.url.as_str(),
            None,
            false,
            Some(self.tls.clone()),
        )
        .await
        .map_err(|e| retry(Error::Connect(e.to_string())))?;
        let token = match self.config.credentials.clone() {
            Credentials::Token(token) => token,
            Credentials::Password { username, password } => {
                let id = self.take_id();
                match self
                    .exchange(&mut socket, id, ClientMessage::Login { username, password })
                    .await?
                {
                    ServerMessage::LoggedIn { token } => token,
                    other => return Err(refusal(other)),
                }
            }
        };
        let id = self.take_id();
        let auth = ClientMessage::Auth {
            token,
            protocol: Some(PROTOCOL_VERSION),
            session_id: self.config.session_id.clone(),
            clean_start: self.config.clean_start && !self.connected_once,
            hold_resumed: self.config.session_id.is_some(),
        };
        match self.exchange(&mut socket, id, auth).await? {
            ServerMessage::Authenticated {
                resumed,
                limits,
                held,
                ..
            } => {
                self.silence = silence_limit(&limits);
                self.held = held;
                info!(url = %self.config.url, resumed = resumed.len(), held, "connected");
                Ok((socket, resumed))
            }
            other => Err(refusal(other)),
        }
    }

    /// Sends a request during connecting and waits for its reply.
    ///
    /// Args:
    ///     socket: The connection.
    ///     id: The request id.
    ///     message: The request.
    ///
    /// Returns:
    ///     The reply.
    ///
    /// Raises:
    ///     ConnectFailure: When the connection drops or closes first, or no
    ///         reply arrives within the request timeout.
    async fn exchange(
        &self,
        socket: &mut Socket,
        id: u64,
        message: ClientMessage,
    ) -> Result<ServerMessage, ConnectFailure> {
        let lost = |error: Error| ConnectFailure { error, retry: true };
        send(socket, Some(id), message)
            .await
            .map_err(|_| lost(Error::Connect("the connection dropped".to_string())))?;
        let wait = async {
            while let Some(frame) = socket.next().await {
                match frame {
                    Ok(WsMessage::Text(text)) => {
                        if let Ok(frame) = serde_json::from_str::<ServerFrame>(&text)
                            && frame.id == Some(RequestId::Number(id))
                        {
                            return Ok(frame.message);
                        }
                    }
                    Ok(WsMessage::Close(close)) => {
                        let code = close.and_then(|c| CloseCode::from_code(c.code.into()));
                        return Err(ConnectFailure {
                            error: Error::Closed(code),
                            retry: code.is_none_or(|c| c.should_reconnect()),
                        });
                    }
                    Ok(_) => {}
                    Err(e) => return Err(lost(Error::Connect(e.to_string()))),
                }
            }
            Err(lost(Error::Connect("the connection closed".to_string())))
        };
        tokio::time::timeout(self.config.request_timeout, wait)
            .await
            .unwrap_or_else(|_| Err(lost(Error::Timeout)))
    }

    /// Serves one connection until it ends.
    ///
    /// Restores subscriptions and resends unconfirmed publishes and queued
    /// frames first, then handles frames and commands.
    ///
    /// Args:
    ///     socket: The authenticated connection.
    ///     resumed: Topics the server restored for the session.
    ///
    /// Returns:
    ///     How the connection ended.
    async fn serve(&mut self, mut socket: Socket, resumed: Vec<String>) -> Ending {
        self.unclaimed = resumed
            .iter()
            .filter(|name| !self.held && !self.topics.contains_key(*name))
            .cloned()
            .collect();
        if self.restore(&mut socket, &resumed).await.is_err() {
            return Ending::Lost(None);
        }
        let mut last_heard = Instant::now();
        loop {
            tokio::select! {
                frame = socket.next() => {
                    last_heard = Instant::now();
                    match frame {
                        Some(Ok(WsMessage::Text(text))) => {
                            match serde_json::from_str::<ServerFrame>(&text) {
                                Ok(frame) => {
                                    if self.on_frame(&mut socket, frame).await.is_err() {
                                        return Ending::Lost(None);
                                    }
                                }
                                Err(e) => debug!(%e, "ignoring a frame this client doesn't know"),
                            }
                        }
                        Some(Ok(WsMessage::Close(close))) => {
                            return Ending::Lost(close.and_then(|c| CloseCode::from_code(c.code.into())));
                        }
                        Some(Ok(_)) => {}
                        Some(Err(e)) => {
                            debug!(%e, "connection error");
                            return Ending::Lost(None);
                        }
                        None => return Ending::Lost(None),
                    }
                }
                command = self.commands.recv() => match command {
                    None | Some(Command::Close) => {
                        let _ = socket.close(None).await;
                        return Ending::Stop;
                    }
                    Some(command) => {
                        if self.on_command(&mut socket, command).await.is_err() {
                            return Ending::Lost(None);
                        }
                    }
                },
                _ = tokio::time::sleep_until(last_heard + self.silence) => {
                    warn!("no word from the server, reconnecting");
                    return Ending::Lost(None);
                }
            }
        }
    }

    /// Restores state on a new connection: resubscribes, resends
    /// unconfirmed publishes, sends queued frames.
    ///
    /// When the server holds the session's topics (`held`), each one this
    /// client tracks is subscribed again from `Resume`, which is one past
    /// the session's cursor. Otherwise topics the server restored for the
    /// session are left alone, except
    /// one still waiting for the `subscribed` that claims it, which is
    /// subscribed again the way the application asked; every other topic is subscribed again from the next `seq` it expects, so
    /// nothing still in the log is missed. Wildcard subscriptions are sent
    /// again after that, from `seq` 1: the server only joins topics the
    /// connection doesn't hold yet, which are exactly the ones created while
    /// it was away, so they are read from their first message.
    ///
    /// Args:
    ///     socket: The connection.
    ///     resumed: Topics the server restored.
    ///
    /// Raises:
    ///     (): When a write fails.
    async fn restore(&mut self, socket: &mut Socket, resumed: &[String]) -> Result<(), ()> {
        let mut resubscribe = Vec::new();
        for (name, topic) in &self.topics {
            if topic.group.is_some() {
                resubscribe.push((name.clone(), StartFrom::Resume, topic.group.clone()));
            } else if topic.claiming {
                let from = match topic.owner {
                    Some(_) => StartFrom::Resume,
                    None => topic.from,
                };
                resubscribe.push((name.clone(), from, None));
            } else if !resumed.contains(name) {
                let from = topic.next_expected.map_or(topic.from, StartFrom::Seq);
                resubscribe.push((name.clone(), from, None));
            } else if self.held {
                resubscribe.push((name.clone(), StartFrom::Resume, None));
            }
        }
        for (name, from, group) in resubscribe {
            let id = self.take_id();
            self.pending.insert(
                id,
                Pending::Subscribe {
                    topic: name.clone(),
                },
            );
            send(
                socket,
                Some(id),
                ClientMessage::Subscribe {
                    topic: name,
                    from,
                    group,
                },
            )
            .await?;
        }
        let mut patterns: Vec<String> = self.patterns.keys().cloned().collect();
        patterns.sort();
        for pattern in patterns {
            let id = self.take_id();
            self.pending.insert(
                id,
                Pending::Subscribe {
                    topic: pattern.clone(),
                },
            );
            let again = ClientMessage::Subscribe {
                topic: pattern,
                from: StartFrom::Seq(1),
                group: None,
            };
            send(socket, Some(id), again).await?;
        }
        let mut unconfirmed: Vec<(u64, ClientMessage)> = self
            .unconfirmed
            .iter()
            .map(|(id, message)| (*id, message.clone()))
            .collect();
        unconfirmed.sort_by_key(|(id, _)| *id);
        for (id, message) in unconfirmed {
            send(socket, Some(id), message).await?;
        }
        while let Some(message) = self.offline.pop_front() {
            let id = self.quiet_id(&message);
            send(socket, id, message).await?;
        }
        Ok(())
    }

    /// Records a command that arrived while disconnected, to act on after
    /// reconnecting.
    ///
    /// Args:
    ///     command: The command.
    fn queue_offline(&mut self, command: Command) {
        match command {
            Command::Subscribe {
                topic,
                from,
                group,
                generation,
                buffer,
                reply,
            } => {
                self.add_topic(topic.clone(), from, group, generation, buffer);
                self.subscribe_waiters.insert(topic, reply);
            }
            Command::Unsubscribe { topic, reply } => {
                let mut names = self.release(&topic, None);
                if names.is_empty() {
                    names.push(topic);
                }
                for name in names {
                    self.offline
                        .push_back(ClientMessage::Unsubscribe { topic: name });
                }
                let _ = reply.send(Ok(()));
            }
            Command::Forget { topic, generation } => {
                for name in self.release(&topic, Some(generation)) {
                    self.offline
                        .push_back(ClientMessage::Unsubscribe { topic: name });
                }
            }
            Command::Publish {
                topic,
                payload,
                encoding,
                confirm,
            } => {
                self.queue_publish(topic, payload, encoding, confirm);
            }
            Command::Ack { topic, seq } => {
                if let Some(entry) = self.topics.get_mut(&topic) {
                    entry.acked.insert(seq);
                }
            }
            Command::Request { reply, .. } => {
                let _ = reply.send(Err(Error::Connect(
                    "not connected; this request isn't queued while disconnected".to_string(),
                )));
            }
            Command::Close => {}
        }
    }

    /// Registers a publish: a confirmed one waits in `unconfirmed` for its
    /// `published`, a QoS 0 one waits in `offline` until it is sent.
    ///
    /// Args:
    ///     topic: The topic.
    ///     payload: The body.
    ///     encoding: How the body encodes the message's bytes, if it isn't text.
    ///     confirm: The idempotency key and reply for a confirmed publish.
    ///
    /// Returns:
    ///     The request id of a confirmed publish.
    fn queue_publish(
        &mut self,
        topic: String,
        payload: String,
        encoding: Option<Encoding>,
        confirm: Option<(String, oneshot::Sender<Result<Published, Error>>)>,
    ) -> Option<u64> {
        match confirm {
            Some((message_id, reply)) => {
                let id = self.take_id();
                self.pending.insert(id, Pending::Publish { reply });
                self.unconfirmed.insert(
                    id,
                    ClientMessage::Publish {
                        topic,
                        payload,
                        encoding,
                        message_id: Some(message_id),
                        qos: Some(1),
                    },
                );
                Some(id)
            }
            None => {
                if self.offline.len() >= OFFLINE_PUBLISHES {
                    self.offline.pop_front();
                    warn!("dropped the oldest publish held while disconnected");
                }
                self.offline.push_back(ClientMessage::Publish {
                    topic,
                    payload,
                    encoding,
                    message_id: None,
                    qos: None,
                });
                None
            }
        }
    }

    /// Adds or replaces a subscription: a topic, or a wildcard pattern.
    ///
    /// Subscribing again to a pattern the client holds replaces the
    /// subscription: the topics it owned move to the new one and keep the
    /// next `seq` they expect, because the server sends nothing again for
    /// topics the connection holds. Subscribing again to a topic by name
    /// starts over from `from`.
    ///
    /// A topic the server held for the session, subscribed by name or
    /// matched by the pattern, is claimed: it is tracked with `claiming`
    /// set, so what the server sent on its own is dropped until the
    /// `subscribed` that answers the claim. A topic subscribed again while
    /// its claim is unanswered stays `claiming`.
    ///
    /// Args:
    ///     topic: The topic or pattern.
    ///     from: Where replay starts.
    ///     group: The shared subscription to join, if any.
    ///     generation: Which subscription this is.
    ///     buffer: Where its messages go.
    ///
    /// Returns:
    ///     For a pattern, the session's topics it claimed, which the caller
    ///     subscribes to by name with `StartFrom::Resume`. Empty for a topic.
    fn add_topic(
        &mut self,
        topic: String,
        from: StartFrom,
        group: Option<String>,
        generation: u64,
        buffer: mpsc::Sender<Message>,
    ) -> Vec<String> {
        if popsub_protocol::topics::is_pattern(&topic) {
            for owned in self.topics.values_mut() {
                if owned.owner.as_deref() == Some(topic.as_str()) {
                    owned.buffer = buffer.clone();
                    owned.generation = generation;
                }
            }
            let mut claimed: Vec<String> = self
                .unclaimed
                .iter()
                .filter(|name| popsub_protocol::topics::matches_pattern(&topic, name))
                .cloned()
                .collect();
            claimed.sort();
            for name in &claimed {
                self.unclaimed.remove(name);
                self.topics.insert(
                    name.clone(),
                    Topic {
                        buffer: buffer.clone(),
                        from: StartFrom::Resume,
                        next_expected: None,
                        generation,
                        acked: AckMemory::default(),
                        owner: Some(topic.clone()),
                        group: None,
                        seen: AckMemory::default(),
                        claiming: true,
                    },
                );
            }
            self.patterns.insert(topic, Pattern { buffer, generation });
            return claimed;
        }
        let claiming = self.unclaimed.remove(&topic)
            || self.topics.get(&topic).is_some_and(|held| held.claiming);
        self.topics.insert(
            topic,
            Topic {
                buffer,
                from,
                next_expected: None,
                generation,
                acked: AckMemory::default(),
                owner: None,
                group,
                seen: AckMemory::default(),
                claiming,
            },
        );
        Vec::new()
    }

    /// Starts tracking a topic the server joined for one of this client's
    /// wildcard subscriptions. When several match, the oldest gets it.
    ///
    /// Args:
    ///     name: The topic.
    ///     start: The first `seq` the server will send, when known.
    ///
    /// Returns:
    ///     Whether a wildcard subscription matched.
    fn adopt(&mut self, name: &str, start: Option<u64>) -> bool {
        let Some((pattern, owner)) = self
            .patterns
            .iter()
            .filter(|(pattern, _)| popsub_protocol::topics::matches_pattern(pattern, name))
            .min_by_key(|(_, owner)| owner.generation)
        else {
            return false;
        };
        let topic = Topic {
            buffer: owner.buffer.clone(),
            from: start.map_or(StartFrom::Seq(1), StartFrom::Seq),
            next_expected: start,
            generation: owner.generation,
            acked: AckMemory::default(),
            owner: Some(pattern.clone()),
            group: None,
            seen: AckMemory::default(),
            claiming: false,
        };
        self.topics.insert(name.to_string(), topic);
        true
    }

    /// Stops tracking a subscription: a topic subscribed by name, or a
    /// wildcard pattern together with every topic joined through it.
    ///
    /// Args:
    ///     name: The topic or pattern.
    ///     generation: Only release it if it is still this subscription;
    ///         `None` releases whatever is there.
    ///
    /// Returns:
    ///     What to unsubscribe from on the server: the name, and for a
    ///     pattern the topics it owned. Empty when nothing was released.
    fn release(&mut self, name: &str, generation: Option<u64>) -> Vec<String> {
        let same = |held: u64| generation.is_none_or(|wanted| wanted == held);
        if self.patterns.get(name).is_some_and(|p| same(p.generation)) {
            self.patterns.remove(name);
            let mut names = vec![name.to_string()];
            let owned: Vec<String> = self
                .topics
                .iter()
                .filter(|(_, topic)| topic.owner.as_deref() == Some(name))
                .map(|(topic, _)| topic.clone())
                .collect();
            for topic in owned {
                self.topics.remove(&topic);
                names.push(topic);
            }
            return names;
        }
        let by_name = self
            .topics
            .get(name)
            .is_some_and(|t| t.owner.is_none() && same(t.generation));
        if by_name {
            self.topics.remove(name);
            return vec![name.to_string()];
        }
        Vec::new()
    }

    /// Acts on a command while connected.
    ///
    /// Args:
    ///     socket: The connection.
    ///     command: The command.
    ///
    /// Raises:
    ///     (): When a write fails.
    async fn on_command(&mut self, socket: &mut Socket, command: Command) -> Result<(), ()> {
        match command {
            Command::Subscribe {
                topic,
                from,
                group,
                generation,
                buffer,
                reply,
            } => {
                let claimed =
                    self.add_topic(topic.clone(), from, group.clone(), generation, buffer);
                self.subscribe_waiters.insert(topic.clone(), reply);
                let id = self.take_id();
                self.pending.insert(
                    id,
                    Pending::Subscribe {
                        topic: topic.clone(),
                    },
                );
                let subscribe = ClientMessage::Subscribe { topic, from, group };
                send(socket, Some(id), subscribe).await?;
                for name in claimed {
                    let id = self.take_id();
                    self.pending.insert(
                        id,
                        Pending::Subscribe {
                            topic: name.clone(),
                        },
                    );
                    let claim = ClientMessage::Subscribe {
                        topic: name,
                        from: StartFrom::Resume,
                        group: None,
                    };
                    send(socket, Some(id), claim).await?;
                }
                Ok(())
            }
            Command::Unsubscribe { topic, reply } => {
                let mut names = self.release(&topic, None);
                if names.is_empty() {
                    names.push(topic);
                }
                let mut reply = Some(reply);
                for name in names {
                    let id = self.take_id();
                    if let Some(reply) = reply.take() {
                        self.pending.insert(id, Pending::Unsubscribe { reply });
                    }
                    send(socket, Some(id), ClientMessage::Unsubscribe { topic: name }).await?;
                }
                Ok(())
            }
            Command::Forget { topic, generation } => {
                for name in self.release(&topic, Some(generation)) {
                    let id = self.take_id();
                    send(socket, Some(id), ClientMessage::Unsubscribe { topic: name }).await?;
                }
                Ok(())
            }
            Command::Publish {
                topic,
                payload,
                encoding,
                confirm,
            } => match self.queue_publish(topic, payload, encoding, confirm) {
                Some(id) => {
                    let message = self.unconfirmed[&id].clone();
                    send(socket, Some(id), message).await
                }
                None => {
                    while let Some(message) = self.offline.pop_front() {
                        let id = self.quiet_id(&message);
                        send(socket, id, message).await?;
                    }
                    Ok(())
                }
            },
            Command::Ack { topic, seq } => {
                if let Some(entry) = self.topics.get_mut(&topic) {
                    entry.acked.insert(seq);
                }
                send(socket, None, ClientMessage::Ack { topic, seq }).await
            }
            Command::Request { message, reply } => {
                let id = self.take_id();
                self.pending.insert(id, Pending::Request { reply });
                send(socket, Some(id), message).await
            }
            Command::Close => Ok(()),
        }
    }

    /// Acts on a frame from the server.
    ///
    /// Args:
    ///     socket: The connection.
    ///     frame: The frame.
    ///
    /// Raises:
    ///     (): When a write fails.
    async fn on_frame(&mut self, socket: &mut Socket, frame: ServerFrame) -> Result<(), ()> {
        let pending = match &frame.id {
            Some(RequestId::Number(id)) => self.pending.remove(id).map(|p| (*id, p)),
            _ => None,
        };
        let pending = match pending {
            Some((_, Pending::Request { reply })) => {
                let outcome = match frame.message {
                    ServerMessage::Error {
                        code,
                        message,
                        retry_after,
                    } => Err(Error::Server {
                        code,
                        message,
                        retry_after,
                    }),
                    other => Ok(other),
                };
                let _ = reply.send(outcome);
                return Ok(());
            }
            other => other,
        };
        let unprompted = frame.id.is_none();
        match frame.message {
            ServerMessage::Message(delivery) => self.deliver(socket, delivery).await?,
            ServerMessage::Subscribed(subscribed) => {
                let start = subscribed.replay_from;
                if self.patterns.contains_key(&subscribed.topic) {
                    debug!(pattern = %subscribed.topic, "wildcard subscription accepted");
                } else if let Some(topic) = self.topics.get_mut(&subscribed.topic) {
                    if topic.claiming {
                        topic.claiming = false;
                        topic.next_expected = None;
                    }
                    if topic.group.is_none() {
                        topic.next_expected =
                            Some(topic.next_expected.map_or(start, |n| n.max(start)));
                    }
                } else {
                    self.adopt(&subscribed.topic, Some(start));
                }
                if let Some(waiter) = self.subscribe_waiters.remove(&subscribed.topic) {
                    let _ = waiter.send(Ok(subscribed));
                }
            }
            ServerMessage::Unsubscribed { topic } => {
                if let Some((_, Pending::Unsubscribe { reply })) = pending {
                    let _ = reply.send(Ok(()));
                } else if unprompted && self.topics.remove(&topic).is_some() {
                    warn!(topic, "the server ended a subscription");
                    let _ = self.events.send(Event::Unsubscribed { topic });
                }
            }
            ServerMessage::Published { message_id, seq } => {
                if let Some((id, Pending::Publish { reply })) = pending {
                    self.unconfirmed.remove(&id);
                    let _ = reply.send(Ok(Published { seq, message_id }));
                }
            }
            ServerMessage::Truncated(truncated) => {
                if let Some(topic) = self.topics.get_mut(&truncated.topic)
                    && topic.group.is_none()
                {
                    let first = truncated.first_available;
                    topic.next_expected = Some(topic.next_expected.map_or(first, |n| n.max(first)));
                }
                let _ = self.events.send(Event::Truncated {
                    topic: truncated.topic,
                    first_available: truncated.first_available,
                });
            }
            ServerMessage::Stalled(stalled) => {
                let _ = self.events.send(Event::Stalled {
                    topic: stalled.topic,
                    seq: stalled.seq,
                });
            }
            ServerMessage::AuthExpiring { expires_at_ms } => {
                let _ = self.events.send(Event::AuthExpiring { expires_at_ms });
                if let Credentials::Password { username, password } =
                    self.config.credentials.clone()
                {
                    let id = self.take_id();
                    self.pending.insert(id, Pending::Relogin);
                    send(
                        socket,
                        Some(id),
                        ClientMessage::Login { username, password },
                    )
                    .await?;
                }
            }
            ServerMessage::LoggedIn { token } => {
                if matches!(pending, Some((_, Pending::Relogin))) {
                    let id = self.take_id();
                    self.pending.insert(id, Pending::Reauth);
                    let auth = ClientMessage::Auth {
                        token,
                        protocol: Some(PROTOCOL_VERSION),
                        session_id: None,
                        clean_start: false,
                        hold_resumed: false,
                    };
                    send(socket, Some(id), auth).await?;
                }
            }
            ServerMessage::Authenticated { .. } => {
                if matches!(pending, Some((_, Pending::Reauth))) {
                    let _ = self.events.send(Event::AuthRefreshed);
                }
            }
            ServerMessage::Error {
                code,
                message,
                retry_after,
            } => self.on_error(pending, code, message, retry_after),
            ServerMessage::Registered {}
            | ServerMessage::Pong {}
            | ServerMessage::User { .. }
            | ServerMessage::Users { .. }
            | ServerMessage::UserDeleted { .. }
            | ServerMessage::TopicInfo { .. }
            | ServerMessage::Topics { .. } => {}
        }
        Ok(())
    }

    /// Routes an error frame to the request it answers, or to the events.
    ///
    /// Args:
    ///     pending: The request it answers, if its `id` matched one.
    ///     code: The error code.
    ///     message: The error text.
    ///     retry_after: Seconds to wait, for limits.
    fn on_error(
        &mut self,
        pending: Option<(u64, Pending)>,
        code: ErrorCode,
        message: String,
        retry_after: Option<u64>,
    ) {
        let error = Error::Server {
            code,
            message: message.clone(),
            retry_after,
        };
        match pending {
            Some((_, Pending::Subscribe { topic })) => {
                self.release(&topic, None);
                if let Some(waiter) = self.subscribe_waiters.remove(&topic) {
                    let _ = waiter.send(Err(error));
                }
            }
            Some((_, Pending::Unsubscribe { reply })) => {
                let _ = reply.send(Err(error));
            }
            Some((id, Pending::Publish { reply })) => {
                self.unconfirmed.remove(&id);
                let _ = reply.send(Err(error));
            }
            Some((_, Pending::Request { reply })) => {
                let _ = reply.send(Err(error));
            }
            Some((_, Pending::Relogin | Pending::Reauth)) | None => {
                let _ = self.events.send(Event::ServerError { code, message });
            }
        }
    }

    /// Hands a delivered message to its subscription, dropping duplicates
    /// and acknowledging as `AckMode` says.
    ///
    /// A message on a topic the server holds for the session that the
    /// application hasn't subscribed to here, or whose subscribe is still
    /// unanswered, is dropped without an acknowledgement: the server keeps
    /// it and replays it when the application's subscribe is handled.
    ///
    /// Args:
    ///     socket: The connection, for acknowledgements.
    ///     delivery: The delivered message.
    ///
    /// Raises:
    ///     (): When a write fails.
    async fn deliver(&mut self, socket: &mut Socket, delivery: Delivery) -> Result<(), ()> {
        let ack = ClientMessage::Ack {
            topic: delivery.topic.clone(),
            seq: delivery.seq,
        };
        if self.unclaimed.contains(&delivery.topic) {
            return Ok(());
        }
        if !self.topics.contains_key(&delivery.topic) {
            self.adopt(&delivery.topic, None);
        }
        let Some(topic) = self.topics.get_mut(&delivery.topic) else {
            if delivery.qos == 1 {
                send(socket, None, ack).await?;
            }
            return Ok(());
        };
        if topic.claiming {
            return Ok(());
        }
        let duplicate = match topic.group {
            Some(_) => topic.seen.contains(delivery.seq),
            None => topic.next_expected.is_some_and(|next| delivery.seq < next),
        };
        if duplicate {
            let acked_before =
                self.config.ack_mode == AckMode::Auto || topic.acked.contains(delivery.seq);
            if delivery.qos == 1 && acked_before {
                send(socket, None, ack).await?;
            }
            return Ok(());
        }
        if topic.group.is_some() {
            topic.seen.insert(delivery.seq);
        } else {
            topic.next_expected = Some(delivery.seq + 1);
        }
        let mut acker = None;
        if delivery.qos == 1 {
            match self.config.ack_mode {
                AckMode::Auto => {
                    topic.acked.insert(delivery.seq);
                    send(socket, None, ack).await?;
                }
                AckMode::Manual => acker = self.acker.upgrade(),
            }
        }
        let buffer = topic.buffer.clone();
        let generation = topic.generation;
        let name = topic
            .owner
            .clone()
            .unwrap_or_else(|| delivery.topic.clone());
        let message = Message {
            topic: delivery.topic,
            seq: delivery.seq,
            message_id: delivery.message_id,
            payload: delivery.payload,
            encoding: delivery.encoding,
            qos: delivery.qos,
            published_at_ms: delivery.published_at_ms,
            acker,
        };
        if buffer.send(message).await.is_err() {
            for released in self.release(&name, Some(generation)) {
                let id = self.take_id();
                let leave = ClientMessage::Unsubscribe { topic: released };
                send(socket, Some(id), leave).await?;
            }
        }
        Ok(())
    }

    /// Stops the client for good: records why, and fails everything waiting.
    ///
    /// Args:
    ///     code: The close code that stopped it, if any.
    fn stop(&mut self, code: Option<CloseCode>) {
        let _ = self.closed.set(code);
        for (_, waiter) in self.subscribe_waiters.drain() {
            let _ = waiter.send(Err(Error::Closed(code)));
        }
        for (_, pending) in self.pending.drain() {
            match pending {
                Pending::Unsubscribe { reply } => {
                    let _ = reply.send(Err(Error::Closed(code)));
                }
                Pending::Publish { reply } => {
                    let _ = reply.send(Err(Error::Closed(code)));
                }
                Pending::Request { reply } => {
                    let _ = reply.send(Err(Error::Closed(code)));
                }
                Pending::Subscribe { .. } | Pending::Relogin | Pending::Reauth => {}
            }
        }
        self.topics.clear();
        self.patterns.clear();
        self.commands.close();
        while let Ok(command) = self.commands.try_recv() {
            fail_command(command, code);
        }
        info!(?code, "client stopped");
    }
}

/// Fails a command that arrived after the client stopped.
///
/// Args:
///     command: The command.
///     code: Why the client stopped.
fn fail_command(command: Command, code: Option<CloseCode>) {
    match command {
        Command::Subscribe { reply, .. } => {
            let _ = reply.send(Err(Error::Closed(code)));
        }
        Command::Unsubscribe { reply, .. } => {
            let _ = reply.send(Err(Error::Closed(code)));
        }
        Command::Publish {
            confirm: Some((_, reply)),
            ..
        } => {
            let _ = reply.send(Err(Error::Closed(code)));
        }
        Command::Request { reply, .. } => {
            let _ = reply.send(Err(Error::Closed(code)));
        }
        _ => {}
    }
}

/// Builds the TLS setup for `wss://`: the public web roots plus any the
/// application added, with `ring` for crypto.
///
/// Args:
///     extra_roots: Certificates to trust besides the public roots.
///
/// Returns:
///     The connector.
fn tls_connector(extra_roots: &[rustls::pki_types::CertificateDer<'static>]) -> Connector {
    let mut roots = rustls::RootCertStore::empty();
    roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
    for root in extra_roots {
        if let Err(e) = roots.add(root.clone()) {
            warn!(%e, "ignoring a root certificate rustls can't use");
        }
    }
    let provider = Arc::new(rustls::crypto::ring::default_provider());
    let config = rustls::ClientConfig::builder_with_provider(provider)
        .with_safe_default_protocol_versions()
        .expect("ring supports the default protocol versions")
        .with_root_certificates(roots)
        .with_no_client_auth();
    Connector::Rustls(Arc::new(config))
}

/// Turns an unexpected reply during connecting into a failure.
///
/// Args:
///     reply: What the server sent instead of the expected reply.
///
/// Returns:
///     The failure; retryable only for the server's temporary refusals.
fn refusal(reply: ServerMessage) -> ConnectFailure {
    match reply {
        ServerMessage::Error {
            code,
            message,
            retry_after,
        } => {
            let error = Error::Server {
                code,
                message,
                retry_after,
            };
            ConnectFailure {
                retry: error.is_temporary(),
                error,
            }
        }
        other => ConnectFailure {
            error: Error::Protocol(format!("{other:?}")),
            retry: true,
        },
    }
}

/// How long to wait for anything from the server before giving up on the
/// connection: the server's idle timeout plus one heartbeat, since it pings
/// at that interval.
///
/// Args:
///     limits: The server's advertised limits.
///
/// Returns:
///     The limit.
fn silence_limit(limits: &Limits) -> Duration {
    Duration::from_secs(
        limits
            .idle_timeout_secs
            .max(1)
            .saturating_add(limits.heartbeat_secs),
    )
}

/// Sends one request.
///
/// Args:
///     socket: The connection.
///     id: The request id, if a reply is expected.
///     message: The request.
///
/// Raises:
///     (): When the write fails.
async fn send(socket: &mut Socket, id: Option<u64>, message: ClientMessage) -> Result<(), ()> {
    let frame = ClientFrame {
        id: id.map(RequestId::Number),
        message,
    };
    let text = serde_json::to_string(&frame).map_err(|_| ())?;
    socket
        .send(WsMessage::Text(text.into()))
        .await
        .map_err(|e| {
            debug!(%e, "write failed");
        })
}