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
//! Inbound peer links: their admission, and the handshake and session of a
//! peer that dialed us, on the cluster port or, in single-port mode, on the
//! WebSocket port.

use std::collections::HashMap;
use std::net::{IpAddr, SocketAddr};
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;

use bytes::{Bytes, BytesMut};
use socket2::SockRef;
use tokio::io::AsyncWriteExt;
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;

use super::peer::{
    exchange_resume, go_live, heartbeat_task, peer_dispatch_task, peer_reader, peer_writer,
    read_frame, tell_disconnected,
};
use super::protocol::write_framed;
use super::{
    CAP_RESUME, ClusterContext, ClusterFrame, ClusterTlsConfig, DispatchItem, Link, LinkRefused,
    MAX_CLUSTER_PEERS, PEER_DISPATCH_QUEUE_CAP, PEER_SESSION_GENERATION, PROTOCOL_VERSION,
    PeerInfo, PeerRegistration, RESUME_TIMEOUT, claim_link, encode_hello, encode_peer_announce,
    encode_peer_list, epoch_ms, local_capabilities, negotiate_capabilities, negotiate_version,
    release_link,
};

// ---------------------------------------------------------------------------
// Inbound links
// ---------------------------------------------------------------------------

/// Where this node takes links from its peers.
pub(crate) enum InboundLinks {
    /// Its own cluster port, bound by connect_cluster().
    Listener(tokio::net::TcpListener),
    /// The WebSocket port (single-port mode): it hands over every connection
    /// that starts like a cluster frame.
    MainPort(mpsc::UnboundedReceiver<(tokio::net::TcpStream, SocketAddr)>),
}

/// Admission of inbound links. Between accept and HELLO a link holds one of
/// `MAX_PENDING_HELLOS` slots, and one address no more than
/// `MAX_PENDING_HELLOS_PER_IP` of them, so junk connections from one host
/// cannot keep the real peers out; past HELLO it holds one of
/// `MAX_CLUSTER_PEERS` sessions.
#[derive(Default)]
pub(super) struct InboundGate {
    pending: std::sync::Mutex<HashMap<IpAddr, usize>>,
    pending_total: std::sync::atomic::AtomicUsize,
    sessions: std::sync::atomic::AtomicUsize,
}

const MAX_PENDING_HELLOS: usize = 64;
const MAX_PENDING_HELLOS_PER_IP: usize = 4;

/// A link's place in the gate until HELLO; `session()` trades it for a
/// session slot.
pub(super) struct Admitted {
    gate: Arc<InboundGate>,
    ip: IpAddr,
    pending: bool,
    session: bool,
}

impl InboundGate {
    pub(super) fn admit(self: &Arc<Self>, addr: SocketAddr) -> Option<Admitted> {
        let ip = addr.ip();
        let mut pending = self.pending.lock().unwrap_or_else(|e| e.into_inner());
        let from_ip = pending.entry(ip).or_insert(0);
        if *from_ip >= MAX_PENDING_HELLOS_PER_IP
            || self.pending_total.load(Ordering::Relaxed) >= MAX_PENDING_HELLOS
        {
            if *from_ip == 0 {
                pending.remove(&ip);
            }
            tracing::warn!("[WSE-Cluster] Too many links waiting for HELLO, refusing {addr}");
            return None;
        }
        *from_ip += 1;
        self.pending_total.fetch_add(1, Ordering::Relaxed);
        Some(Admitted {
            gate: Arc::clone(self),
            ip,
            pending: true,
            session: false,
        })
    }
}

impl Admitted {
    fn leave_pending(&mut self) {
        if std::mem::take(&mut self.pending) {
            let mut pending = self.gate.pending.lock().unwrap_or_else(|e| e.into_inner());
            if let Some(from_ip) = pending.get_mut(&self.ip) {
                *from_ip -= 1;
                if *from_ip == 0 {
                    pending.remove(&self.ip);
                }
            }
            self.gate.pending_total.fetch_sub(1, Ordering::Relaxed);
        }
    }

    /// HELLO arrived: a session slot, or false when all are taken.
    fn session(&mut self) -> bool {
        self.leave_pending();
        let taken = self
            .gate
            .sessions
            .try_update(Ordering::AcqRel, Ordering::Acquire, |n| {
                (n < MAX_CLUSTER_PEERS).then_some(n + 1)
            })
            .is_ok();
        self.session = taken;
        taken
    }
}

