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)]
44#[non_exhaustive]
45pub enum Refused {
46    /// The session already holds as many subscriptions as its limits allow.
47    #[error("{limit} subscriptions is the most this session holds; the venue allows an IP 1000")]
48    Subscriptions {
49        /// The session's limit, [`Limits::subscriptions`].
50        limit: usize,
51    },
52    /// The subscription would name one user more than the session's limits allow.
53    #[error("{limit} users is the most this session follows; the venue allows an IP 10")]
54    Users {
55        /// The session's limit, [`Limits::users`].
56        limit: usize,
57    },
58    /// As many posts as the session's limits allow are waiting for an answer.
59    #[error("{limit} posts are already waiting for an answer; the venue allows an IP 100")]
60    Posts {
61        /// The session's limit, [`Limits::posts`].
62        limit: usize,
63    },
64    /// A post is never held to send later: an action sent late may land after the moment it
65    /// was meant for, and one sent again may land twice.
66    #[error("the socket is not open, and a post is never held to send later")]
67    Closed,
68    /// The subscription is not one the venue would take.
69    #[error(transparent)]
70    Bad(#[from] BadSubscription),
71}
72
73#[cfg(test)]
74#[path = "tests/limits.rs"]
75mod tests;