wse 3.0.0

WebSocket engine for reactive user interfaces: topics with message recovery, presence, clustering without a broker, JWT, end-to-end encryption and signed messages.
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
//! One peer link: writer, reader, dispatch and heartbeat tasks, the outbound
//! session and its reconnect loop.

use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;

use bytes::{Bytes, BytesMut};
use rustls::pki_types::ServerName;
use socket2::{SockRef, TcpKeepalive};
use tokio::io::{AsyncReadExt, AsyncWriteExt, BufReader};
use tokio::net::TcpStream;
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;

use super::protocol::write_framed;
use super::{
    CAP_RESUME, ClusterCommand, ClusterContext, ClusterFrame, ClusterMetrics, ClusterTlsConfig,
    DispatchItem, HEARTBEAT_INTERVAL_SECS, HEARTBEAT_TIMEOUT_SECS, InterestUpdate, Link,
    LinkRefused, MAX_BATCH_BYTES, MAX_FRAME_SIZE, MAX_RESUME_POINTS, PEER_DISPATCH_QUEUE_CAP,
    PEER_SESSION_GENERATION, PRESENCE_ROUND, PRESENCE_ROUND_FIRST, PRESENCE_ROUND_LAST,
    PROTOCOL_VERSION, PeerInfo, PeerRegistration, PresencePeerFrame, RESUME_TIMEOUT, ResumePoint,
    RoundPart, claim_link, decode_frame, encode_hello, encode_peer_announce, encode_peer_list,
    encode_ping, encode_pong, encode_replayed, encode_resume, epoch_ms, local_capabilities,
    negotiate_capabilities, negotiate_version, release_link,
};
use crate::reliability::{CircuitBreaker, ExponentialBackoff};

// ---------------------------------------------------------------------------
// Per-peer writer task (write coalescing: recv + try_recv drain)
// ---------------------------------------------------------------------------

pub(super) async fn peer_writer<W: AsyncWriteExt + Unpin>(
    mut rx: mpsc::Receiver<Bytes>,
    mut writer: W,
    cancel: CancellationToken,
    metrics: Arc<ClusterMetrics>,
) {
    // Whichever of a link's tasks ends first ends the link: its cleanup then
    // runs and the dialer reconnects.
    let _end_link = cancel.clone().drop_guard();
    let mut buf = BytesMut::with_capacity(MAX_BATCH_BYTES);

    loop {
        let first = tokio::select! {
            msg = rx.recv() => msg,
            () = cancel.cancelled() => break,
        };

        let Some(first) = first else { break };

        // Frame the first message
        write_framed(&mut buf, &first);

        // Drain everything else ready (non-blocking coalescing)
        while buf.len() < MAX_BATCH_BYTES {
            match rx.try_recv() {
                Ok(data) => write_framed(&mut buf, &data),
                Err(_) => break,
            }
        }

        let bytes_count = buf.len() as u64;
        if writer.write_all(&buf).await.is_err() {
            break;
        }
        // flush() is required for TLS -- rustls buffers encrypted data internally
        // and only writes to the TCP socket on flush. For plain TCP this is a no-op.
        if writer.flush().await.is_err() {
            break;
        }
        metrics.bytes_sent.fetch_add(bytes_count, Ordering::Relaxed);
        buf.clear();
    }
}

// ---------------------------------------------------------------------------
// Per-peer reader task
// ---------------------------------------------------------------------------

