rivetkit-client 2.3.1

Rust client for RivetKit - the Stateful Serverless Framework for building AI agents, realtime apps, and game servers
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
use anyhow::Result;
use futures_util::FutureExt;
use parking_lot::Mutex as SyncMutex;
use scc::{hash_map::Entry as SccEntry, HashMap as SccHashMap};
use serde_json::Value;
use std::fmt::Debug;
use std::ops::Deref;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Weak};
use std::time::Duration;
use tokio::sync::{broadcast, oneshot, watch, Mutex};

use crate::{
	backoff::Backoff,
	drivers::*,
	protocol::{query::ActorQuery, *},
	remote_manager::RemoteManager,
	EncodingKind, TransportKind,
};
use tracing::debug;

type RpcResponse = Result<to_client::ActionResponse, to_client::Error>;
type EventCallback = dyn Fn(Event) + Send + Sync;
type VoidCallback = dyn Fn() + Send + Sync;
type ErrorCallback = dyn Fn(&str) + Send + Sync;
type StatusCallback = dyn Fn(ConnectionStatus) + Send + Sync;

#[derive(Debug, Clone)]
pub struct Event {
	pub name: String,
	pub args: Vec<Value>,
	pub raw_args: Vec<u8>,
}

struct EventSubscription {
	id: u64,
	callback: Box<EventCallback>,
}

#[derive(Clone)]
pub struct SubscriptionHandle {
	inner: Arc<SubscriptionHandleInner>,
}

struct SubscriptionHandleInner {
	conn: Weak<ActorConnectionInner>,
	event_name: String,
	id: u64,
	active: AtomicBool,
}

impl SubscriptionHandle {
	fn new(conn: &Arc<ActorConnectionInner>, event_name: String, id: u64) -> Self {
		Self {
			inner: Arc::new(SubscriptionHandleInner {
				conn: Arc::downgrade(conn),
				event_name,
				id,
				active: AtomicBool::new(true),
			}),
		}
	}

	pub async fn unsubscribe(&self) {
		if !self.inner.active.swap(false, Ordering::SeqCst) {
			return;
		}

		let Some(conn) = self.inner.conn.upgrade() else {
			return;
		};

		conn.remove_event_subscription(&self.inner.event_name, self.inner.id)
			.await;
	}
}

struct SendMsgOpts {
	ephemeral: bool,
}

impl Default for SendMsgOpts {
	fn default() -> Self {
		Self { ephemeral: false }
	}
}

// struct WatchPair {
//     tx: watch::Sender<bool>,
//     rx: watch::Receiver<bool>,
// }
type WatchPair = (watch::Sender<bool>, watch::Receiver<bool>);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConnectionStatus {
	Idle,
	Connecting,
	Connected,
	Disconnected,
}

pub type ActorConnection = Arc<ActorConnectionInner>;

struct ConnectionAttempt {
	did_open: bool,
	_task_end_reason: DriverStopReason,
}

pub struct ActorConnectionInner {
	remote_manager: RemoteManager,
	transport_kind: TransportKind,
	encoding_kind: EncodingKind,
	query: ActorQuery,
	parameters: Option<Value>,

	driver: Mutex<Option<DriverHandle>>,
	msg_queue: Mutex<Vec<Arc<to_server::ToServer>>>,

	rpc_counter: AtomicU64,
	event_subscription_counter: AtomicU64,
	in_flight_rpcs: SccHashMap<u64, oneshot::Sender<RpcResponse>>,

	event_subscriptions: SccHashMap<String, Vec<Arc<EventSubscription>>>,
	on_open_callbacks: Mutex<Vec<Box<VoidCallback>>>,
	on_close_callbacks: Mutex<Vec<Box<VoidCallback>>>,
	on_error_callbacks: Mutex<Vec<Box<ErrorCallback>>>,
	on_status_change_callbacks: Mutex<Vec<Box<StatusCallback>>>,

	// Connection info for reconnection
	actor_id: Mutex<Option<String>>,
	connection_id: Mutex<Option<String>>,
	connection_token: Mutex<Option<String>>,

	dc_watch: WatchPair,
	status_watch: (
		watch::Sender<ConnectionStatus>,
		watch::Receiver<ConnectionStatus>,
	),
	disconnection_rx: Mutex<Option<oneshot::Receiver<()>>>,
}

