Skip to main content

polyoxide_ws/supervisor/
mod.rs

1//! The generic supervision loop (AD-11): one task per supervised connection,
2//! which keeps it alive, replaces it, replays its membership and reports its
3//! outages, for any venue that describes its protocol as a [`Protocol`].
4
5mod task;
6
7use std::{
8    future::Future,
9    pin::Pin,
10    task::{Context, Poll},
11    time::Duration,
12};
13
14use futures_util::Stream;
15use tokio::sync::{mpsc, oneshot};
16
17use crate::{
18    tungstenite::{self, handshake::client::Request, Message},
19    Connection, Socket, Timing, WsError,
20};
21use task::{Command, Receiver, Start, Task};
22
23/// How many membership changes may wait for the task before a caller waits
24/// to hand one over.
25const COMMANDS: usize = 16;
26
27/// What to do after a failure.
28#[derive(Debug, Clone, Copy, PartialEq, Eq)]
29#[non_exhaustive]
30pub enum Recovery {
31    /// Replace the connection: back off, connect again and replay the
32    /// membership.
33    Reconnect,
34    /// Yield the error and keep the connection, such as for one frame that
35    /// did not decode. A failed connection attempt has no connection to keep,
36    /// so there it reads as [`Reconnect`](Self::Reconnect).
37    Skip,
38    /// Yield the error and end the stream: retrying replays the same failure.
39    Stop,
40}
41
42/// The queue between the task and the consumer.
43#[derive(Debug, Clone, Copy, PartialEq, Eq)]
44#[non_exhaustive]
45pub enum Queue {
46    /// At most this many items unread (at least one). While it is full the
47    /// task reads nothing more from the connection, and still pings.
48    Bounded(usize),
49    /// No limit: the task never waits for the consumer.
50    Unbounded,
51}
52
53/// What one frame decodes to.
54#[derive(Debug)]
55#[non_exhaustive]
56pub enum Decoded<E, Err> {
57    /// Nothing for the consumer: a control frame, an answer the protocol
58    /// reads itself, or a frame it skips.
59    Nothing,
60    /// One event.
61    Event(E),
62    /// Several events, in order, such as one the protocol originates ahead of
63    /// the update it concerns.
64    Events(Vec<E>),
65    /// A failure, which [`Protocol::recover`] decides.
66    Error(Err),
67}
68
69/// What becomes of a membership change made while no connection is up.
70#[derive(Debug)]
71#[non_exhaustive]
72pub enum Offline<C, A, E> {
73    /// Recorded in the membership for the replay to apply; the caller gets
74    /// this answer now.
75    Recorded(Result<A, E>),
76    /// Held, and applied with [`Protocol::apply`] once a connection is up and
77    /// its membership replayed; the caller waits for that answer.
78    Held(C),
79}
80
81/// Why a connection went down, for [`Protocol::disconnected`], which owns it,
82/// so a protocol's marker can carry the error.
83#[derive(Debug)]
84#[non_exhaustive]
85pub enum Outage<E> {
86    /// The server closed the connection: with a close frame, whose code and
87    /// reason these are, or by ending the stream with none (no code and an
88    /// empty reason).
89    Closed {
90        /// The close code, when a close frame carried one.
91        code: Option<u16>,
92        /// The close reason, empty when none was given.
93        reason: String,
94    },
95    /// A failure: a read, a ping, a decoded frame [`Protocol::recover`] sent
96    /// to a reconnect, or the first connection attempt of
97    /// [`Supervisor::start`].
98    Error(E),
99    /// Nothing that counts as liveness arrived for the staleness window, or a
100    /// membership change went unanswered for it.
101    Stale {
102        /// The silence, less any time the consumer's queue was full.
103        silent: Duration,
104    },
105    /// Replaced on purpose at [`Timing::max_connection_age`].
106    Rotation,
107}
108
109/// A venue's socket protocol, as the [`Supervisor`] drives it.
110///
111/// The supervisor owns the rules every venue shares (AD-11): pings on the
112/// wall clock, staleness, backoff, rotation, the outage markers and the
113/// consumer's queue. A protocol says what is its own, and each divergence row
114/// stays here, at the venue:
115///
116/// - **D4, the ping:** [`ping`](Self::ping), sent every
117///   [`Timing::ping_interval`] whatever the traffic.
118/// - **D5, liveness:** [`is_live`](Self::is_live), which restarts the
119///   staleness window.
120/// - **D6, a delivered connection:** [`delivered`](Self::delivered), which
121///   resets the backoff when the connection ends.
122/// - **D8, outage markers:** [`disconnected`](Self::disconnected), or none.
123/// - **D19 and the rest of recovery:** [`recover`](Self::recover).
124///
125/// The hooks that talk to the server, [`replay`](Self::replay) and
126/// [`apply`](Self::apply), borrow the [`Connection`], so they use its request
127/// correlation. A request is not cancel-safe, so the supervisor bounds each by
128/// [`Timing::stale_after`] and replaces a connection that outlives it.
129///
130/// The trait is generic only, and its async hooks return `impl Future + Send`.
131pub trait Protocol: Send + Sized + 'static {
132    /// What the stream yields.
133    type Event: Send + 'static;
134    /// What the stream and the membership calls fail with. Every socket
135    /// failure arrives as a [`WsError`] first.
136    type Error: std::error::Error + From<WsError> + Send + 'static;
137    /// What the connection carries, such as its subscriptions.
138    type Membership: Send + 'static;
139    /// One change to the membership.
140    type Change: Send + 'static;
141    /// The answer to a change.
142    type Ack: Send + 'static;
143
144    /// The handshake request for one connection attempt, built afresh for
145    /// each, so any authentication in it is recomputed.
146    fn handshake(&mut self) -> Result<Request, Self::Error>;
147
148    /// Open the connection for one attempt, within `timeout`.
149    ///
150    /// The [`Supervisor`] calls it for every attempt, the first and each
151    /// reconnect, with [`Timing::connect_timeout`]. By default it builds
152    /// [`handshake`](Self::handshake)'s request first, so a failure there is
153    /// the attempt's, and dials it with the kit's [`connect`](crate::connect).
154    /// A protocol that dials its venue a step at a time, such as a venue that
155    /// tries each resolved address in turn, overrides it: it bounds each step
156    /// by `timeout` itself, upgrades with [`connect_over`](crate::connect_over),
157    /// and may keep `handshake` for the request it upgrades with. An error is a
158    /// failed attempt, which [`recover`](Self::recover) decides on.
159    fn connect(
160        &mut self,
161        timeout: Duration,
162    ) -> impl Future<Output = Result<Socket, Self::Error>> + Send {
163        let request = self.handshake();
164        async move {
165            let socket = crate::connect(request?, timeout).await?;
166            Ok::<_, Self::Error>(socket)
167        }
168    }
169
170    /// Shape each fresh connection before anything is sent on it, such as
171    /// with [`Connection::min_request_interval`] for a venue that paces its
172    /// requests. By default the connection is left as it is.
173    fn configure(&self, connection: Connection) -> Connection {
174        connection
175    }
176
177    /// Put the membership on a fresh connection, paced as the venue requires.
178    /// A failure is [`recover`](Self::recover)ed as a failed attempt.
179    fn replay(
180        &mut self,
181        connection: &mut Connection,
182        membership: &Self::Membership,
183    ) -> impl Future<Output = Result<(), Self::Error>> + Send;
184
185    /// Make one change on a live connection, and record it in the membership
186    /// as far as it took effect. The caller gets the answer; an error keeps
187    /// the connection, unless the request outlived the staleness window.
188    fn apply(
189        &mut self,
190        connection: &mut Connection,
191        membership: &mut Self::Membership,
192        change: Self::Change,
193    ) -> impl Future<Output = Result<Self::Ack, Self::Error>> + Send;
194
195    /// What becomes of a change made during an outage. By default it is held
196    /// and [`apply`](Self::apply)'d once a connection is up.
197    fn record(
198        &mut self,
199        membership: &mut Self::Membership,
200        change: Self::Change,
201    ) -> Offline<Self::Change, Self::Ack, Self::Error> {
202        let _ = membership;
203        Offline::Held(change)
204    }
205
206    /// What one frame means: Text, Binary, Ping, Pong or Close.
207    fn decode(&mut self, frame: &Message) -> Decoded<Self::Event, Self::Error>;
208
209    /// Whether this frame, decoded so, proves the connection alive and
210    /// restarts the staleness window (D5).
211    fn is_live(&self, frame: &Message, decoded: &Decoded<Self::Event, Self::Error>) -> bool;
212
213    /// Whether this frame, decoded so, makes the connection one that
214    /// delivered something, which resets the backoff when it ends (D6).
215    fn delivered(&self, frame: &Message, decoded: &Decoded<Self::Event, Self::Error>) -> bool;
216
217    /// The ping to send at a ping tick, or `None` for none (D4). It is sent
218    /// without awaiting an answer; the answer is a frame like any other.
219    fn ping(&mut self, connection: &mut Connection) -> Option<Message>;
220
221    /// Whether the membership still wants a connection. One that stops being
222    /// wanted on a healthy connection closes it politely, with no marker, and
223    /// ends the stream; during an outage it ends the outage with
224    /// [`reconnected`](Self::reconnected) and then the stream.
225    fn wanted(membership: &Self::Membership) -> bool {
226        let _ = membership;
227        true
228    }
229
230    /// Whether reconnecting can help after this error. Required, since only
231    /// the venue knows its classes; it is what [`recover`](Self::recover)
232    /// reads by default.
233    fn is_retriable(err: &Self::Error) -> bool;
234
235    /// What to do after this error: by default
236    /// [`Reconnect`](Recovery::Reconnect) exactly when
237    /// [`is_retriable`](Self::is_retriable) says so, and
238    /// [`Stop`](Recovery::Stop) otherwise.
239    fn recover(&self, err: &Self::Error) -> Recovery {
240        if Self::is_retriable(err) {
241            Recovery::Reconnect
242        } else {
243            Recovery::Stop
244        }
245    }
246
247    /// How long the server asked the client to wait after this error, such
248    /// as a refused upgrade's `Retry-After`; `None` by default.
249    ///
250    /// The reconnect that follows waits at least this, clamped to
251    /// [`Timing::max_backoff`], and never less than the backoff's own step:
252    /// a server's wait only extends the delay (DRIFT R11). A venue answers
253    /// it from its error's class, the one place that reads the header.
254    fn retry_after(&self, err: &Self::Error) -> Option<Duration> {
255        let _ = err;
256        None
257    }
258
259    /// The event announcing an outage, or `None` for a protocol that
260    /// declares none (the default). Called at most once per outage.
261    fn disconnected(&mut self, outage: Outage<Self::Error>) -> Option<Self::Event> {
262        let _ = outage;
263        None
264    }
265
266    /// The event ending an outage, yielded exactly once per outage: after a
267    /// fresh connection has replayed the membership, or when the membership
268    /// stopped being wanted during the outage.
269    fn reconnected(&mut self) -> Self::Event;
270
271    /// Forget what belongs to the previous connection, before a fresh one
272    /// replays the membership.
273    fn reset(&mut self) {}
274
275    /// The queue between the task and the consumer.
276    fn queue(&self) -> Queue;
277
278    /// The error for a membership change, or a connection attempt, that the
279    /// server left unanswered for the staleness window. By default a
280    /// [`Kind::NoAnswer`](crate::Kind::NoAnswer).
281    fn stalled(silent: Duration) -> Self::Error {
282        WsError::no_answer(silent).into()
283    }
284
285    /// The error for a membership change, or a [`Supervised::close`], once
286    /// the task has stopped. By default a
287    /// [`Kind::AlreadyClosed`](crate::Kind::AlreadyClosed).
288    fn stopped() -> Self::Error {
289        WsError::from(tungstenite::Error::AlreadyClosed).into()
290    }
291}
292
293/// Starts a supervised connection for a [`Protocol`].
294///
295/// The task it spawns holds one connection at a time and keeps these rules
296/// (AD-11):
297///
298/// - **Pings on the wall clock.** A ping goes out every
299///   [`Timing::ping_interval`] whatever the traffic, and also while the
300///   consumer's queue is full: the task then races the queue's space against
301///   the ping timer and reads nothing more, and the time it waits does not
302///   count as silence. None goes out while a membership change is in flight,
303///   which [`Timing::stale_after`] bounds.
304/// - **Staleness.** Silence for [`Timing::stale_after`], by the protocol's
305///   liveness rule, replaces the connection.
306/// - **No drops.** No decoded event is dropped; a bounded queue holds the
307///   task back instead.
308/// - **Outage markers.** At most one [`Protocol::disconnected`] per outage,
309///   and exactly one [`Protocol::reconnected`], even when the membership
310///   stops being wanted during the outage.
311/// - **Not wanted.** A membership that stops being wanted on a healthy
312///   connection closes it politely, with no marker, and ends the stream.
313/// - **Rotation.** A connection older than [`Timing::max_connection_age`] is
314///   closed politely and replaced at once.
315/// - **Membership during an outage.** A change made during an outage goes to
316///   [`Protocol::record`], and a held one is applied after the reconnect.
317/// - **The consumer gone.** Dropping [`Supervised`] ends the task promptly,
318///   and [`Supervised::close`] does not wait on a consumer that never read.
319///
320/// Reconnects wait out [`Timing::backoff`]'s schedule, with no cap on
321/// attempts, or the [`Protocol::retry_after`] of the failure before them when
322/// that is longer, up to [`Timing::max_backoff`] (DRIFT R11); each attempt is
323/// bounded by [`Timing::connect_timeout`]. The task logs under the
324/// `polyoxide_ws` target.
325///
326/// The basis is one socket crate's former supervised tier (`ws/supervised.rs`,
327/// deleted in Story 4.6), with another socket crate's former loop (replaced
328/// in Story 4.7 by a router over one supervisor per path) for rotation,
329/// outage markers, changes recorded during an outage, the start in an outage
330/// and the consumer's departure. Neither kept pinging behind a full queue.
331#[derive(Debug)]
332pub struct Supervisor<P: Protocol> {
333    protocol: P,
334    timing: Timing,
335}
336
337impl<P: Protocol> Supervisor<P> {
338    /// A supervisor for `protocol` on `timing`.
339    pub fn new(protocol: P, timing: Timing) -> Self {
340        Self { protocol, timing }
341    }
342
343    /// Connect, replay `membership`, and start supervising. Any failure of
344    /// that first attempt is returned, so a setup that is wrong is reported
345    /// rather than retried.
346    pub async fn connect(
347        mut self,
348        mut membership: P::Membership,
349    ) -> Result<Supervised<P>, P::Error> {
350        let live = task::attempt(&mut self.protocol, &self.timing, &mut membership).await?;
351        Ok(self.spawn(membership, Start::Up(Box::new(live))))
352    }
353
354    /// As [`connect`](Self::connect), except that a first failure
355    /// [`Protocol::recover`] would retry starts the stream in an outage: the
356    /// protocol's [`disconnected`](Protocol::disconnected) for it, then the
357    /// backoff and the attempts that follow. Only a failure it would
358    /// [`Stop`](Recovery::Stop) on is returned.
359    pub async fn start(mut self, mut membership: P::Membership) -> Result<Supervised<P>, P::Error> {
360        match task::attempt(&mut self.protocol, &self.timing, &mut membership).await {
361            Ok(live) => Ok(self.spawn(membership, Start::Up(Box::new(live)))),
362            Err(err) => match self.protocol.recover(&err) {
363                Recovery::Stop => Err(err),
364                Recovery::Reconnect | Recovery::Skip => {
365                    Ok(self.spawn(membership, Start::Outage(err)))
366                }
367            },
368        }
369    }
370
371    fn spawn(self, membership: P::Membership, start: Start<P>) -> Supervised<P> {
372        let (events, receiver) = task::queue(self.protocol.queue());
373        let (commands, commands_rx) = mpsc::channel(COMMANDS);
374        let task = Task::new(self.protocol, self.timing, membership, events, commands_rx);
375        Supervised {
376            events: receiver,
377            commands,
378            task: tokio::spawn(task.run(start)),
379        }
380    }
381}
382
383/// A supervised connection: a `Stream` of the protocol's events, and of the
384/// errors it skips past or stops on, that survives drops, stalls and
385/// rotations by reconnecting and replaying its membership.
386///
387/// It ends after an error [`Protocol::recover`] stops on, which it yields
388/// first, or when the membership stops being wanted. Dropping it ends the
389/// task, closing the connection politely.
390pub struct Supervised<P: Protocol> {
391    events: Receiver<task::Item<P>>,
392    commands: mpsc::Sender<Command<P>>,
393    /// Whether the task stopped on an error.
394    task: tokio::task::JoinHandle<bool>,
395}
396
397impl<P: Protocol> std::fmt::Debug for Supervised<P> {
398    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
399        f.debug_struct("Supervised")
400            .field("running", &!self.task.is_finished())
401            .finish_non_exhaustive()
402    }
403}
404
405impl<P: Protocol> Supervised<P> {
406    /// A handle for changing the membership while the stream runs.
407    pub fn membership(&self) -> MembershipHandle<P> {
408        MembershipHandle {
409            commands: self.commands.clone(),
410        }
411    }
412
413    /// Close the connection politely and stop the task, without waiting on
414    /// the consumer's queue. Fails with [`Protocol::stopped`] if the task had
415    /// already stopped on an error, which the stream yielded. Every
416    /// [`MembershipHandle`] answers [`Protocol::stopped`] afterwards.
417    pub async fn close(self) -> Result<(), P::Error> {
418        let Self {
419            events,
420            commands,
421            task,
422        } = self;
423        // The task sees the queue's receiver go, wherever it is waiting,
424        // a full queue included.
425        drop(events);
426        drop(commands);
427        match task.await {
428            Ok(false) => Ok(()),
429            Ok(true) | Err(_) => Err(P::stopped()),
430        }
431    }
432}
433
434impl<P: Protocol> Stream for Supervised<P> {
435    type Item = Result<P::Event, P::Error>;
436
437    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
438        self.get_mut().events.poll_recv(cx)
439    }
440}
441
442/// Changes the membership of a running [`Supervised`].
443///
444/// A call waits for the task to take the change and for its answer. The task
445/// takes changes between frames on a live connection, and during an outage's
446/// backoff, but not while the consumer's queue is full: a call made from the
447/// task that reads the stream can then wait for ever. Make changes from
448/// another task, or keep reading while one is pending.
449pub struct MembershipHandle<P: Protocol> {
450    commands: mpsc::Sender<Command<P>>,
451}
452
453impl<P: Protocol> Clone for MembershipHandle<P> {
454    fn clone(&self) -> Self {
455        Self {
456            commands: self.commands.clone(),
457        }
458    }
459}
460
461impl<P: Protocol> std::fmt::Debug for MembershipHandle<P> {
462    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
463        f.debug_struct("MembershipHandle")
464            .field("running", &!self.commands.is_closed())
465            .finish()
466    }
467}
468
469impl<P: Protocol> MembershipHandle<P> {
470    /// Make one change, and wait for its answer: from
471    /// [`Protocol::apply`] on a live connection, from [`Protocol::record`]
472    /// during an outage, [`Protocol::stalled`] for a request left unanswered
473    /// for the staleness window, or [`Protocol::stopped`] once the task has
474    /// stopped.
475    pub async fn apply(&self, change: P::Change) -> Result<P::Ack, P::Error> {
476        let (reply, answer) = oneshot::channel();
477        self.commands
478            .send(Command { change, reply })
479            .await
480            .map_err(|_| P::stopped())?;
481        answer.await.map_err(|_| P::stopped())?
482    }
483}
484
485#[cfg(test)]
486mod tests;