/// Reads frames from a peer TCP connection. Decoded MSG payloads are pushed to
/// a dispatch channel (non-blocking) so fan-out never stalls TCP reads.
/// BufReader (64KB) reduces read syscalls from 2/frame to ~1 per 650 frames.
pub(super) async fn peer_reader<R: AsyncReadExt + Unpin>(
    reader: R,
    peer_write_tx: mpsc::Sender<Bytes>,
    peer_addr: String,
    ctx: ClusterContext,
    dispatch_tx: mpsc::Sender<DispatchItem>,
    cancel: CancellationToken,
    session: (Arc<AtomicU64>, u64),
) {
    let _end_link = cancel.clone().drop_guard();
    let (last_activity, generation) = session;
    // BufReader: bulk reads from TCP into 64KB buffer, serves read_exact from memory
    let mut reader = BufReader::with_capacity(65_536, reader);
    let mut len_buf = [0u8; 4];

    loop {
        // Read frame length (4 bytes, big-endian)
        let read_result = tokio::select! {
            r = reader.read_exact(&mut len_buf) => r,
            () = cancel.cancelled() => break,
        };
        if read_result.is_err() {
            break;
        }

        let frame_len = u32::from_be_bytes(len_buf) as usize;
        if frame_len == 0 || frame_len > MAX_FRAME_SIZE {
            tracing::warn!("[WSE-Cluster] Invalid frame size: {frame_len}, disconnecting");
            break;
        }

        // Read frame payload
        let mut frame_buf = BytesMut::zeroed(frame_len);
        let read_result = tokio::select! {
            r = reader.read_exact(&mut frame_buf) => r,
            () = cancel.cancelled() => break,
        };
        if read_result.is_err() {
            break;
        }

        ctx.metrics
            .bytes_received
            .fetch_add((4 + frame_len) as u64, Ordering::Relaxed);

        // Decode and dispatch
        match decode_frame(frame_buf) {
            Some(ClusterFrame::Msg {
                topic,
                payload,
                recovery,
            }) => {
                last_activity.store(epoch_ms(), Ordering::Relaxed);
                // Bounded dispatch: shed (don't block the reader) when local fan-out
                // falls behind, so a fast peer cannot grow an unbounded backlog -> OOM.
                // Shed MSGs stay recoverable via the recovery buffer; the reader remains
                // responsive to control/heartbeat frames on the same loop.
                if dispatch_tx.try_send((topic, payload, recovery)).is_err() {
                    ctx.metrics.messages_dropped.fetch_add(1, Ordering::Relaxed);
                }
            }
            Some(ClusterFrame::Replayed {
                topic,
                payload,
                position,
            }) => {
                last_activity.store(epoch_ms(), Ordering::Relaxed);
                // What this node missed while the link was down: delivered,
                // so it waits for room instead of being shed.
                if !to_manager(&dispatch_tx, (topic, payload, Some(position)), &cancel).await {
                    break;
                }
            }
            Some(ClusterFrame::Resume { .. }) => {
                // Taken during the handshake only.
            }
            Some(ClusterFrame::Ping) => {
                last_activity.store(epoch_ms(), Ordering::Relaxed);
                let mut pong = BytesMut::new();
                encode_pong(&mut pong);
                let _ = peer_write_tx.try_send(pong.freeze());
            }
            Some(ClusterFrame::Pong) => {
                last_activity.store(epoch_ms(), Ordering::Relaxed);
            }
            Some(ClusterFrame::Shutdown) => {
                tracing::info!("[WSE-Cluster] Peer sent SHUTDOWN");
                break;
            }
            Some(ClusterFrame::Drain) => {
                tracing::info!("[WSE-Cluster] Peer entering drain mode (lame duck)");
                // Stop routing NEW messages to the draining peer (clear its remote
                // interest) but keep the connection open for in-flight delivery.
                last_activity.store(epoch_ms(), Ordering::Relaxed);
                let update = InterestUpdate::Drain {
                    peer_addr: peer_addr.clone(),
                };
                if !to_manager(&ctx.interest_tx, update, &cancel).await {
                    break;
                }
            }
            Some(ClusterFrame::Hello { .. }) => {
                // Unexpected HELLO after handshake, ignore
            }
            Some(ClusterFrame::Unknown { msg_type }) => {
                last_activity.store(epoch_ms(), Ordering::Relaxed);
                ctx.metrics
                    .unknown_message_types
                    .fetch_add(1, Ordering::Relaxed);
                tracing::warn!(
                    "[WSE-Cluster] Unknown message type 0x{msg_type:02x}, skipping frame"
                );
            }
            Some(ClusterFrame::Sub { topic }) => {
                last_activity.store(epoch_ms(), Ordering::Relaxed);
                let update = InterestUpdate::Sub {
                    peer_addr: peer_addr.clone(),
                    topic,
                };
                if !to_manager(&ctx.interest_tx, update, &cancel).await {
                    break;
                }
            }
            Some(ClusterFrame::Unsub { topic }) => {
                last_activity.store(epoch_ms(), Ordering::Relaxed);
                let update = InterestUpdate::Unsub {
                    peer_addr: peer_addr.clone(),
                    topic,
                };
                if !to_manager(&ctx.interest_tx, update, &cancel).await {
                    break;
                }
            }
            Some(ClusterFrame::Resync {
                topics,
                is_continuation,
                more,
            }) => {
                last_activity.store(epoch_ms(), Ordering::Relaxed);
                let update = InterestUpdate::Resync {
                    peer_addr: peer_addr.clone(),
                    topics,
                    is_continuation,
                    more,
                    generation,
                };
                if !to_manager(&ctx.interest_tx, update, &cancel).await {
                    break;
                }
            }
            Some(ClusterFrame::PeerAnnounce { addr }) => {
                last_activity.store(epoch_ms(), Ordering::Relaxed);
                // Notify cluster_manager to connect to new peer
                let _ = ctx.new_peer_tx.send(addr.clone());
                // Gossip to all other peers (excluding sender)
                let _ = ctx.cmd_tx.send(ClusterCommand::GossipPeerAnnounce {
                    addr,
                    exclude_peer: peer_addr.clone(),
                });
            }
            Some(ClusterFrame::PeerList { addrs }) => {
                last_activity.store(epoch_ms(), Ordering::Relaxed);
                for addr in addrs {
                    let _ = ctx.new_peer_tx.send(addr);
                }
            }
            Some(ClusterFrame::PresenceUpdate {
                topic,
                user_id,
                action,
                data,
                updated_at,
            }) => {
                last_activity.store(epoch_ms(), Ordering::Relaxed);
                let frame = PresencePeerFrame::Update {
                    origin: peer_addr.clone(),
                    topic,
                    user_id,
                    action,
                    data,
                    updated_at,
                    generation,
                };
                if !to_manager(&ctx.presence_tx, frame, &cancel).await {
                    break;
                }
            }
            Some(ClusterFrame::PresenceFull { entries, flags }) => {
                last_activity.store(epoch_ms(), Ordering::Relaxed);
                let round = (flags & PRESENCE_ROUND != 0).then_some(RoundPart {
                    first: flags & PRESENCE_ROUND_FIRST != 0,
                    last: flags & PRESENCE_ROUND_LAST != 0,
                });
                let frame = PresencePeerFrame::Full {
                    origin: peer_addr.clone(),
                    entries,
                    round,
                    generation,
                };
                if !to_manager(&ctx.presence_tx, frame, &cancel).await {
                    break;
                }
            }
            None => {
                tracing::error!("[WSE-Cluster] Failed to decode frame, disconnecting");
                break;
            }
        }
    }
}