impl ActorConnectionInner {
	pub(crate) fn new(
		remote_manager: RemoteManager,
		query: ActorQuery,
		transport_kind: TransportKind,
		encoding_kind: EncodingKind,
		parameters: Option<Value>,
	) -> ActorConnection {
		Arc::new(Self {
			remote_manager,
			transport_kind,
			encoding_kind,
			query,
			parameters,
			driver: Mutex::new(None),
			msg_queue: Mutex::new(Vec::new()),
			rpc_counter: AtomicU64::new(0),
			event_subscription_counter: AtomicU64::new(0),
			in_flight_rpcs: SccHashMap::new(),
			event_subscriptions: SccHashMap::new(),
			on_open_callbacks: Mutex::new(Vec::new()),
			on_close_callbacks: Mutex::new(Vec::new()),
			on_error_callbacks: Mutex::new(Vec::new()),
			on_status_change_callbacks: Mutex::new(Vec::new()),
			actor_id: Mutex::new(None),
			connection_id: Mutex::new(None),
			connection_token: Mutex::new(None),
			dc_watch: watch::channel(false),
			status_watch: watch::channel(ConnectionStatus::Idle),
			disconnection_rx: Mutex::new(None),
		})
	}

	fn is_disconnecting(self: &Arc<Self>) -> bool {
		*self.dc_watch.1.borrow() == true
	}

	async fn try_connect(self: &Arc<Self>) -> ConnectionAttempt {
		self.set_status(ConnectionStatus::Connecting).await;

		// Get connection info for reconnection
		let conn_id = self.connection_id.lock().await.clone();
		let conn_token = self.connection_token.lock().await.clone();

		let (driver, mut recver, task) = match connect_driver(
			self.transport_kind,
			DriverConnectArgs {
				remote_manager: self.remote_manager.clone(),
				query: self.query.clone(),
				encoding_kind: self.encoding_kind,
				parameters: self.parameters.clone(),
				conn_id,
				conn_token,
			},
		)
		.await
		{
			Ok(value) => value,
			Err(error) => {
				let message = error.to_string();
				self.emit_error(&message).await;
				self.set_status(ConnectionStatus::Disconnected).await;
				return ConnectionAttempt {
					did_open: false,
					_task_end_reason: DriverStopReason::TaskError,
				};
			}
		};

		{
			let mut my_driver = self.driver.lock().await;
			*my_driver = Some(driver);
		}

		let mut task_end_reason = task.map(|res| match res {
			Ok(a) => a,
			Err(task_err) => {
				if task_err.is_cancelled() {
					debug!("Connection task was cancelled");
					DriverStopReason::UserAborted
				} else {
					DriverStopReason::TaskError
				}
			}
		});

		let mut did_connection_open = false;

		// spawn listener for rpcs
		let task_end_reason = loop {
			tokio::select! {
				reason = &mut task_end_reason => {
					debug!("Connection closed: {:?}", reason);

					break reason;
				},
				msg = recver.recv() => {
					// If the sender is dropped, break the loop
					let Some(msg) = msg else {
						// break DriverStopReason::ServerDisconnect;
						continue;
					};

					if let to_client::ToClientBody::Init(_) = &msg.body {
						did_connection_open = true;
					}

					self.on_message(msg).await;
				}
			}
		};

		'destroy_driver: {
			debug!("Destroying driver");
			let mut d_guard = self.driver.lock().await;
			let Some(d) = d_guard.take() else {
				// We destroyed the driver already,
				// e.g. .disconnect() was called
				break 'destroy_driver;
			};

			d.disconnect();
		}

		self.set_status(ConnectionStatus::Disconnected).await;
		self.emit_close().await;

