useceleris-client 1.0.0

Client SDK for Celeris realtime channels.
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
use std::collections::VecDeque;
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, OnceLock, Weak};
use std::time::Duration;

use futures_util::stream::{SplitSink, SplitStream};
use futures_util::{SinkExt, StreamExt};
use rustls::ClientConfig;
use tokio::io::{AsyncRead, AsyncWrite};
use tokio::net::TcpStream;
use tokio::sync::Notify;
use tokio::task::AbortHandle;
use tokio::time::Instant;
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
use tokio_tungstenite::tungstenite::protocol::WebSocketConfig;
use tokio_tungstenite::tungstenite::{Bytes, Message};
use tokio_tungstenite::{Connector, MaybeTlsStream, WebSocketStream, client_async_tls_with_config};

use crate::channel::{ChannelInner, ChannelShared};
use crate::command_queue::QueuedPublish;
use crate::constants::{
    MAXIMUM_BATCH_BYTES, MAXIMUM_BATCH_COMMANDS, MAXIMUM_BUFFERED_BYTES, MAXIMUM_PENDING_COMMANDS,
};
use crate::decode::decode_server_message;
use crate::errors::{CelerisError, protocol_error};
use crate::heartbeat::heartbeat;
use crate::messages::ServerMessage;

/// The byte stream under a WebSocket: TCP in production, an in-memory pipe in
/// tests.
pub(crate) trait Io: AsyncRead + AsyncWrite + Unpin + Send {}

impl<Stream: AsyncRead + AsyncWrite + Unpin + Send> Io for Stream {}

pub(crate) type WebSocket = WebSocketStream<MaybeTlsStream<Box<dyn Io>>>;

pub(crate) type SharedSink = Arc<tokio::sync::Mutex<SplitSink<WebSocket, Message>>>;

/// The one transport seam: opens a WebSocket to a URL, which carries the
/// credentials, so tests can substitute an in-memory peer. The error carries
/// nothing, because a dial error quotes the URL.
pub(crate) type Dialer = Arc<
    dyn Fn(String) -> Pin<Box<dyn Future<Output = Result<WebSocket, ()>> + Send>> + Send + Sync,
>;

pub(crate) fn web_socket_dialer() -> Dialer {
    Arc::new(|url| Box::pin(dial_web_socket(url)))
} // end fn web_socket_dialer

async fn dial_web_socket(url: String) -> Result<WebSocket, ()> {
    let request = url.as_str().into_client_request().map_err(drop)?;
    let uri = request.uri();
    let secure = uri.scheme_str() == Some("wss");
    let host = uri
        .host()
        .ok_or(())?
        .trim_start_matches('[')
        .trim_end_matches(']')
        .to_owned();
    let port = uri.port_u16().unwrap_or(if secure { 443 } else { 80 });

    let tcp = TcpStream::connect((host.as_str(), port))
        .await
        .map_err(drop)?;
    tcp.set_nodelay(true).map_err(drop)?;

    // Received messages are never size-checked (LIMIT-01).
    let config = WebSocketConfig::default()
        .max_message_size(None)
        .max_frame_size(None);
    let stream: Box<dyn Io> = Box::new(tcp);
    let connector = Connector::Rustls(tls_configuration());
    let (socket, _response) =
        client_async_tls_with_config(request, stream, Some(config), Some(connector))
            .await
            .map_err(drop)?;

    Ok(socket)
} // end fn dial_web_socket

/// TLS verified against the system's roots, with the crate's own crypto
/// provider, so an application's TLS settings cannot weaken a connection
/// (SEC-02).
fn tls_configuration() -> Arc<ClientConfig> {
    static CONFIGURATION: OnceLock<Arc<ClientConfig>> = OnceLock::new();

    let configuration = CONFIGURATION.get_or_init(|| {
        let mut roots = rustls::RootCertStore::empty();
        roots.add_parsable_certificates(rustls_native_certs::load_native_certs().certs);

        let provider = Arc::new(rustls::crypto::ring::default_provider());

        let configuration = ClientConfig::builder_with_provider(provider)
            .with_safe_default_protocol_versions()
            .expect("the ring provider supports the default protocol versions")
            .with_root_certificates(roots)
            .with_no_client_auth();

        Arc::new(configuration)
    });

    Arc::clone(configuration)
} // end fn tls_configuration