/// Hand an update to the manager, waiting while its queue is full: a peer
/// sending faster than the manager keeps up is slowed down at its socket,
/// not buffered without bound. False once the link ends.
async fn to_manager<T>(tx: &mpsc::Sender<T>, item: T, cancel: &CancellationToken) -> bool {
    tokio::select! {
        sent = tx.send(item) => sent.is_ok(),
        () = cancel.cancelled() => false,
    }
}

/// Tell the manager a link ended. It must hear of every one (its interest
/// and presence go with it), so this waits for room, except on shutdown:
/// the manager reads no more then.
pub(super) async fn tell_disconnected(
    ctx: &ClusterContext,
    peer_addr: String,
    generation: u64,
    global_cancel: &CancellationToken,
) {
    let update = InterestUpdate::PeerDisconnected {
        peer_addr,
        generation,
    };
    tokio::select! {
        _ = ctx.interest_tx.send(update) => {}
        () = global_cancel.cancelled() => {}
    }
}

// ---------------------------------------------------------------------------
// Per-peer dispatch task: receives decoded (topic, payload) from the reader,
// stores it for recovery and hands it to the lanes that deliver it. Decouples
// TCP reads from the delivery.
// ---------------------------------------------------------------------------

pub(super) async fn peer_dispatch_task(
    mut dispatch_rx: mpsc::Receiver<DispatchItem>,
    ctx: ClusterContext,
    cancel: CancellationToken,
) {
    let _end_link = cancel.clone().drop_guard();
    let mut batch: Vec<DispatchItem> = Vec::with_capacity(64);

    loop {
        // Wait for first message (blocking)
        let msg = tokio::select! {
            m = dispatch_rx.recv() => m,
            () = cancel.cancelled() => break,
        };
        let Some(first) = msg else { break };
        batch.push(first);

        // Drain any additional queued messages (non-blocking)
        while batch.len() < 256 {
            match dispatch_rx.try_recv() {
                Ok(m) => batch.push(m),
                Err(_) => break,
            }
        }

        // Queue-group deliveries, counted here; the lanes count the rest.
        let mut total_delivered = 0u64;
        let mut total_dropped = 0u64;

        for (topic, payload, rec) in batch.drain(..) {
            // Store the unframed payload in the foreign recovery buffer
            // (cross-node recovery), the form the local buffer stores too.
            // One this node has already (a peer catching it up sends from
            // where it stands with nothing missing) is not delivered again.
            if let (Some(rm), Some((epoch, offset))) = (&ctx.recovery, rec)
                && !rm.push_foreign(&topic, payload.clone(), epoch, offset)
            {
                continue;
            }

            // Per-topic message count for the Prometheus top-N metrics.
            crate::server::count_message(&ctx.topic_message_counts, &topic);

            // Queue groups: one member each, round-robin, without the
            // recovery stamp (a member sees only some of the offsets).
            let (d, f) = ctx
                .fanout
                .queue_groups(
                    &ctx.queue_groups,
                    &topic,
                    || crate::server::Outgoing::text(crate::server::unstamped(&payload)),
                    ctx.max_outbound,
                    ctx.slow_drops.shard(0),
                )
                .await;
            total_delivered += d;
            total_dropped += f;

            ctx.fanout
                .publish(
                    topic,
                    || crate::server::Outgoing::text(payload),
                    Some(&ctx.metrics),
                )
                .await;
        }

        if total_delivered > 0 {
            ctx.metrics.add_delivered(total_delivered);
        }
        if total_dropped > 0 {
            ctx.metrics.add_dropped(total_dropped);
        }
    }
}

