Skip to main content

cloudpub_common/
constants.rs

1use backoff::ExponentialBackoff;
2use std::time::Duration;
3
4/// Application-layer heartbeat interval in secs
5pub const DEFAULT_HEARTBEAT_INTERVAL_SECS: u64 = 30;
6pub const DEFAULT_HEARTBEAT_TIMEOUT_SECS: u64 = 40;
7
8pub const DATA_BUFFER_SIZE: usize = 16384; // 16 KiB, a reasonable size for data buffers
9pub const DATA_CHANNEL_SIZE: usize = 1024; // Packets in data channel
10pub const CONTROL_CHANNEL_SIZE: usize = 1024; // Packets in control channel
11
12/// Client
13pub const DEFAULT_CLIENT_RETRY_INTERVAL_SECS: u64 = 60;
14pub const DEFAULT_CLIENT_DATA_CHANNEL_CAPACITY: u32 = DATA_BUFFER_SIZE as u32 * 4;
15
16/// Server
17pub const BACKLOG_SIZE: usize = 1024; // The capacity TCP incoming conn backlog
18pub const HANDSHAKE_TIMEOUT: u64 = 5; // Timeout for transport handshake
19
20/// TCP
21pub const DEFAULT_NODELAY: bool = true;
22pub const DEFAULT_KEEPALIVE_SECS: u64 = 20;
23pub const DEFAULT_KEEPALIVE_INTERVAL: u64 = 8;
24pub const MESSAGE_TIMEOUT_SECS: u64 = 60;
25
26/// Deadline for establishing a connection: TCP connect, TLS handshake and the
27/// WebSocket upgrade. None of those steps carry a protocol-level message, so
28/// `MESSAGE_TIMEOUT_SECS` does not cover them - without this a peer that
29/// accepts the connection and then goes silent parks the dial forever.
30pub const DEFAULT_CONNECT_TIMEOUT_SECS: u64 = 30;
31
32/// WebSocket read buffer chunk size (tungstenite `read_buffer_size`).
33///
34/// tungstenite's `FrameCodec::read_in` zero-fills its input buffer up to
35/// `len + read_buffer_size` on every read cycle (see frame/mod.rs `resize(.., 0)`).
36/// The 128 KiB default means each read memset-zeroes 128 KiB regardless of the
37/// actual payload - profiling the server showed this zero-fill at ~8% of the
38/// endpoint thread's CPU. 16 KiB caps the wasted zeroing while staying a
39/// reasonable per-syscall read chunk for bulk tunnel traffic.
40pub const WEBSOCKET_READ_BUFFER_SIZE: usize = 16 * 1024;
41
42// FIXME: Determine reasonable size
43/// UDP MTU. Currently far larger than necessary
44pub const UDP_BUFFER_SIZE: usize = 2048;
45pub const UDP_SENDQ_SIZE: usize = 1024;
46pub const UDP_TIMEOUT: u64 = 60;
47
48// Pingora service param
49pub const LISTENERS_PER_FD: usize = 1;
50
51pub fn listen_backoff() -> ExponentialBackoff {
52    ExponentialBackoff {
53        max_elapsed_time: None,
54        max_interval: Duration::from_secs(1),
55        ..Default::default()
56    }
57}
58
59pub fn run_control_chan_backoff(interval: u64) -> ExponentialBackoff {
60    ExponentialBackoff {
61        randomization_factor: 0.2,
62        max_elapsed_time: None,
63        multiplier: 3.0,
64        max_interval: Duration::from_secs(interval),
65        ..Default::default()
66    }
67}