/// One command handed to the writer.
pub(crate) struct OutboundFrame {
    data: Bytes,

    /// Set for a publish, which settles once written.
    publish: Option<Arc<QueuedPublish>>,
} // end struct OutboundFrame

/// One socket and the tasks that drive it: a writer that writes handed-off
/// frames in order, packed into batches (BATCH-01), a reader that routes what arrives, and a heartbeat that
/// pings the server and finds a dead path. Guarded by the channel's lock.
pub(crate) struct ConnectionState {
    pub(crate) id: u64,

    /// Frames handed to the writer and not yet written, the batch being
    /// written first: its size is `writing`. They are bounded by count and
    /// bytes, so a maximum-size command always fits an empty writer.
    outbound: VecDeque<OutboundFrame>,
    outbound_bytes: usize,
    writing: usize,

    /// Wakes the writer: a frame was handed off, or the connection closes or
    /// breaks.
    ready: Arc<Notify>,

    /// Asks the writer to flush, then to perform the closing handshake.
    closing: bool,

    /// Asks the writer to stop at once.
    broken: bool,

    pub(crate) heartbeat: HeartbeatState,

    tasks: Vec<AbortHandle>,
    pub(crate) writer: Option<tokio::task::JoinHandle<()>>,
} // end struct ConnectionState

/// What the heartbeat knows about the connection (HEARTBEAT-01).
pub(crate) struct HeartbeatState {
    /// Reading time: how long the reader has waited for a message, the only
    /// time a pong or a message can arrive. It is the finished reads' total,
    /// plus the current read's time since `reading_since`.
    reading_time: Duration,
    reading_since: Option<Instant>,

    /// The reading time when the oldest unanswered ping was sent, if a ping
    /// awaits its pong.
    pub(crate) unanswered_since: Option<Duration>,

    /// When the server was last heard from: the socket opening, a pong or a
    /// received message.
    pub(crate) last_heard: Instant,
} // end struct HeartbeatState

impl HeartbeatState {
    fn start_reading(&mut self) {
        self.reading_since = Some(Instant::now());
    } // end method start_reading

    fn stop_reading(&mut self, received: bool) {
        if let Some(since) = self.reading_since.take() {
            self.reading_time += since.elapsed();
        }

        if received {
            self.heard();
        }
    } // end method stop_reading

    /// Records a pong or a received message, which clears the unanswered
    /// ping.
    pub(crate) fn heard(&mut self) {
        self.last_heard = Instant::now();
        self.unanswered_since = None;
    } // end method heard

    /// Includes the read in progress.
    pub(crate) fn total_reading_time(&self) -> Duration {
        self.reading_time
            + self
                .reading_since
                .map_or(Duration::ZERO, |since| since.elapsed())
    } // end method total_reading_time
} // end impl HeartbeatState

impl ConnectionState {
    pub(crate) fn has_room(&self, size: usize) -> bool {
        self.outbound.len() < MAXIMUM_PENDING_COMMANDS
            && self.outbound_bytes + size <= MAXIMUM_BUFFERED_BYTES
    } // end method has_room

    pub(crate) fn hand_off(&mut self, data: Bytes, publish: Option<Arc<QueuedPublish>>) {
        self.outbound_bytes += data.len();
        self.outbound.push_back(OutboundFrame { data, publish });
        self.ready.notify_one();
    } // end method hand_off

    pub(crate) fn begin_closing(&mut self) {
        self.closing = true;
        self.ready.notify_one();
    } // end method begin_closing

    fn is_writing(&self, publish: &Arc<QueuedPublish>) -> bool {
        self.outbound
            .iter()
            .take(self.writing)
            .filter_map(|frame| frame.publish.as_ref())
            .any(|written| Arc::ptr_eq(written, publish))
    } // end method is_writing