// ---------------------------------------------------------------------------
// Heartbeat task
// ---------------------------------------------------------------------------

pub(super) async fn heartbeat_task(
    peer_write_tx: mpsc::Sender<Bytes>,
    cancel: CancellationToken,
    last_activity: Arc<AtomicU64>,
) {
    let _end_link = cancel.clone().drop_guard();
    let mut interval = tokio::time::interval(Duration::from_secs(HEARTBEAT_INTERVAL_SECS));
    let timeout_ms = HEARTBEAT_TIMEOUT_SECS * 1000;
    loop {
        tokio::select! {
            _ = interval.tick() => {
                // Check if peer is dead (no activity for HEARTBEAT_TIMEOUT_SECS)
                let last = last_activity.load(Ordering::Relaxed);
                if epoch_ms().saturating_sub(last) > timeout_ms {
                    tracing::warn!("[WSE-Cluster] Peer heartbeat timeout ({}s no activity)", timeout_ms / 1000);
                    cancel.cancel();
                    break;
                }
                let mut ping = BytesMut::new();
                encode_ping(&mut ping);
                match peer_write_tx.try_send(ping.freeze()) {
                    Ok(()) => {}
                    Err(mpsc::error::TrySendError::Full(_)) => {
                        // Write channel full -- ping couldn't be queued.
                        // Do NOT treat as liveness evidence: a TCP-dead peer with a full
                        // send buffer also triggers Full, and updating last_activity here
                        // would prevent the timeout from ever firing.
                    }
                    Err(mpsc::error::TrySendError::Closed(_)) => break,
                }
            }
            () = cancel.cancelled() => break,
        }
    }
}

// ---------------------------------------------------------------------------
// Handshake helpers
// ---------------------------------------------------------------------------

/// The catch-up on a new link (CAP_RESUME): this node's RESUME request goes
/// out while the peer's comes in, so a large one each way cannot stall on
/// the other. The peer's request, or None when it did not come whole in time.
pub(super) async fn exchange_resume<R, W>(
    reader: &mut R,
    writer: &mut W,
    ctx: &ClusterContext,
) -> Option<Vec<ResumePoint>>
where
    R: AsyncReadExt + Unpin,
    W: AsyncWriteExt + Unpin,
{
    let points = ctx
        .recovery
        .as_ref()
        .map(|rm| rm.foreign_positions())
        .unwrap_or_default();
    let mut request = BytesMut::new();
    for frame in encode_resume(&points) {
        write_framed(&mut request, &frame);
    }
    let send = async {
        writer.write_all(&request).await?;
        writer.flush().await
    };
    let receive = tokio::time::timeout(RESUME_TIMEOUT, async {
        let mut wanted: Vec<ResumePoint> = Vec::new();
        loop {
            let Ok(Some(ClusterFrame::Resume { points, last })) = read_frame(reader).await else {
                return None;
            };
            if wanted.len() + points.len() > MAX_RESUME_POINTS {
                return None;
            }
            wanted.extend(points);
            if last {
                return Some(wanted);
            }
        }
    });
    let (sent, received) = tokio::join!(send, receive);
    sent.ok()?;
    received.ok().flatten()
}