		ConnectionAttempt {
			did_open: did_connection_open,
			_task_end_reason: task_end_reason,
		}
	}

	async fn handle_open(self: &Arc<Self>, init: &to_client::Init) {
		debug!("Connected to server: {:?}", init);

		// Store connection info for reconnection
		*self.actor_id.lock().await = Some(init.actor_id.clone());
		*self.connection_id.lock().await = Some(init.connection_id.clone());
		*self.connection_token.lock().await = init.connection_token.clone();
		self.set_status(ConnectionStatus::Connected).await;
		self.emit_open().await;

		let mut event_names = Vec::new();
		self.event_subscriptions
			.iter_async(|event_name, _| {
				event_names.push(event_name.clone());
				true
			})
			.await;
		for event_name in event_names {
			self.send_subscription(event_name.clone(), true).await;
		}

		// Flush message queue
		for msg in self.msg_queue.lock().await.drain(..) {
			// If its in the queue, it isn't ephemeral, so we pass
			// default SendMsgOpts
			self.send_msg(msg, SendMsgOpts::default()).await;
		}
	}

	async fn on_message(self: &Arc<Self>, msg: Arc<to_client::ToClient>) {
		let body = &msg.body;

		match body {
			to_client::ToClientBody::Init(init) => {
				self.handle_open(init).await;
			}
			to_client::ToClientBody::ActionResponse(ar) => {
				let id = ar.id;
				let Some((_, tx)) = self.in_flight_rpcs.remove_async(&id).await else {
					debug!("Unexpected response: rpc id not found");
					return;
				};
				if let Err(e) = tx.send(Ok(ar.clone())) {
					debug!("{:?}", e);
					return;
				}
			}
			to_client::ToClientBody::Event(ev) => {
				let args = decode_event_args(&ev.args);

				let callbacks = {
					self.event_subscriptions
						.read_async(&ev.name, |_, listeners| listeners.clone())
						.await
						.unwrap_or_default()
				};
				let event = Event {
					name: ev.name.clone(),
					args,
					raw_args: ev.args.clone(),
				};
				for subscription in callbacks {
					(subscription.callback)(event.clone());
				}
			}
			to_client::ToClientBody::Error(e) => {
				if let Some(action_id) = e.action_id {
					let Some((_, tx)) = self.in_flight_rpcs.remove_async(&action_id).await else {
						debug!("Unexpected response: rpc id not found");
						return;
					};
					if let Err(e) = tx.send(Err(e.clone())) {
						debug!("{:?}", e);
						return;
					}

					return;
				}

				debug!("Connection error: {} - {}", e.code, e.message);
				self.emit_error(&e.message).await;
			}
		}
	}

	async fn set_status(self: &Arc<Self>, status: ConnectionStatus) {
		if *self.status_watch.1.borrow() == status {
			return;
		}
		self.status_watch.0.send(status).ok();
		for callback in self.on_status_change_callbacks.lock().await.iter() {
			callback(status);
		}
	}

	async fn emit_open(self: &Arc<Self>) {
		for callback in self.on_open_callbacks.lock().await.iter() {
			callback();
		}
	}

	async fn emit_close(self: &Arc<Self>) {
		for callback in self.on_close_callbacks.lock().await.iter() {
			callback();
		}
	}

	async fn emit_error(self: &Arc<Self>, message: &str) {
		for callback in self.on_error_callbacks.lock().await.iter() {
			callback(message);
		}
	}

	async fn send_msg(self: &Arc<Self>, msg: Arc<to_server::ToServer>, opts: SendMsgOpts) {
		let guard = self.driver.lock().await;

		'send_immediately: {
			let Some(driver) = guard.deref() else {
				break 'send_immediately;
			};

			let Ok(_) = driver.send(msg.clone()).await else {
				break 'send_immediately;
			};

			return;
		}

		// Otherwise queue
		if opts.ephemeral == false {
			self.msg_queue.lock().await.push(msg.clone());
		}

		return;
	}

	pub async fn action(self: &Arc<Self>, method: &str, params: Vec<Value>) -> Result<Value> {
		let id: u64 = self.rpc_counter.fetch_add(1, Ordering::SeqCst);

		let (tx, rx) = oneshot::channel();
		if self.in_flight_rpcs.insert_async(id, tx).await.is_err() {
			return Err(anyhow::anyhow!("duplicate rpc id"));
		}

		// Encode params as CBOR
		let args_cbor = serde_cbor::to_vec(&params)?;

		self.send_msg(
			Arc::new(to_server::ToServer {
				body: to_server::ToServerBody::ActionRequest(to_server::ActionRequest {
					id,
					name: method.to_string(),
					args: args_cbor,
				}),
			}),
			SendMsgOpts::default(),
		)
		.await;

		let Ok(res) = rx.await else {
			return Err(anyhow::anyhow!("Socket closed during rpc"));
		};

		match res {
			Ok(ok) => {
				// Decode CBOR output
				let output: Value = serde_cbor::from_slice(&ok.output)?;
				Ok(output)
			}
			Err(err) => {
				let metadata = if let Some(md) = &err.metadata {
					match serde_cbor::from_slice::<Value>(md) {
						Ok(v) => v,
						Err(_) => Value::Null,
					}
				} else {
					Value::Null
				};

				Err(anyhow::anyhow!(
					"RPC Error({}/{}): {}, {:#}",
					err.group,
					err.code,
					err.message,
					metadata
				))
			}
		}
	}

	async fn send_subscription(self: &Arc<Self>, event_name: String, subscribe: bool) {
		self.send_msg(
			Arc::new(to_server::ToServer {
				body: to_server::ToServerBody::SubscriptionRequest(
					to_server::SubscriptionRequest {
						event_name,
						subscribe,
					},
				),
			}),
			SendMsgOpts { ephemeral: true },
		)
		.await;
	}

	async fn add_event_subscription(
		self: &Arc<Self>,
		event_name: String,
		callback: Box<EventCallback>,
	) -> SubscriptionHandle {
		let id = self
			.event_subscription_counter
			.fetch_add(1, Ordering::SeqCst);
		let handle = SubscriptionHandle::new(self, event_name.clone(), id);

		self.insert_event_subscription(event_name, id, callback)
			.await;

		handle
	}

	async fn insert_event_subscription(
		self: &Arc<Self>,
		event_name: String,
		id: u64,
		callback: Box<EventCallback>,
	) {
		let is_new_subscription = {
			let mut listeners = self
				.event_subscriptions
				.entry_async(event_name.clone())
				.await
				.or_insert_with(Vec::new);
			let is_new_subscription = listeners.is_empty();

			listeners.push(Arc::new(EventSubscription { id, callback }));

			is_new_subscription
		};

		if is_new_subscription {
			self.send_subscription(event_name, true).await;
		}
	}

	async fn remove_event_subscription(self: &Arc<Self>, event_name: &str, id: u64) {
		let should_unsubscribe = {
			match self
				.event_subscriptions
				.entry_async(event_name.to_string())
				.await
			{
				SccEntry::Occupied(mut entry) => {
					entry.retain(|subscription| subscription.id != id);
					if entry.is_empty() {
						let _ = entry.remove_entry();
						true
					} else {
						false
					}
				}
				SccEntry::Vacant(entry) => {
					drop(entry);
					false
				}
			}
		};

		if should_unsubscribe {
			self.send_subscription(event_name.to_string(), false).await;
		}
	}

	pub async fn on_event<F>(self: &Arc<Self>, event_name: &str, callback: F) -> SubscriptionHandle
	where
		F: Fn(&Vec<Value>) + Send + Sync + 'static,
	{
		self.add_event_subscription(
			event_name.to_string(),
			Box::new(move |event| callback(&event.args)),
		)
		.await
	}

	pub async fn on_event_raw<F>(
		self: &Arc<Self>,
		event_name: &str,
		callback: F,
	) -> SubscriptionHandle
	where
		F: Fn(Event) + Send + Sync + 'static,
	{
		self.add_event_subscription(event_name.to_string(), Box::new(callback))
			.await
	}

	pub async fn once_event<F>(
		self: &Arc<Self>,
		event_name: &str,
		callback: F,
	) -> SubscriptionHandle
	where
		F: FnOnce(Event) + Send + 'static,
	{
		let id = self
			.event_subscription_counter
			.fetch_add(1, Ordering::SeqCst);
		let handle = SubscriptionHandle::new(self, event_name.to_string(), id);
		// Event callbacks are synchronous, so a FnOnce lives behind a short sync lock.
		let callback = Arc::new(SyncMutex::new(Some(callback)));
		let unsubscribe_handle = handle.clone();
		let fired = Arc::new(AtomicBool::new(false));
		self.insert_event_subscription(
			event_name.to_string(),
			id,
			Box::new(move |event| {
				if fired.swap(true, Ordering::SeqCst) {
					return;
				}

				let unsubscribe_handle = unsubscribe_handle.clone();
				tokio::spawn(async move {
					unsubscribe_handle.unsubscribe().await;
				});

				let Some(callback) = callback.lock().take() else {
					return;
				};
				callback(event);
			}),
		)
		.await;

		handle
	}

	pub async fn on_open<F>(self: &Arc<Self>, callback: F)
	where
		F: Fn() + Send + Sync + 'static,
	{
		self.on_open_callbacks.lock().await.push(Box::new(callback));
	}

	pub async fn on_close<F>(self: &Arc<Self>, callback: F)
	where
		F: Fn() + Send + Sync + 'static,
	{
		self.on_close_callbacks
			.lock()
			.await
			.push(Box::new(callback));
	}

	pub async fn on_error<F>(self: &Arc<Self>, callback: F)
	where
		F: Fn(&str) + Send + Sync + 'static,
	{
		self.on_error_callbacks
			.lock()
			.await
			.push(Box::new(callback));
	}

	pub async fn on_status_change<F>(self: &Arc<Self>, callback: F)
	where
		F: Fn(ConnectionStatus) + Send + Sync + 'static,
	{
		self.on_status_change_callbacks
			.lock()
			.await
			.push(Box::new(callback));
	}

	pub fn conn_status(self: &Arc<Self>) -> ConnectionStatus {
		*self.status_watch.1.borrow()
	}

	pub fn status_receiver(self: &Arc<Self>) -> watch::Receiver<ConnectionStatus> {
		self.status_watch.1.clone()
	}

	pub async fn disconnect(self: &Arc<Self>) {
		if self.is_disconnecting() {
			// We are already disconnecting
			return;
		}

		debug!("Disconnecting from actor conn");

		self.dc_watch.0.send(true).ok();
		self.set_status(ConnectionStatus::Disconnected).await;

		if let Some(d) = self.driver.lock().await.deref() {
			d.disconnect();
		}
		self.in_flight_rpcs.clear_async().await;
		self.event_subscriptions.clear_async().await;
		let Some(rx) = self.disconnection_rx.lock().await.take() else {
			return;
		};

		rx.await.ok();
	}

	pub async fn dispose(self: &Arc<Self>) {
		self.disconnect().await
	}
}