impl Drop for Admitted {
    fn drop(&mut self) {
        self.leave_pending();
        if self.session {
            self.gate.sessions.fetch_sub(1, Ordering::AcqRel);
        }
    }
}

/// What an inbound session needs from the manager.
#[derive(Clone)]
pub(super) struct InboundSessionCtx {
    pub(super) instance_id: String,
    pub(super) ctx: ClusterContext,
    pub(super) peer_reg_tx: mpsc::UnboundedSender<PeerRegistration>,
    pub(super) global_cancel: CancellationToken,
}

impl InboundSessionCtx {
    pub(super) async fn run<S>(self, stream: S, addr: SocketAddr, admitted: Admitted)
    where
        S: tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin + Send + 'static,
    {
        handle_cluster_inbound_generic(stream, addr, self, admitted).await;
    }
}

/// Accept links on the cluster port until the cluster shuts down. An accept
/// error (out of file descriptors, say) is retried after a pause that grows
/// from 10 ms to a second, and logged once per pause.
pub(super) async fn accept_cluster_links(
    listener: tokio::net::TcpListener,
    tls: Option<Arc<arc_swap::ArcSwap<ClusterTlsConfig>>>,
    gate: Arc<InboundGate>,
    inbound: InboundSessionCtx,
) {
    let cancel = inbound.global_cancel.clone();
    let mut pause = Duration::ZERO;
    loop {
        let accepted = tokio::select! {
            res = listener.accept() => res,
            () = cancel.cancelled() => break,
        };
        let (stream, addr) = match accepted {
            Ok(link) => {
                pause = Duration::ZERO;
                link
            }
            Err(e) => {
                pause = (pause * 2).clamp(Duration::from_millis(10), Duration::from_secs(1));
                tracing::error!("[WSE-Cluster] Cluster accept error: {e} (retrying in {pause:?})");
                tokio::select! {
                    () = tokio::time::sleep(pause) => continue,
                    () = cancel.cancelled() => break,
                }
            }
        };
        let Some(admitted) = gate.admit(addr) else {
            continue;
        };
        let _ = stream.set_nodelay(true);
        let sock = SockRef::from(&stream);
        let _ = sock.set_send_buffer_size(262_144);
        let _ = sock.set_recv_buffer_size(262_144);
        let tls_snap = tls.as_ref().map(|holder| holder.load_full());
        let inbound = inbound.clone();
        tokio::spawn(async move {
            match tls_snap {
                Some(tls_cfg) => {
                    match tokio::time::timeout(
                        Duration::from_secs(5),
                        tls_cfg.acceptor.accept(stream),
                    )
                    .await
                    {
                        Ok(Ok(tls_stream)) => inbound.run(tls_stream, addr, admitted).await,
                        Ok(Err(e)) => {
                            tracing::warn!("[WSE-Cluster] TLS accept failed from {addr}: {e}");
                        }
                        Err(_) => tracing::warn!("[WSE-Cluster] TLS accept timeout from {addr}"),
                    }
                }
                None => inbound.run(stream, addr, admitted).await,
            }
        });
    }
}

// ---------------------------------------------------------------------------
// Inbound cluster connection handler (generic over transport)
// ---------------------------------------------------------------------------