/// What the peer missed of the streams this node publishes, after where its
/// RESUME request says it stands: written to the link before anything live
/// goes to it.
pub(super) async fn write_replay<W>(
    writer: &mut W,
    wanted: &[ResumePoint],
    ctx: &ClusterContext,
) -> std::io::Result<u64>
where
    W: AsyncWriteExt + Unpin,
{
    let Some(rm) = &ctx.recovery else {
        return Ok(0);
    };
    let mut out = BytesMut::with_capacity(MAX_BATCH_BYTES);
    let mut frame = BytesMut::new();
    let (mut sent, mut bytes) = (0u64, 0u64);
    for (topic, epoch, after) in wanted {
        for (offset, data) in rm.published_after(topic, *epoch, *after) {
            let Ok(payload) = std::str::from_utf8(&data) else {
                continue;
            };
            frame.clear();
            if !encode_replayed(&mut frame, topic, payload, *epoch, offset) {
                continue;
            }
            write_framed(&mut out, &frame);
            sent += 1;
            if out.len() >= MAX_BATCH_BYTES {
                writer.write_all(&out).await?;
                bytes += out.len() as u64;
                out.clear();
            }
        }
    }
    if !out.is_empty() {
        writer.write_all(&out).await?;
        bytes += out.len() as u64;
    }
    writer.flush().await?;
    ctx.metrics.messages_sent.fetch_add(sent, Ordering::Relaxed);
    ctx.metrics.bytes_sent.fetch_add(bytes, Ordering::Relaxed);
    Ok(sent)
}

/// Hand a live link to the manager, which routes publishes, interest and
/// presence to it from then on, starting with this node's RESYNC and
/// presence: they wait in the link's channel until its writer runs. With a
/// catch-up owed, the peer first gets what it missed, taken from the
/// recovery buffers once the manager routes to the link: every message is
/// in one or the other, and one in both is delivered there once. False when
/// the link failed meanwhile.
pub(super) async fn go_live<W>(
    writer: &mut W,
    peer_reg_tx: &mpsc::UnboundedSender<PeerRegistration>,
    link: (&str, mpsc::Sender<Bytes>, u32),
    wanted: Option<Vec<ResumePoint>>,
    ctx: &ClusterContext,
) -> bool
where
    W: AsyncWriteExt + Unpin,
{
    let (peer_addr, write_tx, negotiated_caps) = link;
    let (taken, routed) = match wanted {
        Some(_) => {
            let (tx, rx) = tokio::sync::oneshot::channel();
            (Some(tx), Some(rx))
        }
        None => (None, None),
    };
    let _ = peer_reg_tx.send(PeerRegistration::Register {
        peer_addr: peer_addr.to_owned(),
        write_tx,
        negotiated_caps,
        taken,
    });
    let (Some(wanted), Some(routed)) = (wanted, routed) else {
        return true;
    };
    // An Err: the manager stopped, and the cluster with it.
    let _ = routed.await;
    match write_replay(writer, &wanted, ctx).await {
        Ok(0) => true,
        Ok(sent) => {
            tracing::info!("[WSE-Cluster] Sent {peer_addr} the {sent} messages it missed");
            true
        }
        Err(_) => false,
    }
}

/// Read one length-prefixed frame (the HELLO of a handshake). `Ok(None)` is a
/// frame of an impossible size or one that does not decode.
pub(super) async fn read_frame<R: AsyncReadExt + Unpin>(
    reader: &mut R,
) -> std::io::Result<Option<ClusterFrame>> {
    let mut len_buf = [0u8; 4];
    reader.read_exact(&mut len_buf).await?;
    let frame_len = u32::from_be_bytes(len_buf) as usize;
    if frame_len == 0 || frame_len > MAX_FRAME_SIZE {
        return Ok(None);
    }
    let mut frame_buf = BytesMut::zeroed(frame_len);
    reader.read_exact(&mut frame_buf).await?;
    Ok(decode_frame(frame_buf))
}

/// The TLS server name for a peer address: its IP address or DNS name without
/// the port. IPv6 addresses come bracketed (`[::1]:9000`).
pub(super) fn server_name_for(addr: &str) -> Option<ServerName<'static>> {
    if let Ok(sock) = addr.parse::<std::net::SocketAddr>() {
        return Some(ServerName::IpAddress(sock.ip().into()));
    }
    let (host, port) = addr.rsplit_once(':')?;
    port.parse::<u16>().ok()?;
    ServerName::try_from(host.to_owned()).ok()
}

// ---------------------------------------------------------------------------
// Outbound link: handshake, session and the dial loop
// ---------------------------------------------------------------------------

/// How an outbound attempt ended, and so what the dial loop does next.
enum Outcome {
    /// A session ran and the link closed: dial again.
    Ended,
    /// The handshake failed: back off, counting a failure.
    Failed,
    /// The peer is already linked to this node through a link that wins:
    /// back off and try again later, in case that link goes away.
    Duplicate,
    /// The address is this node's own: stop dialing it.
    SelfLink,
    /// The cluster is shutting down.
    Shutdown,
}

