BlueGit

ict rusty-teams

Files

rusty-teams / crates / chatsvc / src / realtime / mod.rs

mod.rs

Initial commit
metis Daniel Däschle · pushed by metis · 7 weeks ago
06e0ae51 History
mod.rs 3.1 KiB Download Raw
1mod event;
2mod host;
3mod runner;
4
5use std::pin::Pin;
6use std::task::{Context, Poll};
7use std::time::Duration;
8
9use futures_util::Stream;
10use session::Session;
11use tokio::sync::{mpsc, oneshot};
12use tokio::task::JoinHandle;
13
14pub use event::{EventKind, MessageEvent, RealtimeEvent, StatusEvent, StatusKind, decode_payload};
15pub use host::{DEFAULT_TROUTER_HOST, is_trouter_host};
16
17use crate::error::Result;
18use runner::{Runner, attach};
19
20const STOP_TIMEOUT: Duration = Duration::from_secs(15);
21
22#[derive(Debug, Clone)]
23pub struct RealtimeConfig {
24 pub host: Option<String>,
25 pub default_host: String,
26 pub discover_host: bool,
27 pub forward_presence: bool,
28 pub ensure_interval: Duration,
29 pub navigation_settle: Duration,
30 pub max_reattach_backoff: Duration,
31}
32
33impl Default for RealtimeConfig {
34 fn default() -> Self {
35 RealtimeConfig {
36 host: None,
37 default_host: DEFAULT_TROUTER_HOST.to_owned(),
38 discover_host: true,
39 forward_presence: false,
40 ensure_interval: Duration::from_secs(30),
41 navigation_settle: Duration::from_millis(1500),
42 max_reattach_backoff: Duration::from_secs(30),
43 }
44 }
45}
46
47pub struct Realtime {
48 host: String,
49 events: mpsc::UnboundedReceiver<RealtimeEvent>,
50 stop: Option<oneshot::Sender<oneshot::Sender<()>>>,
51 task: JoinHandle<()>,
52}
53
54impl Realtime {
55 /// Works on a parked tab (same origin, localStorage readable); the CDP attachment keeps it from being parked.
56 pub async fn start(session: &Session) -> Result<Realtime> {
57 Self::start_with(session, RealtimeConfig::default()).await
58 }
59
60 pub async fn start_with(session: &Session, config: RealtimeConfig) -> Result<Realtime> {
61 let session = session.clone();
62 let host = host::resolve(&session, &config).await;
63 let attached = attach(&session).await?;
64 let (output, events) = mpsc::unbounded_channel();
65 let (stop, stop_receiver) = oneshot::channel();
66 let mut runner = Runner::new(session, config, host.clone(), output);
67 if let Err(error) = runner.ensure().await {
68 attached.shutdown().await;
69 return Err(error.into());
70 }
71 let task = tokio::spawn(runner.run(attached, stop_receiver));
72 Ok(Realtime {
73 host,
74 events,
75 stop: Some(stop),
76 task,
77 })
78 }
79
80 pub fn host(&self) -> &str {
81 &self.host
82 }
83
84 pub async fn recv(&mut self) -> Option<RealtimeEvent> {
85 self.events.recv().await
86 }
87
88 /// Deletes the registration and closes the page-side socket.
89 pub async fn stop(mut self) {
90 if let Some(stop) = self.stop.take() {
91 let (done, finished) = oneshot::channel();
92 if stop.send(done).is_ok() {
93 let _ = tokio::time::timeout(STOP_TIMEOUT, finished).await;
94 }
95 }
96 let _ = self.task.await;
97 }
98}
99
100impl Stream for Realtime {
101 type Item = RealtimeEvent;
102
103 fn poll_next(
104 mut self: Pin<&mut Self>,
105 context: &mut Context<'_>,
106 ) -> Poll<Option<RealtimeEvent>> {
107 self.events.poll_recv(context)
108 }
109}