    /// Marks the frames at the front of the writer that go out together, in
    /// order: at most `MAXIMUM_BATCH_COMMANDS` of them, within
    /// `MAXIMUM_BATCH_BYTES` with the array header. A first frame larger than
    /// that goes alone. Returns the bytes to write: a single command as it is,
    /// several in a `*N` array.
    fn pack_batch(&mut self) -> Bytes {
        let mut size = self.outbound[0].data.len();
        let mut count = 1;

        while let Some(frame) = self.outbound.get(count) {
            let next = size + frame.data.len();
            // The array header is "*", the count and LF.
            let frame_length = 2 + (count + 1).to_string().len() + next;

            if count == MAXIMUM_BATCH_COMMANDS || frame_length > MAXIMUM_BATCH_BYTES {
                break;
            }

            size = next;
            count += 1;
        }

        self.writing = count;

        if count == 1 {
            return self.outbound[0].data.clone();
        }

        let header = format!("*{count}\n");
        let mut data = Vec::with_capacity(header.len() + size);
        data.extend_from_slice(header.as_bytes());

        for frame in self.outbound.iter().take(count) {
            data.extend_from_slice(&frame.data);
        }

        Bytes::from(data)
    } // end method pack_batch

    /// Takes back every frame of `publish` that the writer has not started, and
    /// reports if there was one. A rate limit can requeue a publish still in
    /// the writer, so it may have been handed over twice.
    pub(crate) fn remove_publish(&mut self, publish: &Arc<QueuedPublish>) -> (bool, bool) {
        let writing = self.is_writing(publish);
        let skip = self.writing;
        let before = self.outbound.len();
        let mut index = 0;

        self.outbound.retain(|frame| {
            index += 1;
            let unwritten = index > skip;
            let matches = frame
                .publish
                .as_ref()
                .is_some_and(|queued| Arc::ptr_eq(queued, publish));

            !(unwritten && matches)
        });

        self.outbound_bytes = self.outbound.iter().map(|frame| frame.data.len()).sum();

        (self.outbound.len() < before, writing)
    } // end method remove_publish

    /// Fails every publish handed to this connection that the socket never
    /// started to write.
    pub(crate) fn fail_unwritten(&mut self, error: &CelerisError) {
        for frame in self.outbound.iter().skip(self.writing) {
            if let Some(publish) = &frame.publish {
                publish.settle(Err(error.clone()));
            }
        }

        self.drop_unwritten();
    } // end method fail_unwritten

    /// Takes back, unsettled and in order, every publish handed to this
    /// connection that the socket never started to write, so recovery can
    /// queue it for the next socket (QUEUE-01). A rate limit can requeue a
    /// publish still in the writer, so it may appear twice; it is taken once.
    /// A publish already written, or being written, keeps that outcome: only a
    /// rate limit resends.
    pub(crate) fn take_unwritten(&mut self) -> Vec<Arc<QueuedPublish>> {
        let mut publishes: Vec<Arc<QueuedPublish>> = Vec::new();

        for frame in self.outbound.iter().skip(self.writing) {
            let Some(publish) = &frame.publish else {
                continue;
            };

            let taken = publishes
                .iter()
                .any(|existing| Arc::ptr_eq(existing, publish));

            if publish.is_settled() || self.is_writing(publish) || taken {
                continue;
            }

            publish.set_connection(None);
            publishes.push(Arc::clone(publish));
        }

        self.drop_unwritten();

        publishes
    } // end method take_unwritten

    /// Empties the writer except for the batch that the socket is writing.
    fn drop_unwritten(&mut self) {
        self.outbound.truncate(self.writing);
        self.outbound_bytes = self.outbound.iter().map(|frame| frame.data.len()).sum();
    } // end method drop_unwritten