/// HELLO exchange, then the session until the link closes. Generic over
/// AsyncRead/AsyncWrite for both plaintext TCP and TLS streams.
async fn run_peer_session<R, W>(
    mut reader: R,
    mut writer: W,
    ids: (&str, &str),
    ctx: &ClusterContext,
    peer_reg_tx: &mpsc::UnboundedSender<PeerRegistration>,
    global_cancel: &CancellationToken,
) -> Outcome
where
    R: AsyncReadExt + Unpin + Send + 'static,
    W: AsyncWriteExt + Unpin + Send + 'static,
{
    let (peer_addr, instance_id) = ids;

    let local_caps = local_capabilities(ctx);
    let mut hello_buf = BytesMut::new();
    encode_hello(&mut hello_buf, instance_id, local_caps);
    let mut frame = BytesMut::new();
    write_framed(&mut frame, &hello_buf);
    if writer.write_all(&frame).await.is_err() || writer.flush().await.is_err() {
        tracing::warn!("[WSE-Cluster] Failed to send HELLO to {peer_addr}");
        return Outcome::Failed;
    }

    let peer_hello =
        match tokio::time::timeout(Duration::from_secs(5), read_frame(&mut reader)).await {
            Ok(Ok(frame)) => frame,
            Ok(Err(_)) => {
                // A peer closes without answering when it keeps another link to
                // this node, or when it is an older version that only accepts
                // links from the lower instance id.
                tracing::debug!("[WSE-Cluster] {peer_addr} closed the link during the handshake");
                return Outcome::Failed;
            }
            Err(_) => {
                tracing::warn!("[WSE-Cluster] No HELLO from {peer_addr} within 5s");
                return Outcome::Failed;
            }
        };

    let (peer_instance_id, peer_version, peer_caps) = match peer_hello {
        Some(ClusterFrame::Hello {
            instance_id: peer_id,
            protocol_version,
            capabilities,
        }) => match negotiate_version(PROTOCOL_VERSION, protocol_version) {
            Some(v) => (peer_id, v, negotiate_capabilities(local_caps, capabilities)),
            None => {
                tracing::warn!(
                    "[WSE-Cluster] Version mismatch with {peer_addr}: local={PROTOCOL_VERSION}, remote={protocol_version}"
                );
                return Outcome::Failed;
            }
        },
        _ => {
            tracing::warn!("[WSE-Cluster] Invalid HELLO from {peer_addr}");
            return Outcome::Failed;
        }
    };

    let peer_cancel = global_cancel.child_token();
    let session_gen = PEER_SESSION_GENERATION.fetch_add(1, Ordering::AcqRel);
    let last_activity = Arc::new(AtomicU64::new(epoch_ms()));
    let link = Link {
        dialer: instance_id.to_owned(),
        session: session_gen,
        cancel: peer_cancel.clone(),
        activity: Arc::clone(&last_activity),
    };
    match claim_link(&ctx.links, instance_id, &peer_instance_id, link) {
        Ok(()) => {}
        Err(LinkRefused::SelfLink) => {
            tracing::info!("[WSE-Cluster] {peer_addr} is this node's own address; not dialing it");
            return Outcome::SelfLink;
        }
        Err(LinkRefused::Duplicate) => {
            tracing::debug!(
                "[WSE-Cluster] Already linked to instance {peer_instance_id}; keeping that link instead of {peer_addr}"
            );
            return Outcome::Duplicate;
        }
    }

    // Catch-up: each node first says where it stands in the other's streams.
    let wanted = if peer_caps & CAP_RESUME != 0 {
        let Some(wanted) = exchange_resume(&mut reader, &mut writer, ctx).await else {
            tracing::warn!("[WSE-Cluster] No RESUME from {peer_addr} within {RESUME_TIMEOUT:?}");
            release_link(&ctx.links, &peer_instance_id, session_gen);
            return Outcome::Failed;
        };
        Some(wanted)
    } else {
        None
    };

    tracing::info!(
        "[WSE-Cluster] Connected to {peer_addr} (v{peer_version}, caps=0x{peer_caps:08x})"
    );
    ctx.metrics.connected_peers.fetch_add(1, Ordering::Relaxed);
    ctx.metrics.peer_info.insert(
        peer_addr.to_owned(),
        PeerInfo {
            address: peer_addr.to_owned(),
            instance_id: peer_instance_id.clone(),
            capabilities: peer_caps,
            connected_at: epoch_ms(),
        },
    );

    // This node's interest (RESYNC) and presence follow from the manager once
    // the link is registered, in order with every change after them.

    // PEER_ANNOUNCE with our cluster address (discovery mode)
    if let Some(ref our_addr) = ctx.cluster_addr {
        let mut announce_buf = BytesMut::new();
        encode_peer_announce(&mut announce_buf, our_addr);
        let mut announce_frame = BytesMut::new();
        write_framed(&mut announce_frame, &announce_buf);
        let _ = writer.write_all(&announce_frame).await;
        let _ = writer.flush().await;
    }

    // PEER_LIST with all known peers
    let addrs: Vec<String> = ctx.known_peers.iter().map(|r| r.key().clone()).collect();
    if !addrs.is_empty() {
        let mut list_buf = BytesMut::new();
        encode_peer_list(&mut list_buf, &addrs);
        let mut list_frame = BytesMut::new();
        write_framed(&mut list_frame, &list_buf);
        let _ = writer.write_all(&list_frame).await;
        let _ = writer.flush().await;
    }

    let (peer_write_tx, peer_write_rx) = mpsc::channel::<Bytes>(10_000);

    let (dispatch_tx, dispatch_rx) = mpsc::channel::<DispatchItem>(PEER_DISPATCH_QUEUE_CAP);
    let dispatch_handle = tokio::spawn(peer_dispatch_task(
        dispatch_rx,
        ctx.clone(),
        peer_cancel.clone(),
    ));

    let reader_handle = tokio::spawn(peer_reader(
        reader,
        peer_write_tx.clone(),
        peer_addr.to_owned(),
        ctx.clone(),
        dispatch_tx,
        peer_cancel.clone(),
        (last_activity.clone(), session_gen),
    ));

    if !go_live(
        &mut writer,
        peer_reg_tx,
        (peer_addr, peer_write_tx.clone(), peer_caps),
        wanted,
        ctx,
    )
    .await
    {
        peer_cancel.cancel();
    }

    let writer_handle = tokio::spawn(peer_writer(
        peer_write_rx,
        writer,
        peer_cancel.clone(),
        ctx.metrics.clone(),
    ));

    let heartbeat_handle = tokio::spawn(heartbeat_task(
        peer_write_tx,
        peer_cancel.clone(),
        last_activity,
    ));

    // The link ends when any of its tasks ends, when a better duplicate
    // replaces it, or on shutdown (the token is a child of the global one).
    peer_cancel.cancelled().await;
    let _ = writer_handle.await;
    let _ = reader_handle.await;
    let _ = dispatch_handle.await;
    let _ = heartbeat_handle.await;

    let _ = peer_reg_tx.send(PeerRegistration::Deregister {
        peer_addr: peer_addr.to_owned(),
    });
    ctx.metrics.connected_peers.fetch_sub(1, Ordering::Relaxed);
    ctx.metrics.peer_info.remove(peer_addr);
    release_link(&ctx.links, &peer_instance_id, session_gen);
    tell_disconnected(ctx, peer_addr.to_owned(), session_gen, global_cancel).await;
    tracing::info!("[WSE-Cluster] Disconnected from {peer_addr}");

    if global_cancel.is_cancelled() {
        Outcome::Shutdown
    } else {
        Outcome::Ended
    }
}

