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    pub subscriptions: usize,
24    pub users: usize,
25    pub posts: usize,
26}
27
28impl Limits {
29    pub const VENUE: Limits = Limits { subscriptions: MAX_SUBSCRIPTIONS, users: MAX_USERS, posts: MAX_INFLIGHT_POSTS };
30}
31
32impl Default for Limits {
33    fn default() -> Self {
34        Limits::VENUE
35    }
36}
37
38/// Why nothing was sent.
39#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
40pub enum Refused {
41    #[error("{limit} subscriptions is the most this session holds; the venue allows an IP 1000")]
42    Subscriptions { limit: usize },
43    #[error("{limit} users is the most this session follows; the venue allows an IP 10")]
44    Users { limit: usize },
45    #[error("{limit} posts are already waiting for an answer; the venue allows an IP 100")]
46    Posts { limit: usize },
47    /// A post is never held to send later: an action sent late may land after the moment it
48    /// was meant for, and one sent again may land twice.
49    #[error("the socket is not open, and a post is never held to send later")]
50    Closed,
51    #[error(transparent)]
52    Bad(#[from] BadSubscription),
53}
54
55#[cfg(test)]
56#[path = "tests/limits.rs"]
57mod tests;