    /// Stops the connection without a closing handshake. A publish whose write
    /// was in progress may or may not have been sent, and is never resent.
    fn abandon(&mut self) {
        self.broken = true;
        self.ready.notify_one();

        for publish in self
            .outbound
            .iter()
            .take(self.writing)
            .filter_map(|frame| frame.publish.as_ref())
        {
            publish.settle(Err(CelerisError::DeliveryUnknown(
                "The connection dropped while the socket was writing the publish, so it may or may not have been sent."
                    .to_owned(),
            )));
        }

        for task in &self.tasks {
            task.abort();
        }

        if let Some(writer) = &self.writer {
            writer.abort();
        }
    } // end method abandon
} // end impl ConnectionState

impl Drop for ConnectionState {
    /// A connection that the channel lets go of, or that goes with a dropped
    /// channel, takes its tasks and its socket with it.
    fn drop(&mut self) {
        self.abandon();
    } // end method drop
} // end impl Drop for ConnectionState

impl ChannelInner {
    pub(crate) fn connection(&self, id: u64) -> Option<&ConnectionState> {
        self.connections
            .iter()
            .find(|connection| connection.id == id)
    } // end method connection

    pub(crate) fn connection_mut(&mut self, id: u64) -> Option<&mut ConnectionState> {
        self.connections
            .iter_mut()
            .find(|connection| connection.id == id)
    } // end method connection_mut

    /// Installs a socket as the channel's connection and starts its tasks.
    pub(crate) fn install_connection(&mut self, socket: WebSocket) -> u64 {
        self.last_connection_id += 1;
        let id = self.last_connection_id;
        let (sink, stream) = socket.split();
        let sink: SharedSink = Arc::new(tokio::sync::Mutex::new(sink));
        let runtime = self.runtime();

        let reader = runtime.spawn(read_loop(self.this.clone(), id, stream));
        let pinger = runtime.spawn(heartbeat(self.this.clone(), id, Arc::clone(&sink)));
        let writer = runtime.spawn(write_loop(self.this.clone(), id, sink));

        self.connections.push(ConnectionState {
            id,
            outbound: VecDeque::new(),
            outbound_bytes: 0,
            writing: 0,
            ready: Arc::new(Notify::new()),
            closing: false,
            broken: false,
            heartbeat: HeartbeatState {
                reading_time: Duration::ZERO,
                reading_since: None,
                unanswered_since: None,
                last_heard: Instant::now(),
            },
            tasks: vec![reader.abort_handle(), pinger.abort_handle()],
            writer: Some(writer),
        });

        self.current_connection = Some(id);

        id
    } // end method install_connection

    /// Lets go of a connection: its unwritten publishes fail with `error`, and
    /// its tasks and socket stop.
    pub(crate) fn discard_connection(&mut self, id: u64, error: &CelerisError) {
        if let Some(position) = self
            .connections
            .iter()
            .position(|connection| connection.id == id)
        {
            let mut connection = self.connections.remove(position);
            connection.fail_unwritten(error);
        }

        if self.current_connection == Some(id) {
            self.current_connection = None;
        }
    } // end method discard_connection
} // end impl ChannelInner

enum NextWrite {
    Frame(Bytes),
    Close,
    Wait(Arc<Notify>),
    Stop,
} // end enum NextWrite

/// Writes handed-off frames in order until the connection breaks, or until it
/// closes and every frame is flushed.
async fn write_loop(channel: Weak<ChannelShared>, id: u64, sink: SharedSink) {
    loop {
        let next = {
            let Some(channel) = channel.upgrade() else {
                return;
            };

            let mut inner = channel.lock();

            match inner.connection_mut(id) {
                None => NextWrite::Stop,
                Some(connection) if connection.broken => NextWrite::Stop,
                Some(connection) if !connection.outbound.is_empty() => {
                    NextWrite::Frame(connection.pack_batch())
                }
                Some(connection) if connection.closing => NextWrite::Close,
                Some(connection) => NextWrite::Wait(Arc::clone(&connection.ready)),
            }
        };

        match next {
            NextWrite::Stop => return,
            NextWrite::Wait(ready) => ready.notified().await,
            NextWrite::Close => {
                let _ = sink.lock().await.close().await;

                return;
            }
            NextWrite::Frame(data) => {
                let written = sink.lock().await.send(Message::Binary(data)).await.is_ok();

                let Some(channel) = channel.upgrade() else {
                    return;
                };

                if !channel.finish_write(id, written) {
                    return;
                }
            }
        }
    }
} // end fn write_loop