/// Consecutive failed connects after which a peer learned through gossip is
/// given up: it has most likely left the cluster. Static peers are dialed for
/// as long as the node runs.
const MAX_UNREACHABLE_ATTEMPTS: u32 = 10;

async fn peer_connection_task(
    peer_addr: String,
    instance_id: String,
    is_static: bool,
    ctx: ClusterContext,
    tls_config: Option<Arc<arc_swap::ArcSwap<ClusterTlsConfig>>>,
    peer_reg_tx: mpsc::UnboundedSender<PeerRegistration>,
    global_cancel: CancellationToken,
) {
    let mut backoff = ExponentialBackoff::new();
    let mut breaker = CircuitBreaker::new();
    let mut unreachable = 0u32;

    loop {
        if global_cancel.is_cancelled() {
            break;
        }
        if !is_static && unreachable >= MAX_UNREACHABLE_ATTEMPTS {
            tracing::info!(
                "[WSE-Cluster] Giving up on {peer_addr}: unreachable for {unreachable} attempts"
            );
            ctx.known_peers.remove(&peer_addr);
            break;
        }

        if !breaker.can_execute() {
            let delay = breaker.reset_timeout;
            tracing::warn!(
                "[WSE-Cluster] Circuit open for {peer_addr}, waiting {:.0}s",
                delay.as_secs_f64()
            );
            tokio::select! {
                () = tokio::time::sleep(delay) => continue,
                () = global_cancel.cancelled() => break,
            }
        }

        let connected =
            tokio::time::timeout(Duration::from_secs(5), TcpStream::connect(&peer_addr)).await;
        let stream = match connected {
            Ok(Ok(stream)) => stream,
            failed => {
                unreachable += 1;
                breaker.record_failure();
                ctx.metrics.reconnect_count.fetch_add(1, Ordering::Relaxed);
                let delay = backoff.next_delay();
                match failed {
                    Ok(Err(e)) => tracing::warn!(
                        "[WSE-Cluster] Connect failed {peer_addr}: {e}. Retry in {:.1}s",
                        delay.as_secs_f64()
                    ),
                    _ => tracing::warn!(
                        "[WSE-Cluster] Connect timeout {peer_addr}. Retry in {:.1}s",
                        delay.as_secs_f64()
                    ),
                }
                tokio::select! {
                    () = tokio::time::sleep(delay) => continue,
                    () = global_cancel.cancelled() => break,
                }
            }
        };
        unreachable = 0;

        // TCP_NODELAY + keepalive + larger buffers for throughput
        let _ = stream.set_nodelay(true);
        let keepalive = TcpKeepalive::new()
            .with_time(Duration::from_secs(10))
            .with_interval(Duration::from_secs(5));
        let sock_ref = SockRef::from(&stream);
        let _ = sock_ref.set_tcp_keepalive(&keepalive);
        let _ = sock_ref.set_recv_buffer_size(262_144);
        let _ = sock_ref.set_send_buffer_size(262_144);

        // The TLS config is read per attempt, so a reload applies to the next dial.
        let tls_snap = tls_config.as_ref().map(|holder| holder.load());
        let outcome = if let Some(ref tls) = tls_snap {
            let Some(server_name) = server_name_for(&peer_addr) else {
                tracing::error!(
                    "[WSE-Cluster] {peer_addr} is not a host:port address usable for TLS; not dialing it"
                );
                break;
            };
            match tokio::time::timeout(
                Duration::from_secs(5),
                tls.connector.connect(server_name, stream),
            )
            .await
            {
                Ok(Ok(tls_stream)) => {
                    let (reader, writer) = tokio::io::split(tls_stream);
                    run_peer_session(
                        reader,
                        writer,
                        (&peer_addr, &instance_id),
                        &ctx,
                        &peer_reg_tx,
                        &global_cancel,
                    )
                    .await
                }
                Ok(Err(e)) => {
                    tracing::warn!("[WSE-Cluster] TLS handshake failed {peer_addr}: {e}");
                    Outcome::Failed
                }
                Err(_) => {
                    tracing::warn!("[WSE-Cluster] TLS handshake timeout {peer_addr}");
                    Outcome::Failed
                }
            }
        } else {
            let (reader, writer) = stream.into_split();
            run_peer_session(
                reader,
                writer,
                (&peer_addr, &instance_id),
                &ctx,
                &peer_reg_tx,
                &global_cancel,
            )
            .await
        };

        let delay = match outcome {
            Outcome::Shutdown | Outcome::SelfLink => break,
            Outcome::Ended => {
                breaker.record_success();
                backoff.reset();
                ctx.metrics.reconnect_count.fetch_add(1, Ordering::Relaxed);
                backoff.next_delay()
            }
            Outcome::Failed => {
                breaker.record_failure();
                ctx.metrics.reconnect_count.fetch_add(1, Ordering::Relaxed);
                backoff.next_delay()
            }
            // The peer answered: no failure, but no need to redial soon.
            Outcome::Duplicate => backoff.next_delay(),
        };
        tracing::debug!(
            "[WSE-Cluster] Dialing {peer_addr} again in {:.1}s",
            delay.as_secs_f64()
        );
        tokio::select! {
            () = tokio::time::sleep(delay) => {}
            () = global_cancel.cancelled() => break,
        }
    }
}

/// Shared context for spawning peer connection tasks.
pub(super) struct PeerSpawnCtx {
    pub(super) instance_id: String,
    pub(super) ctx: ClusterContext,
    pub(super) global_cancel: CancellationToken,
    pub(super) tls_config: Option<Arc<arc_swap::ArcSwap<ClusterTlsConfig>>>,
    pub(super) peer_reg_tx: mpsc::UnboundedSender<PeerRegistration>,
}

impl PeerSpawnCtx {
    /// Start dialing `peer_addr`. A static peer (from the configuration) is
    /// dialed for as long as the node runs; one learned through gossip is
    /// given up after it stays unreachable.
    pub(super) fn spawn(&self, peer_addr: String, is_static: bool) -> tokio::task::JoinHandle<()> {
        tokio::spawn(peer_connection_task(
            peer_addr,
            self.instance_id.clone(),
            is_static,
            self.ctx.clone(),
            self.tls_config.clone(),
            self.peer_reg_tx.clone(),
            self.global_cancel.clone(),
        ))
    }
}