Skip to main content

helius_laserstream/
config.rs

1use serde::{Deserialize, Serialize};
2
3/// Compression encoding options
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
5#[serde(rename_all = "lowercase")]
6pub enum CompressionEncoding {
7    /// Gzip compression
8    Gzip,
9    /// Zstd compression
10    Zstd,
11}
12
13#[derive(Debug, Clone)]
14pub struct LaserstreamConfig {
15    /// API Key for authentication.
16    pub api_key: String,
17    /// The Laserstream endpoint URL.
18    pub endpoint: String,
19    /// Maximum number of reconnection attempts. Defaults to 10.
20    /// A hard cap of 240 attempts (20 minutes / 5 seconds) is enforced internally.
21    pub max_reconnect_attempts: Option<u32>,
22    /// gRPC channel options
23    pub channel_options: ChannelOptions,
24    /// When true, enable replay on reconnects (uses from_slot and internal slot tracking).
25    /// When false, no replay - start from current slot on reconnects.
26    /// Default: true
27    pub replay: bool,
28}
29
30#[derive(Debug, Clone, Default)]
31pub struct ChannelOptions {
32    /// Connect timeout in seconds. Default: 10
33    pub connect_timeout_secs: Option<u64>,
34    /// Request timeout in seconds. Default: 30
35    pub timeout_secs: Option<u64>,
36    /// Max message size for receiving in bytes. Default: 1GB
37    pub max_decoding_message_size: Option<usize>,
38    /// Max message size for sending in bytes. Default: 32MB
39    pub max_encoding_message_size: Option<usize>,
40    /// HTTP/2 keep-alive interval in seconds. Default: 30
41    pub http2_keep_alive_interval_secs: Option<u64>,
42    /// Keep-alive timeout in seconds. Default: 5
43    pub keep_alive_timeout_secs: Option<u64>,
44    /// Enable keep-alive while idle. Default: true
45    pub keep_alive_while_idle: Option<bool>,
46    /// Initial stream window size in bytes. Default: 4MB
47    pub initial_stream_window_size: Option<u32>,
48    /// Initial connection window size in bytes. Default: 8MB
49    pub initial_connection_window_size: Option<u32>,
50    /// Enable HTTP/2 adaptive window. Default: true
51    pub http2_adaptive_window: Option<bool>,
52    /// Enable TCP no-delay. Default: true
53    pub tcp_nodelay: Option<bool>,
54    /// TCP keep-alive interval in seconds. Default: 60
55    pub tcp_keepalive_secs: Option<u64>,
56    /// Buffer size in bytes. Default: 64KB
57    pub buffer_size: Option<usize>,
58    /// Compression encodings to accept from server. Default: ["gzip", "zstd"]
59    pub accept_compression: Option<Vec<CompressionEncoding>>,
60    /// Compression encoding to use when sending. Default: None
61    pub send_compression: Option<CompressionEncoding>,
62}
63
64
65impl ChannelOptions {
66    /// Enable zstd compression for both sending and receiving
67    pub fn with_zstd_compression(mut self) -> Self {
68        self.send_compression = Some(CompressionEncoding::Zstd);
69        self.accept_compression = Some(vec![CompressionEncoding::Zstd, CompressionEncoding::Gzip]);
70        self
71    }
72    
73    /// Enable gzip compression for both sending and receiving
74    pub fn with_gzip_compression(mut self) -> Self {
75        self.send_compression = Some(CompressionEncoding::Gzip);
76        self.accept_compression = Some(vec![CompressionEncoding::Gzip, CompressionEncoding::Zstd]);
77        self
78    }
79}
80
81impl Default for LaserstreamConfig {
82    fn default() -> Self {
83        Self {
84            api_key: String::new(),
85            endpoint: String::new(),
86            max_reconnect_attempts: None, // Default to None
87            channel_options: ChannelOptions::default(),
88            replay: true, // Default to true
89        }
90    }
91}
92
93impl LaserstreamConfig {
94    pub fn new(endpoint: String, api_key: String) -> Self {
95        Self {
96            endpoint,
97            api_key,
98            max_reconnect_attempts: None, // Default to None
99            channel_options: ChannelOptions::default(),
100            replay: true, // Default to true
101        }
102    }
103
104    /// Sets the maximum number of reconnection attempts.
105    pub fn with_max_reconnect_attempts(mut self, attempts: u32) -> Self {
106        self.max_reconnect_attempts = Some(attempts);
107        self
108    }
109
110    /// Sets custom channel options.
111    pub fn with_channel_options(mut self, options: ChannelOptions) -> Self {
112        self.channel_options = options;
113        self
114    }
115
116    /// Sets replay behavior on reconnects.
117    /// When true (default), uses from_slot and internal slot tracking for replay.
118    /// When false, starts from current slot on reconnects (no replay).
119    pub fn with_replay(mut self, replay: bool) -> Self {
120        self.replay = replay;
121        self
122    }
123}