Skip to main content

df_hyperliquid_core/live/session/
limits.rs

1//! The venue's per-IP limits, the part of them one session holds ([`Limits`]), and why a session
2//! sends nothing ([`Refused`]).
3
4use crate::live::BadSubscription;
5
6/// Websocket connections an IP may hold.
7pub const MAX_CONNECTIONS: usize = 10;
8/// New websocket connections an IP may open a minute.
9pub const MAX_NEW_CONNECTIONS_PER_MINUTE: usize = 30;
10/// Subscriptions an IP may hold across its connections.
11pub const MAX_SUBSCRIPTIONS: usize = 1_000;
12/// Distinct users an IP's user subscriptions may name.
13pub const MAX_USERS: usize = 10;
14/// Messages an IP may send a minute across its connections.
15pub const MAX_MESSAGES_PER_MINUTE: usize = 2_000;
16/// Posts an IP may have waiting for an answer across its connections.
17pub const MAX_INFLIGHT_POSTS: usize = 100;
18
19/// What one session may hold. [`Limits::VENUE`] is the whole of an IP's allowance; a session
20/// sharing the IP with others takes a part of it.
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub struct Limits {
23    /// Subscriptions the session may hold.
24    pub subscriptions: usize,
25    /// Distinct users the session's subscriptions may name.
26    pub users: usize,
27    /// Posts the session may have waiting for an answer.
28    pub posts: usize,
29}
30
31impl Limits {
32    /// An IP's whole allowance: 1000 subscriptions, 10 users, 100 posts in flight.
33    pub const VENUE: Limits = Limits { subscriptions: MAX_SUBSCRIPTIONS, users: MAX_USERS, posts: MAX_INFLIGHT_POSTS };
34}
35
36impl Default for Limits {
37    fn default() -> Self {
38        Limits::VENUE
39    }
40}
41
42/// Why nothing was sent.
43#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
44pub enum Refused {
45    /// The session already holds as many subscriptions as its limits allow.
46    #[error("{limit} subscriptions is the most this session holds; the venue allows an IP 1000")]
47    Subscriptions {
48        /// The session's limit, [`Limits::subscriptions`].
49        limit: usize,
50    },
51    /// The subscription would name one user more than the session's limits allow.
52    #[error("{limit} users is the most this session follows; the venue allows an IP 10")]
53    Users {
54        /// The session's limit, [`Limits::users`].
55        limit: usize,
56    },
57    /// As many posts as the session's limits allow are waiting for an answer.
58    #[error("{limit} posts are already waiting for an answer; the venue allows an IP 100")]
59    Posts {
60        /// The session's limit, [`Limits::posts`].
61        limit: usize,
62    },
63    /// A post is never held to send later: an action sent late may land after the moment it
64    /// was meant for, and one sent again may land twice.
65    #[error("the socket is not open, and a post is never held to send later")]
66    Closed,
67    /// The subscription is not one the venue would take.
68    #[error(transparent)]
69    Bad(#[from] BadSubscription),
70}
71
72#[cfg(test)]
73#[path = "tests/limits.rs"]
74mod tests;