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)]
44#[non_exhaustive]
45pub enum Tick {
46 /// Nothing to do yet.
47 Wait,
48 /// Send [`crate::live::request::PING`].
49 Ping,
50 /// Nothing has arrived for too long: close the socket, call [`Session::closed`], and
51 /// connect again after the delay it gives.
52 Reconnect,
53}
54
55/// What a closed socket leaves behind.
56#[derive(Debug, Clone, PartialEq, Eq)]
57#[non_exhaustive]
58pub struct Retry {
59 /// Wait this long, then connect again.
60 pub after_ms: u64,
61 /// Posts sent and never answered. Unknown, not failed: an action among them may have
62 /// landed, so none is sent again; read the account to find out.
63 pub unanswered: Vec<u64>,
64}
65
66impl Session {
67 /// What the socket needs at `now`: a ping after [`Timing::ping_after_ms`] with no pong or
68 /// data (once, until one arrives or as long again passes), or a reconnect after
69 /// [`Timing::silent_ms`] of nothing at all.
70 pub fn tick(&mut self, now: u64) -> Tick {
71 let Link::Open { heard, kept, pinged, .. } = &mut self.link else { return Tick::Wait };
72 if now.saturating_sub(*heard) >= self.timing.silent_ms {
73 return Tick::Reconnect;
74 }
75 if now.saturating_sub((*kept).max(*pinged)) >= self.timing.ping_after_ms {
76 *pinged = now;
77 return Tick::Ping;
78 }
79 Tick::Wait
80 }
81
82 /// When a tick next has something to do; `None` while closed.
83 #[must_use]
84 pub fn due(&self) -> Option<u64> {
85 let Link::Open { heard, kept, pinged, .. } = self.link else { return None };
86 let ping = kept.max(pinged).saturating_add(self.timing.ping_after_ms);
87 let silent = heard.saturating_add(self.timing.silent_ms);
88 Some(ping.min(silent))
89 }
90
91 /// The socket closed at `now`, or a connect failed. Gives how long to wait before the next
92 /// connect, and the posts left unanswered. The wait doubles from
93 /// [`Timing::first_retry_ms`] to [`Timing::last_retry_ms`] and `random` adds up to a quarter
94 /// more, so many clients dropped at once do not return at once. It starts over after a
95 /// socket that held ([`Timing::steady_ms`]). There is no last attempt.
96 pub fn closed(&mut self, now: u64, random: u64) -> Retry {
97 if let Link::Open { since, talked: true, .. } = self.link
98 && now.saturating_sub(since) >= self.timing.steady_ms
99 {
100 self.failures = 0;
101 }
102 self.link = Link::Closed;
103 let doubled = 1u64.checked_shl(self.failures).unwrap_or(u64::MAX);
104 let step = self.timing.first_retry_ms.saturating_mul(doubled).min(self.timing.last_retry_ms);
105 let jitter = random % (step / 4).saturating_add(1);
106 self.failures = self.failures.saturating_add(1);
107 Retry { after_ms: step.saturating_add(jitter), unanswered: std::mem::take(&mut self.waiting) }
108 }
109}
110
111#[cfg(test)]
112#[path = "tests/timing.rs"]
113mod tests;