pub fn start_connection(
	conn: &Arc<ActorConnectionInner>,
	mut shutdown_rx: broadcast::Receiver<()>,
) {
	let (tx, rx) = oneshot::channel();

	let conn = conn.clone();

	tokio::spawn(async move {
		{
			let mut stop_rx = conn.disconnection_rx.lock().await;
			if stop_rx.is_some() {
				// Already doing connection_with_retry
				// - this drops the oneshot
				return;
			}

			*stop_rx = Some(rx);
		}

		'keepalive: loop {
			debug!("Attempting to reconnect");
			let mut backoff = Backoff::new(Duration::from_secs(1), Duration::from_secs(30));
			let mut retry_attempt = 0;
			'retry: loop {
				retry_attempt += 1;
				debug!(
					"Establish conn: attempt={}, timeout={:?}",
					retry_attempt,
					backoff.delay()
				);
				let attempt = conn.try_connect().await;

				if conn.is_disconnecting() {
					break 'keepalive;
				}

				if attempt.did_open {
					break 'retry;
				}

				let mut dc_rx = conn.dc_watch.0.subscribe();

				tokio::select! {
					_ = backoff.tick() => {},
					_ = dc_rx.wait_for(|x| *x == true) => {
						break 'keepalive;
					}
					_ = shutdown_rx.recv() => {
						debug!("Received shutdown signal, stopping connection attempts");
						break 'keepalive;
					}
				}
			}
		}

		tx.send(()).ok();
		conn.disconnection_rx.lock().await.take();
	});
}

impl Debug for ActorConnectionInner {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		f.debug_struct("ActorConnection")
			.field("transport_kind", &self.transport_kind)
			.field("encoding_kind", &self.encoding_kind)
			.finish()
	}
}

fn decode_event_args(raw_args: &[u8]) -> Vec<Value> {
	match serde_cbor::from_slice::<Vec<Value>>(raw_args) {
		Ok(args) => args,
		Err(vector_error) => match serde_cbor::from_slice::<Value>(raw_args) {
			Ok(Value::Array(args)) => args,
			Ok(value) => vec![value],
			Err(value_error) => {
				debug!(?vector_error, ?value_error, "failed to decode event args");
				Vec::new()
			}
		},
	}
}