impl ChannelShared {
    /// Settles the batch that the writer finished. Reports `false` when the
    /// writer must stop.
    fn finish_write(self: &Arc<Self>, id: u64, written: bool) -> bool {
        let mut inner = self.lock();

        let Some(connection) = inner.connection_mut(id) else {
            return false;
        };

        let batch: Vec<OutboundFrame> = connection.outbound.drain(..connection.writing).collect();
        connection.writing = 0;
        connection.outbound_bytes -= batch.iter().map(|frame| frame.data.len()).sum::<usize>();

        let publishes = batch.into_iter().filter_map(|frame| frame.publish);

        if !written {
            for publish in publishes {
                publish.settle(Err(CelerisError::DeliveryUnknown(
                    "The socket failed while writing the publish, so it may or may not have been sent.".to_owned(),
                )));
            }

            // A write that the socket refuses leaves the server's view of this
            // connection unknown, so the socket is replaced and reconnecting
            // restores every subscription (RESEND-01). Otherwise Close let go
            // of it while it flushed, so what it never wrote is cancelled.
            let queued = inner.receive_socket_failure(id);
            inner.discard_connection(id, &closed_before_sent());
            drop(inner);

            if queued {
                self.dispatch_events();
            }

            return false;
        }

        for publish in publishes {
            publish.settle(Ok(()));
        }

        if inner.current_connection == Some(id) {
            inner.drain();
        }

        true
    } // end method finish_write
} // end impl ChannelShared

pub(crate) fn closed_before_sent() -> CelerisError {
    CelerisError::Cancelled("Channel closed before the publish was sent.".to_owned())
} // end fn closed_before_sent

/// What one read produced.
enum Received {
    Message(Result<ServerMessage, CelerisError>),
    Heard,
    Ended,
} // end enum Received

/// Routes what arrives on one socket until it fails or the channel lets go of
/// it.
async fn read_loop(channel: Weak<ChannelShared>, id: u64, mut stream: SplitStream<WebSocket>) {
    loop {
        if !with_heartbeat(&channel, id, HeartbeatState::start_reading) {
            return;
        }

        let next = stream.next().await;
        let received = matches!(next, Some(Ok(_)));

        if !with_heartbeat(&channel, id, |heartbeat| heartbeat.stop_reading(received)) {
            return;
        }

        // A message that cannot be decoded costs exactly that message: decoding
        // never spans messages, so the next one is unaffected and the
        // connection stays up (DECODE-01).
        let outcome = match next {
            Some(Ok(Message::Binary(data))) => Received::Message(decode_server_message(&data)),
            Some(Ok(Message::Text(_))) => Received::Message(Err(protocol_error(
                "Expected a binary WebSocket message.",
                "Message",
                0,
            ))),
            Some(Ok(Message::Ping(_) | Message::Pong(_) | Message::Frame(_))) => Received::Heard,
            Some(Ok(Message::Close(_)) | Err(_)) | None => Received::Ended,
        };

        let Some(channel) = channel.upgrade() else {
            return;
        };

        match outcome {
            Received::Heard => {}
            Received::Ended => {
                let queued = channel.lock().receive_socket_failure(id);

                if queued {
                    channel.dispatch_events();
                }

                return;
            }
            Received::Message(message) => {
                if !channel.route(id, message).await {
                    return;
                }
            }
        }
    }
} // end fn read_loop

/// Runs `update` on the connection's heartbeat state. Reports `false` when the
/// channel or the connection is gone.
fn with_heartbeat(
    channel: &Weak<ChannelShared>,
    id: u64,
    update: impl FnOnce(&mut HeartbeatState),
) -> bool {
    let Some(channel) = channel.upgrade() else {
        return false;
    };

    let mut inner = channel.lock();

    match inner.connection_mut(id) {
        Some(connection) => {
            update(&mut connection.heartbeat);

            true
        }
        None => false,
    }
} // end fn with_heartbeat