/// Handle inbound cluster peer connection. Generic over stream type (TLS or plaintext).
async fn handle_cluster_inbound_generic<S>(
    stream: S,
    addr: SocketAddr,
    session: InboundSessionCtx,
    mut admitted: Admitted,
) where
    S: tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin + Send + 'static,
{
    let InboundSessionCtx {
        instance_id,
        ctx,
        peer_reg_tx,
        global_cancel,
    } = session;
    let instance_id: &str = &instance_id;
    let local_caps = local_capabilities(&ctx);
    let (mut reader, mut writer) = tokio::io::split(stream);

    // Read peer's HELLO frame
    let peer_hello =
        match tokio::time::timeout(Duration::from_secs(5), read_frame(&mut reader)).await {
            Ok(Ok(frame)) => frame,
            _ => None,
        };

    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 inbound {addr}: local={PROTOCOL_VERSION}, remote={protocol_version}"
                );
                return;
            }
        },
        _ => {
            tracing::warn!("[WSE-Cluster] Invalid HELLO from inbound {addr}");
            return;
        }
    };

    if !admitted.session() {
        tracing::warn!(
            "[WSE-Cluster] Refusing inbound {addr}: {MAX_CLUSTER_PEERS} peer sessions already"
        );
        return;
    }

    // Claim the link. Refused, it closes before our HELLO: the dialer keeps
    // retrying with backoff, in case the link that won goes away.
    let 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: peer_instance_id.clone(),
        session: session_gen,
        cancel: cancel.clone(),
        activity: Arc::clone(&last_activity),
    };
    match claim_link(&ctx.links, instance_id, &peer_instance_id, link) {
        Ok(()) => {}
        Err(LinkRefused::SelfLink) => {
            // Our HELLO tells the dialer it reached itself: it stops dialing
            // this address.
            tracing::debug!("[WSE-Cluster] Closing inbound from {addr}: it is this node");
            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);
            let _ = writer.write_all(&frame).await;
            let _ = writer.flush().await;
            return;
        }
        Err(LinkRefused::Duplicate) => {
            tracing::debug!(
                "[WSE-Cluster] Closing inbound from {addr}: already linked to instance {peer_instance_id}"
            );
            return;
        }
    }

    // Send our HELLO response
    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::error!("[WSE-Cluster] Failed to send HELLO to inbound {addr}");
        release_link(&ctx.links, &peer_instance_id, session_gen);
        return;
    }

    // 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 inbound {addr} within {RESUME_TIMEOUT:?}");
            release_link(&ctx.links, &peer_instance_id, session_gen);
            return;
        };
        Some(wanted)
    } else {
        None
    };

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

    // Send PEER_ANNOUNCE with our cluster address (if in 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;
    }

    // Send 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;
        }
    }

    tracing::info!(
        "[WSE-Cluster] Accepted inbound peer from {addr} (v{_peer_version}, caps=0x{peer_caps:08x})"
    );
    let peer_addr_str = addr.to_string();
    ctx.metrics.connected_peers.fetch_add(1, Ordering::Relaxed);
    ctx.metrics.peer_info.insert(
        peer_addr_str.clone(),
        PeerInfo {
            address: peer_addr_str.clone(),
            instance_id: peer_instance_id.clone(),
            capabilities: peer_caps,
            connected_at: epoch_ms(),
        },
    );

    let (write_tx, 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(), cancel.clone()));

    let reader_handle = tokio::spawn(peer_reader(
        reader,
        write_tx.clone(),
        peer_addr_str.clone(),
        ctx.clone(),
        dispatch_tx,
        cancel.clone(),
        (last_activity.clone(), session_gen),
    ));

    // Publish/Sub/Unsub frames from the manager (bidirectional routing),
    // after what the peer missed.
    if !go_live(
        &mut writer,
        &peer_reg_tx,
        (&peer_addr_str, write_tx.clone(), peer_caps),
        wanted,
        &ctx,
    )
    .await
    {
        cancel.cancel();
    }

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

    let heartbeat_handle = tokio::spawn(heartbeat_task(write_tx, cancel.clone(), last_activity));

    // The link ends when any of its tasks ends (each cancels the token as it
    // exits), when a better duplicate replaces it, or on shutdown. Only then
    // are the tasks awaited, so none is polled again after it finished.
    cancel.cancelled().await;
    let _ = reader_handle.await;
    let _ = writer_handle.await;
    let _ = dispatch_handle.await;
    let _ = heartbeat_handle.await;

    // Deregister inbound peer from the cluster manager's peer_txs
    let _ = peer_reg_tx.send(PeerRegistration::Deregister {
        peer_addr: peer_addr_str.clone(),
    });
    release_link(&ctx.links, &peer_instance_id, session_gen);
    ctx.metrics.connected_peers.fetch_sub(1, Ordering::Relaxed);
    ctx.metrics.peer_info.remove(&peer_addr_str);
    tell_disconnected(&ctx, peer_addr_str, session_gen, &global_cancel).await;
    tracing::info!("[WSE-Cluster] Inbound peer {addr} disconnected");
}