Skip to main content

df_hyperliquid_core/live/session/
timing.rs

1//! The session's clock: its rules in milliseconds ([`Timing`]), what a tick asks of the socket's
2//! owner, and how long a closed socket waits before it connects again.
3
4use crate::live::session::{Link, Session};
5
6/// The clock's rules, in milliseconds.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub struct Timing {
9    /// Ping a socket no pong or data has arrived on for this long. The venue closes one it has
10    /// sent nothing to for 60 s, and its acks do not count; a ping makes it send a pong.
11    pub ping_after_ms: u64,
12    /// A socket nothing has arrived on for this long, pong included, is dead. The venue would
13    /// have closed it by now; when no close came, the connection is half open.
14    pub silent_ms: u64,
15    /// A socket open this long that heard something worked: the next failure starts the
16    /// reconnect delays over. One dropped sooner keeps backing off.
17    pub steady_ms: u64,
18    /// The first reconnect delay; each failure after doubles it.
19    pub first_retry_ms: u64,
20    /// The longest reconnect delay, before jitter adds up to a quarter.
21    pub last_retry_ms: u64,
22}
23
24impl Timing {
25    /// A 30 s ping, dead after 60 s of nothing (the venue's own idle limit), and reconnects
26    /// 250 ms doubling to 20 s.
27    pub const DEFAULT: Timing = Timing {
28        ping_after_ms: 30_000,
29        silent_ms: 60_000,
30        steady_ms: 30_000,
31        first_retry_ms: 250,
32        last_retry_ms: 20_000,
33    };
34}
35
36impl Default for Timing {
37    fn default() -> Self {
38        Timing::DEFAULT
39    }
40}
41
42/// What a tick asks of the socket's owner.
43#[derive(Debug, Clone, Copy, PartialEq, Eq)]
44pub enum Tick {
45    /// Nothing to do yet.
46    Wait,
47    /// Send [`crate::live::request::PING`].
48    Ping,
49    /// Nothing has arrived for too long: close the socket, call [`Session::closed`], and
50    /// connect again after the delay it gives.
51    Reconnect,
52}
53
54/// What a closed socket leaves behind.
55#[derive(Debug, Clone, PartialEq, Eq)]
56pub struct Retry {
57    /// Wait this long, then connect again.
58    pub after_ms: u64,
59    /// Posts sent and never answered. Unknown, not failed: an action among them may have
60    /// landed, so none is sent again; read the account to find out.
61    pub unanswered: Vec<u64>,
62}
63
64impl Session {
65    /// What the socket needs at `now`: a ping after [`Timing::ping_after_ms`] with no pong or
66    /// data (once, until one arrives or as long again passes), or a reconnect after
67    /// [`Timing::silent_ms`] of nothing at all.
68    pub fn tick(&mut self, now: u64) -> Tick {
69        let Link::Open { heard, kept, pinged, .. } = &mut self.link else { return Tick::Wait };
70        if now.saturating_sub(*heard) >= self.timing.silent_ms {
71            return Tick::Reconnect;
72        }
73        if now.saturating_sub((*kept).max(*pinged)) >= self.timing.ping_after_ms {
74            *pinged = now;
75            return Tick::Ping;
76        }
77        Tick::Wait
78    }
79
80    /// When a tick next has something to do; `None` while closed.
81    #[must_use]
82    pub fn due(&self) -> Option<u64> {
83        let Link::Open { heard, kept, pinged, .. } = self.link else { return None };
84        let ping = kept.max(pinged).saturating_add(self.timing.ping_after_ms);
85        let silent = heard.saturating_add(self.timing.silent_ms);
86        Some(ping.min(silent))
87    }
88
89    /// The socket closed at `now`, or a connect failed. Gives how long to wait before the next
90    /// connect, and the posts left unanswered. The wait doubles from
91    /// [`Timing::first_retry_ms`] to [`Timing::last_retry_ms`] and `random` adds up to a quarter
92    /// more, so many clients dropped at once do not return at once. It starts over after a
93    /// socket that held ([`Timing::steady_ms`]). There is no last attempt.
94    pub fn closed(&mut self, now: u64, random: u64) -> Retry {
95        if let Link::Open { since, talked: true, .. } = self.link
96            && now.saturating_sub(since) >= self.timing.steady_ms
97        {
98            self.failures = 0;
99        }
100        self.link = Link::Closed;
101        let doubled = 1u64.checked_shl(self.failures).unwrap_or(u64::MAX);
102        let step = self.timing.first_retry_ms.saturating_mul(doubled).min(self.timing.last_retry_ms);
103        let jitter = random % (step / 4).saturating_add(1);
104        self.failures = self.failures.saturating_add(1);
105        Retry { after_ms: step.saturating_add(jitter), unanswered: std::mem::take(&mut self.waiting) }
106    }
107}
108
109#[cfg(test)]
110#[path = "tests/timing.rs"]
111mod tests;