forked from clockworklabs/SpacetimeDB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscribe.rs
More file actions
1771 lines (1596 loc) · 62.8 KB
/
subscribe.rs
File metadata and controls
1771 lines (1596 loc) · 62.8 KB
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
use std::fmt::Display;
use std::future::{poll_fn, Future};
use std::num::NonZeroUsize;
use std::panic;
use std::pin::{pin, Pin};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::Duration;
use async_stream::stream;
use axum::extract::{Path, Query, State};
use axum::response::IntoResponse;
use axum::Extension;
use axum_extra::TypedHeader;
use bytes::Bytes;
use bytestring::ByteString;
use derive_more::From;
use futures::{pin_mut, Sink, SinkExt, Stream, StreamExt};
use http::{HeaderValue, StatusCode};
use prometheus::IntGauge;
use scopeguard::{defer, ScopeGuard};
use serde::Deserialize;
use spacetimedb::client::messages::{
serialize, IdentityTokenMessage, SerializableMessage, SerializeBuffer, SwitchedServerMessage, ToProtocol,
};
use spacetimedb::client::{
ClientActorId, ClientConfig, ClientConnection, DataMessage, MessageExecutionError, MessageHandleError,
MeteredReceiver, Protocol,
};
use spacetimedb::host::module_host::ClientConnectedError;
use spacetimedb::host::NoSuchModule;
use spacetimedb::util::spawn_rayon;
use spacetimedb::worker_metrics::WORKER_METRICS;
use spacetimedb::Identity;
use spacetimedb_client_api_messages::websocket::{self as ws_api, Compression};
use spacetimedb_datastore::execution_context::WorkloadType;
use spacetimedb_lib::connection_id::{ConnectionId, ConnectionIdForUrl};
use std::time::Instant;
use tokio::sync::{mpsc, watch};
use tokio::task::JoinHandle;
use tokio::time::error::Elapsed;
use tokio::time::{sleep_until, timeout};
use tokio_tungstenite::tungstenite::Utf8Bytes;
use crate::auth::SpacetimeAuth;
use crate::util::serde::humantime_duration;
use crate::util::websocket::{
CloseCode, CloseFrame, Message as WsMessage, WebSocketConfig, WebSocketStream, WebSocketUpgrade, WsError,
};
use crate::util::{NameOrIdentity, XForwardedFor};
use crate::{log_and_500, ControlStateDelegate, NodeDelegate};
#[allow(clippy::declare_interior_mutable_const)]
pub const TEXT_PROTOCOL: HeaderValue = HeaderValue::from_static(ws_api::TEXT_PROTOCOL);
#[allow(clippy::declare_interior_mutable_const)]
pub const BIN_PROTOCOL: HeaderValue = HeaderValue::from_static(ws_api::BIN_PROTOCOL);
pub trait HasWebSocketOptions {
fn websocket_options(&self) -> WebSocketOptions;
}
impl<T: HasWebSocketOptions> HasWebSocketOptions for Arc<T> {
fn websocket_options(&self) -> WebSocketOptions {
(**self).websocket_options()
}
}
#[derive(Deserialize)]
pub struct SubscribeParams {
pub name_or_identity: NameOrIdentity,
}
#[derive(Deserialize)]
pub struct SubscribeQueryParams {
pub connection_id: Option<ConnectionIdForUrl>,
#[serde(default)]
pub compression: Compression,
/// Whether we want "light" responses, tailored to network bandwidth constrained clients.
/// This knob works by setting other, more specific, knobs to the value.
#[serde(default)]
pub light: bool,
}
pub fn generate_random_connection_id() -> ConnectionId {
ConnectionId::from_le_byte_array(rand::random())
}
pub async fn handle_websocket<S>(
State(ctx): State<S>,
Path(SubscribeParams { name_or_identity }): Path<SubscribeParams>,
Query(SubscribeQueryParams {
connection_id,
compression,
light,
}): Query<SubscribeQueryParams>,
forwarded_for: Option<TypedHeader<XForwardedFor>>,
Extension(auth): Extension<SpacetimeAuth>,
ws: WebSocketUpgrade,
) -> axum::response::Result<impl IntoResponse>
where
S: NodeDelegate + ControlStateDelegate + HasWebSocketOptions,
{
if connection_id.is_some() {
// TODO: Bump this up to `log::warn!` after removing the client SDKs' uses of that parameter.
log::debug!("The connection_id query parameter to the subscribe HTTP endpoint is internal and will be removed in a future version of SpacetimeDB.");
}
let connection_id = connection_id
.map(ConnectionId::from)
.unwrap_or_else(generate_random_connection_id);
if connection_id == ConnectionId::ZERO {
Err((
StatusCode::BAD_REQUEST,
"Invalid connection ID: the all-zeros ConnectionId is reserved.",
))?;
}
let db_identity = name_or_identity.resolve(&ctx).await?;
let (res, ws_upgrade, protocol) =
ws.select_protocol([(BIN_PROTOCOL, Protocol::Binary), (TEXT_PROTOCOL, Protocol::Text)]);
let protocol = protocol.ok_or((StatusCode::BAD_REQUEST, "no valid protocol selected"))?;
let client_config = ClientConfig {
protocol,
compression,
tx_update_full: !light,
};
// TODO: Should also maybe refactor the code and the protocol to allow a single websocket
// to connect to multiple modules
let database = ctx
.get_database_by_identity(&db_identity)
.unwrap()
.ok_or(StatusCode::NOT_FOUND)?;
let leader = ctx
.leader(database.id)
.await
.map_err(log_and_500)?
.ok_or(StatusCode::NOT_FOUND)?;
let identity_token = auth.creds.token().into();
let module_rx = leader.module_watcher().await.map_err(log_and_500)?;
let client_id = ClientActorId {
identity: auth.identity,
connection_id,
name: ctx.client_actor_index().next_client_name(),
};
let ws_config = WebSocketConfig::default()
.max_message_size(Some(0x2000000))
.max_frame_size(None)
.accept_unmasked_frames(false);
let ws_opts = ctx.websocket_options();
tokio::spawn(async move {
let ws = match ws_upgrade.upgrade(ws_config).await {
Ok(ws) => ws,
Err(err) => {
log::error!("WebSocket init error: {err}");
return;
}
};
match forwarded_for {
Some(TypedHeader(XForwardedFor(ip))) => {
log::debug!("New client connected from ip {ip}")
}
None => log::debug!("New client connected from unknown ip"),
}
let actor = |client, sendrx| ws_client_actor(ws_opts, client, ws, sendrx);
let client = match ClientConnection::spawn(client_id, client_config, leader.replica_id, module_rx, actor).await
{
Ok(s) => s,
Err(e @ (ClientConnectedError::Rejected(_) | ClientConnectedError::OutOfEnergy)) => {
log::info!("{e}");
return;
}
Err(e @ (ClientConnectedError::DBError(_) | ClientConnectedError::ReducerCall(_))) => {
log::warn!("ModuleHost died while we were connecting: {e:#}");
return;
}
};
// Send the client their identity token message as the first message
// NOTE: We're adding this to the protocol because some client libraries are
// unable to access the http response headers.
// Clients that receive the token from the response headers should ignore this
// message.
let message = IdentityTokenMessage {
identity: auth.identity,
token: identity_token,
connection_id,
};
if let Err(e) = client.send_message(message) {
log::warn!("{e}, before identity token was sent")
}
});
Ok(res)
}
struct ActorState {
pub client_id: ClientActorId,
pub database: Identity,
config: WebSocketOptions,
closed: AtomicBool,
got_pong: AtomicBool,
}
impl ActorState {
pub fn new(database: Identity, client_id: ClientActorId, config: WebSocketOptions) -> Self {
Self {
database,
client_id,
config,
closed: AtomicBool::new(false),
got_pong: AtomicBool::new(true),
}
}
pub fn closed(&self) -> bool {
self.closed.load(Ordering::Relaxed)
}
pub fn close(&self) -> bool {
self.closed.swap(true, Ordering::Relaxed)
}
pub fn set_ponged(&self) {
self.got_pong.store(true, Ordering::Relaxed);
}
pub fn reset_ponged(&self) -> bool {
self.got_pong.swap(false, Ordering::Relaxed)
}
pub fn next_idle_deadline(&self) -> Instant {
Instant::now() + self.config.idle_timeout
}
}
/// Configuration for WebSocket connections.
#[derive(Clone, Copy, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct WebSocketOptions {
/// Interval at which to send `Ping` frames.
///
/// We use pings for connection keep-alive.
/// Value must be smaller than `idle_timeout`.
///
/// Default: 15s
#[serde(with = "humantime_duration")]
#[serde(default = "WebSocketOptions::default_ping_interval")]
pub ping_interval: Duration,
/// Amount of time after which an idle connection is closed.
///
/// A connection is considered idle if no data is received nor sent.
/// This includes `Ping`/`Pong` frames used for keep-alive.
///
/// Value must be greater than `ping_interval`.
///
/// Default: 30s
#[serde(with = "humantime_duration")]
#[serde(default = "WebSocketOptions::default_idle_timeout")]
pub idle_timeout: Duration,
/// For how long to keep draining the incoming messages until a client close
/// is received.
///
/// Default: 250ms
#[serde(with = "humantime_duration")]
#[serde(default = "WebSocketOptions::default_close_handshake_timeout")]
pub close_handshake_timeout: Duration,
/// Maximum number of messages to queue for processing.
///
/// If this number is exceeded, the client is disconnected.
///
/// Default: 2048
#[serde(default = "WebSocketOptions::default_incoming_queue_length")]
pub incoming_queue_length: NonZeroUsize,
}
impl Default for WebSocketOptions {
fn default() -> Self {
Self::DEFAULT
}
}
impl WebSocketOptions {
const DEFAULT_PING_INTERVAL: Duration = Duration::from_secs(15);
const DEFAULT_IDLE_TIMEOUT: Duration = Duration::from_secs(30);
const DEFAULT_CLOSE_HANDSHAKE_TIMEOUT: Duration = Duration::from_millis(250);
const DEFAULT_INCOMING_QUEUE_LENGTH: NonZeroUsize = NonZeroUsize::new(2048).expect("2048 > 0, qed");
const DEFAULT: Self = Self {
ping_interval: Self::DEFAULT_PING_INTERVAL,
idle_timeout: Self::DEFAULT_IDLE_TIMEOUT,
close_handshake_timeout: Self::DEFAULT_CLOSE_HANDSHAKE_TIMEOUT,
incoming_queue_length: Self::DEFAULT_INCOMING_QUEUE_LENGTH,
};
const fn default_ping_interval() -> Duration {
Self::DEFAULT_PING_INTERVAL
}
const fn default_idle_timeout() -> Duration {
Self::DEFAULT_IDLE_TIMEOUT
}
const fn default_close_handshake_timeout() -> Duration {
Self::DEFAULT_CLOSE_HANDSHAKE_TIMEOUT
}
const fn default_incoming_queue_length() -> NonZeroUsize {
Self::DEFAULT_INCOMING_QUEUE_LENGTH
}
}
async fn ws_client_actor(
options: WebSocketOptions,
client: ClientConnection,
ws: WebSocketStream,
sendrx: MeteredReceiver<SerializableMessage>,
) {
// ensure that even if this task gets cancelled, we always cleanup the connection
let mut client = scopeguard::guard(client, |client| {
tokio::spawn(client.disconnect());
});
ws_client_actor_inner(&mut client, options, ws, sendrx).await;
ScopeGuard::into_inner(client).disconnect().await;
}
async fn ws_client_actor_inner(
client: &mut ClientConnection,
config: WebSocketOptions,
ws: WebSocketStream,
sendrx: MeteredReceiver<SerializableMessage>,
) {
let database = client.module.info().database_identity;
let client_id = client.id;
let client_closed_metric = WORKER_METRICS.ws_clients_closed_connection.with_label_values(&database);
let state = Arc::new(ActorState::new(database, client_id, config));
// Channel for [`UnorderedWsMessage`]s.
let (unordered_tx, unordered_rx) = mpsc::unbounded_channel();
// Split websocket into send and receive halves.
let (ws_send, ws_recv) = ws.split();
// Set up the idle timer.
let (idle_tx, idle_rx) = watch::channel(state.next_idle_deadline());
let idle_timer = ws_idle_timer(idle_rx);
// Spawn a task to send outgoing messages
// obtained from `sendrx` and `unordered_rx`.
let send_task = tokio::spawn(ws_send_loop(
state.clone(),
client.config,
ws_send,
sendrx,
unordered_rx,
));
// Spawn a task to handle incoming messages.
let recv_task = tokio::spawn(ws_recv_task(
state.clone(),
idle_tx,
client_closed_metric,
{
let client = client.clone();
move |data, timer| {
let client = client.clone();
async move { client.handle_message(data, timer).await }
}
},
unordered_tx.clone(),
ws_recv,
));
let hotswap = {
let client = client.clone();
move || {
let mut client = client.clone();
async move { client.watch_module_host().await }
}
};
ws_main_loop(state, hotswap, idle_timer, send_task, recv_task, move |msg| {
let _ = unordered_tx.send(msg);
})
.await;
log::info!("Client connection ended: {client_id}");
}
/// The main `select!` loop of the websocket client actor.
///
/// > This function is defined standalone with generic parameters so that its
/// > behavior can be tested in isolation, not requiring I/O and allowing to
/// > mock effects easily.
///
/// The loop's responsibilities are:
///
/// - Drive the tasks handling the send and receive ends of the websockets to
/// completion, terminating when either of them completes.
///
/// - Terminating if the connection is idle for longer than [`ActorConfig::idle_timeout`].
/// The connection becomes idle if nothing is received from the socket.
///
/// - Periodically sending `Ping` frames to prevent the connection from becoming
/// idle (the client is supposed to respond with `Pong`, which resets the
/// idle timer). See [`ActorConfig::ping_interval`].
///
/// - Watch for changes to the [`ClientConnection`]'s module reference.
/// If it changes, the [`ClientConnection`] "hotswaps" the module, if it
/// is exited, the loop schedules a `Close` frame to be sent, initiating a
/// connection shutdown.
///
/// A peculiarity of handling termination is the websocket [close handshake]:
/// whichever side wants to close the connection sends a `Close` frame and needs
/// to wait for the other end to respond with a `Close` for the connection to
/// end cleanly.
///
/// `tungstenite` handles the protocol details of the close handshake for us,
/// but for it to work properly, we must keep polling the socket until the
/// handshake is complete.
///
/// This is straightforward when the client initiates the close, as the receive
/// stream will just become exhausted, and we'll exit the loop.
///
/// In the case of a server-initiated close, it's a bit more tricky, as we're
/// not supposed to send any more data after a `Close` frame (and `tungstenite`
/// prevents it). Yet, we need to keep polling the receive end until either
/// the `Close` response (which could be queued behind a large number of
/// outstanding messages) arrives, or a timeout elapses (in case the client
/// never responds).
///
/// The implementations [`ws_recv_loop`] and [`ws_send_loop`] thus share the
/// [`ActorState`], which tracks whether the connection is in the closing phase
/// ([`ActorState::closed()`]). If closed, both the send and receive loops keep
/// running, but drop any incoming or outgoing messages respectively until
/// either the `Close` response arrives or [`ActorConfig::close_handshake_timeout`]
/// elapses.
///
///
/// Parameters:
///
/// * **state**:
/// The shared [`ActorState`], updated here when a `Pong` message is received.
///
/// * **hotswap**:
/// An abstraction for [`ClientConnection::watch_module_host`], which updates
/// the connection's internal reference to the module if it was updated,
/// allowing database updates without disconnecting clients.
///
/// It is polled here for its error return value: if the output of the future
/// is `Err(NoSuchModule)`, the database was shut down and existing clients
/// must be disconnected.
///
/// * **idle_timer**:
/// Abstraction for [`ws_idle_timer`]: if and when the future completes, the
/// connection is considered unresponsive, and the connection is closed.
///
/// The idle timer should be reset whenever data is received from the websocket.
///
/// * **send_task**:
/// Task handling outgoing messages. Holds the receive end of `unordered_tx`.
///
/// If the task returns, the connection is considered bad, and the main loop
/// exits. If the task panicked, the panic is resumed on the current thread.
///
/// Note that the send task must not terminate after it has sent a `Close`
/// frame (via `unordered_tx`) -- the websocket protocol mandates that the
/// initiator of the close handshake wait for the other end to respond with
/// a `Close` frame. Thus, the loop must continue to poll `recv_task` and not
/// exit due to `send_task` being complete.
///
/// See [`ws_send_loop`].
///
/// * **recv_task**:
/// Task handling incoming messages.
///
/// If the task returns, the connection is considered closed, and the main
/// loop exits. If the task panicked, the panic is resumed on the current
/// thread.
///
/// See [`ws_recv_task`].
///
/// * **unordered_tx**:
/// Channel connected to `send_task` that allows the loop to send `Ping` and
/// `Close` frames.
///
/// Note that messages sent while the receiving `send_task` is already
/// terminated are silently ignored. This is safe because the loop will exit
/// anyway when the `send_task` is complete.
///
///
/// [close handshake]: https://datatracker.ietf.org/doc/html/rfc6455#section-7
async fn ws_main_loop<HotswapWatcher>(
state: Arc<ActorState>,
hotswap: impl Fn() -> HotswapWatcher,
idle_timer: impl Future<Output = ()>,
mut send_task: JoinHandle<()>,
mut recv_task: JoinHandle<()>,
unordered_tx: impl Fn(UnorderedWsMessage),
) where
HotswapWatcher: Future<Output = Result<(), NoSuchModule>>,
{
// Ensure we terminate both tasks if either exits.
let abort_send = send_task.abort_handle();
let abort_recv = recv_task.abort_handle();
defer! {
abort_send.abort();
abort_recv.abort();
};
// Set up the ping interval.
let mut ping_interval = tokio::time::interval(state.config.ping_interval);
// Arm the first hotswap watcher.
let watch_hotswap = hotswap();
pin_mut!(watch_hotswap);
pin_mut!(idle_timer);
loop {
let closed = state.closed();
tokio::select! {
// Drive send and receive tasks to completion,
// propagating panics.
//
// If either task completes,
// the connection is considered closed and we break the loop.
//
// NOTE: We don't abort the tasks until this function returns,
// so the `Err` can't contain an `is_cancelled()` value.
//
// Even if the tasks were cancelled (e.g. if the caller retains
// [`tokio::task::AbortHandle`]s), the reasonable thing to do is to
// exit the loop as if the tasks completed normally.
res = &mut send_task => {
if let Err(e) = res {
if e.is_panic() {
panic::resume_unwind(e.into_panic())
}
}
break;
},
res = &mut recv_task => {
if let Err(e) = res {
if e.is_panic() {
panic::resume_unwind(e.into_panic())
}
}
break;
},
// Exit if we haven't heard from the client for too long.
_ = &mut idle_timer => {
log::warn!("Client {} timed out", state.client_id);
break;
},
// Update the client's module host if it was hotswapped,
// or close the session if the module exited.
//
// Branch is disabled if we already sent a close frame.
res = &mut watch_hotswap, if !closed => {
if let Err(NoSuchModule) = res {
let close = CloseFrame {
code: CloseCode::Away,
reason: "module exited".into()
};
unordered_tx(close.into());
}
watch_hotswap.set(hotswap());
},
// Send ping.
//
// If we didn't receive a response to the last ping,
// we don't bother sending a fresh one.
//
// Either the connection is idle (in which case the timer will kick
// in), or there is a massive backlog to process until the pong
// appears on the ordered stream. In either case, adding more pings
// is of no value.
//
// Branch is disabled if we already sent a close frame.
_ = ping_interval.tick(), if !closed => {
let was_ponged = state.reset_ponged();
if was_ponged {
unordered_tx(UnorderedWsMessage::Ping(Bytes::new()));
}
}
}
}
}
/// A sleep that can be extended by sending it new deadlines.
///
/// Sleeps until the deadline appearing on the `activity` channel,
/// i.e. if a new deadline appears before the sleep finishes,
/// the sleep is reset to the new deadline.
///
/// The `activity` should be updated whenever a new message is received.
async fn ws_idle_timer(mut activity: watch::Receiver<Instant>) {
let mut deadline = *activity.borrow();
let sleep = sleep_until(deadline.into());
pin_mut!(sleep);
loop {
tokio::select! {
biased;
Ok(()) = activity.changed() => {
let new_deadline = *activity.borrow_and_update();
if new_deadline != deadline {
deadline = new_deadline;
sleep.as_mut().reset(deadline.into());
}
},
() = &mut sleep => {
break;
},
}
}
}
/// Consumes `ws` by composing [`ws_recv_queue`], [`ws_recv_loop`],
/// [`ws_client_message_handler`] and `message_handler`.
///
/// `idle_tx` is the sending end of a [`ws_idle_timer`]. The [`ws_recv_loop`]
/// sends a new, extended deadline whenever it receives a message.
///
/// `unordered_tx` is used to send message execution errors
/// or to initiate a close handshake.
///
/// Initiates a close handshake if the `message_handler` returns any variant
/// of [`MessageHandleError`] that is **not** [`MessageHandleError::Execution`].
///
/// Terminates if:
///
/// - the `ws` stream is exhausted
/// - or, `unordered_tx` is already closed
///
/// In the latter case, we assume that the connection is in an errored state,
/// such that we wouldn't be able to receive any more messages anyway.
async fn ws_recv_task<MessageHandler>(
state: Arc<ActorState>,
idle_tx: watch::Sender<Instant>,
client_closed_metric: IntGauge,
message_handler: impl Fn(DataMessage, Instant) -> MessageHandler,
unordered_tx: mpsc::UnboundedSender<UnorderedWsMessage>,
ws: impl Stream<Item = Result<WsMessage, WsError>> + Unpin + Send + 'static,
) where
MessageHandler: Future<Output = Result<(), MessageHandleError>>,
{
let recv_queue = ws_recv_queue(state.clone(), unordered_tx.clone(), ws);
let recv_loop = pin!(ws_recv_loop(state.clone(), idle_tx, recv_queue));
let recv_handler = ws_client_message_handler(state.clone(), client_closed_metric, recv_loop);
pin_mut!(recv_handler);
while let Some((data, timer)) = recv_handler.next().await {
let result = message_handler(data, timer).await;
if let Err(e) = result {
if let MessageHandleError::Execution(err) = e {
log::error!("{err:#}");
// If the send task has exited, also exit this recv task.
if unordered_tx.send(err.into()).is_err() {
break;
}
continue;
}
log::debug!("Client caused error: {e}");
let close = CloseFrame {
code: CloseCode::Error,
reason: format!("{e:#}").into(),
};
// If the send task has exited, also exit this recv task.
// No need to send the close handshake in that case; the client is already gone.
if unordered_tx.send(close.into()).is_err() {
break;
};
}
}
}
/// Stream that consumes a stream of [`WsMessage`]s and yields [`ClientMessage`]s.
///
/// Terminates if:
///
/// - the input stream is exhausted
/// - the input stream yields an error
///
/// If `state.closed`, continues to poll the input stream in order for the
/// websocket close handshake to complete. Any messages received while in this
/// state are dropped.
fn ws_recv_loop(
state: Arc<ActorState>,
idle_tx: watch::Sender<Instant>,
mut ws: impl Stream<Item = Result<WsMessage, WsError>> + Unpin,
) -> impl Stream<Item = ClientMessage> {
// Get the next message from `ws`, or `None` if the stream is exhausted.
//
// If `state.closed`, `ws` is drained until it either yields an `Err`, is
// exhausted, or a timeout of 250ms has elapsed.
async fn next_message(
state: &ActorState,
ws: &mut (impl Stream<Item = Result<WsMessage, WsError>> + Unpin),
) -> Option<Result<WsMessage, WsError>> {
if state.closed() {
log::trace!("drain websocket waiting for client close");
let res: Result<Option<Result<WsMessage, WsError>>, Elapsed> =
timeout(state.config.close_handshake_timeout, async {
while let Some(item) = ws.next().await {
match item {
Ok(message) => drop(message),
Err(e) => return Some(Err(e)),
}
}
None
})
.await;
match res {
Err(_elapsed) => {
log::warn!("timeout waiting for client close");
None
}
Ok(item) => item, // either error or `None`
}
} else {
log::trace!("await next client message without timeout");
ws.next().await
}
}
stream! {
loop {
let Some(res) = next_message(&state, &mut ws).await else {
log::trace!("recv stream exhausted");
break;
};
match res {
Ok(m) => {
idle_tx.send(state.next_idle_deadline()).ok();
if !state.closed() {
yield ClientMessage::from_message(m);
}
// If closed, keep polling until either:
//
// - the client sends a close frame (`ws` returns `None)
// - or `ws` yields an error
log::trace!("message received while already closed");
}
// None of the error cases can be meaningfully recovered from
// (and some can't even occur on the `ws` stream).
// Exit here but spell out an exhaustive match
// in order to bring any future library changes to our attention.
Err(e) => match e {
e @ (WsError::ConnectionClosed
| WsError::AlreadyClosed
| WsError::Io(_)
| WsError::Tls(_)
| WsError::Capacity(_)
| WsError::Protocol(_)
| WsError::WriteBufferFull(_)
| WsError::Utf8(_)
| WsError::AttackAttempt
| WsError::Url(_)
| WsError::Http(_)
| WsError::HttpFormat(_)) => {
log::warn!("Websocket receive error: {e}");
break;
}
},
}
}
}
}
/// Consumes `ws` and queues its items in a channel.
///
/// The channel is initialized with [`ActorConfig::incoming_queue_length`].
/// If it is at capacity, a connection shutdown is initiated by sending
/// [`UnorderedWsMessage::Close`] via `unordered_tx`.
///
/// Returns the channel receiver.
///
/// NOTE: This function is provided for backwards-compatibility, in particular
/// SDK clients not handling backpressure gracefully, and for observability of
/// transaction backlogging. It will probably go away in the future, see [#1851].
///
/// [#1851]: https://github.com/clockworklabs/SpacetimeDBPrivate/issues/1851
fn ws_recv_queue(
state: Arc<ActorState>,
unordered_tx: mpsc::UnboundedSender<UnorderedWsMessage>,
mut ws: impl Stream<Item = Result<WsMessage, WsError>> + Unpin + Send + 'static,
) -> impl Stream<Item = Result<WsMessage, WsError>> {
const CLOSE: UnorderedWsMessage = UnorderedWsMessage::Close(CloseFrame {
code: CloseCode::Again,
reason: Utf8Bytes::from_static("too many requests"),
});
let on_message_after_close = move |client_id| {
log::warn!("client {client_id} sent message after close or error");
};
let (tx, rx) = mpsc::channel(state.config.incoming_queue_length.get());
let rx = MeteredReceiverStream {
inner: MeteredReceiver::with_gauge(
rx,
WORKER_METRICS
.total_incoming_queue_length
.with_label_values(&state.database),
),
};
tokio::spawn(async move {
while let Some(item) = ws.next().await {
if let Err(e) = tx.try_send(item) {
match e {
// If the queue is full, disconnect the client.
mpsc::error::TrySendError::Full(item) => {
// If we can't send close (send task already terminated):
//
// - Let downstream handlers know that we're closing,
// so that remaining items in the queue are dropped.
//
// - Then exit the loop, as we won't be processing any
// more messages, and we don't expect a close response
// to arrive from the client.
if unordered_tx.send(CLOSE).is_err() {
state.close();
break;
}
// If we successfully enqueued `CLOSE`, enqueue `item`
// as well, as soon as there is space in the channel.
//
// This is to allow the client to complete the close
// handshake, for which the downstream handler needs to
// drain the queue.
//
// If `tx.send` fails, the pipeline is broken, so exit.
// See commentary on the `TrySendError::Closed` match
// arm below.
if tx.send(item).await.is_err() {
on_message_after_close(state.client_id);
break;
}
}
// If the downstream consumer went away,
// it has consumed a `Close` frame or `Err` value
// from the queue and thus has determined that it's done.
//
// Well-behaved clients shouldn't send anything after
// closing, so issue a warning.
//
// We're done either way, so break.
mpsc::error::TrySendError::Closed(_item) => {
on_message_after_close(state.client_id);
break;
}
}
}
}
});
rx
}
/// Turns a [`MeteredReceiver`] into a [`Stream`],
/// like [`tokio_stream::wrappers::ReceiverStream`] does for [`mpsc::Receiver`].
struct MeteredReceiverStream<T> {
inner: MeteredReceiver<T>,
}
impl<T> Stream for MeteredReceiverStream<T> {
type Item = T;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.inner.poll_recv(cx)
}
}
/// Stream that consumes [`ClientMessage`]s and yields [`DataMessage`]s for
/// evaluation.
///
/// Calls `state.set_ponged()` if and when the input yields a pong message.
/// Calls `state.close()` if and when the input yields a close frame,
/// i.e. the client initiated a close handshake, which we track using the
/// `client_closed_metric`.
///
/// Terminates if and when the input stream terminates.
fn ws_client_message_handler(
state: Arc<ActorState>,
client_closed_metric: IntGauge,
mut messages: impl Stream<Item = ClientMessage> + Unpin,
) -> impl Stream<Item = (DataMessage, Instant)> {
stream! {
while let Some(message) = messages.next().await {
match message {
ClientMessage::Message(message) => {
log::trace!("Received client message");
yield (message, Instant::now());
},
ClientMessage::Ping(_bytes) => {
log::trace!("Received ping from client {}", state.client_id);
// `tungstenite` will respond with `Pong` for us,
// no need to send it ourselves.
},
ClientMessage::Pong(_bytes) => {
log::trace!("Received pong from client {}", state.client_id);
state.set_ponged();
},
ClientMessage::Close(close_frame) => {
log::trace!("Received Close frame from client {}: {:?}", state.client_id, close_frame);
let was_closed = state.close();
// This is the client telling us they want to close.
if !was_closed {
client_closed_metric.inc();
}
}
}
}
log::trace!("client message handler done");
}
}
/// Outgoing messages that don't need to be ordered wrt subscription updates.
#[derive(Debug, From)]
enum UnorderedWsMessage {
/// Server-initiated close.
Close(CloseFrame),
/// Server-initiated ping.
Ping(Bytes),
/// Error calling a reducer.
///
/// The error indicates that the reducer was **not** called,
/// and can thus be unordered wrt subscription updates.
Error(MessageExecutionError),
}
/// Sink that sends outgoing messages to the `ws` sink.
///
/// Consumes `messages`, which yields subscription updates and reducer call
/// results. Note that [`SerializableMessage`]s require serialization and
/// potentially compression, which can be costly.
/// Also consumes `unordered`, which yields [`UnorderedWsMessage`]s.
///
/// Terminates if:
///
/// - `unordered` is closed
/// - an error occurs sending to the `ws` sink
///
/// If an [`UnorderedWsMessage::Close`] is encountered, a close frame is sent
/// to the `ws` sink, and `state.close()` is called. When this happens,
/// `messages` will no longer be polled (no data can be sent after a close
/// frame anyways), so `messages.close()` will be called.
///
/// Keeps polling `unordered` if `state.closed()`, but discards all data.
/// This is so `ws_client_actor_inner` keeps polling the receive end of the
/// socket until the close handshake completes -- it would otherwise exit early
/// when sending to `unordered` fails.
async fn ws_send_loop(
state: Arc<ActorState>,
config: ClientConfig,
mut ws: impl Sink<WsMessage, Error: Display> + Unpin,
mut messages: MeteredReceiver<SerializableMessage>,
mut unordered: mpsc::UnboundedReceiver<UnorderedWsMessage>,
) {
let mut messages_buf = Vec::with_capacity(32);
let mut serialize_buf = SerializeBuffer::new(config);
loop {
let closed = state.closed();
tokio::select! {
// `biased` towards the unordered queue,
// which may initiate a connection shutdown.
biased;
maybe_msg = unordered.recv() => {
let Some(msg) = maybe_msg else {
break;
};
// We shall not sent more data after a close frame,
// but keep polling `unordered` so that `ws_client_actor` keeps
// waiting for an acknowledgement from the client,
// even if it spuriously initiates another close itself.
if closed {
continue;
}
match msg {