Skip to main content

mill_net/tcp/
config.rs

1use std::net::SocketAddr;
2
3/// Configuration for TCP server.
4///
5/// Controls server behavior including bind address, buffer sizes, connection limits,
6/// and socket options. Use TcpServerConfig::builder() for ergonomic construction.
7///
8/// ## Socket Options
9///
10/// - no_delay: When enabled (default), disables Nagle's algorithm for lower latency
11/// - keep_alive: Configures SO_KEEPALIVE to detect dead connections
12///
13/// ## Resource Limits
14///
15/// - buffer_size: Size of read buffers allocated from the pool
16/// - max_connections: Hard limit on concurrent connections (None for unlimited)
17#[derive(Clone)]
18pub struct TcpServerConfig {
19    /// Address to bind to
20    pub address: SocketAddr,
21    /// Size of connection buffer
22    pub buffer_size: usize,
23    /// Maximum number of connections
24    pub max_connections: Option<usize>,
25    /// Enable TCP_NODELAY
26    pub no_delay: bool,
27    /// SO_KEEPALIVE setting
28    pub keep_alive: Option<std::time::Duration>,
29}
30
31impl TcpServerConfig {
32    /// Create a new builder for TcpServerConfig
33    pub fn builder() -> TcpServerConfigBuilder {
34        TcpServerConfigBuilder::new()
35    }
36}
37
38impl Default for TcpServerConfig {
39    fn default() -> Self {
40        Self {
41            address: "127.0.0.1:8080".parse().unwrap(),
42            buffer_size: 8192,
43            max_connections: None,
44            no_delay: true,
45            keep_alive: Some(std::time::Duration::from_secs(60)),
46        }
47    }
48}
49
50/// Builder for TcpServerConfig using the builder pattern.
51///
52/// All fields are optional and will use defaults from TcpServerConfig::default()
53/// if not explicitly set.
54pub struct TcpServerConfigBuilder {
55    address: Option<SocketAddr>,
56    buffer_size: Option<usize>,
57    max_connections: Option<usize>,
58    no_delay: Option<bool>,
59    keep_alive: Option<Option<std::time::Duration>>,
60}
61
62impl TcpServerConfigBuilder {
63    /// Create a new builder with default values
64    pub fn new() -> Self {
65        Self {
66            address: None,
67            buffer_size: None,
68            max_connections: None,
69            no_delay: None,
70            keep_alive: None,
71        }
72    }
73
74    /// Set the address to bind to
75    pub fn address(mut self, address: SocketAddr) -> Self {
76        self.address = Some(address);
77        self
78    }
79
80    /// Set the buffer size for connections
81    pub fn buffer_size(mut self, size: usize) -> Self {
82        self.buffer_size = Some(size);
83        self
84    }
85
86    /// Set the maximum number of connections
87    pub fn max_connections(mut self, max: usize) -> Self {
88        self.max_connections = Some(max);
89        self
90    }
91
92    /// Enable or disable TCP_NODELAY
93    pub fn no_delay(mut self, enabled: bool) -> Self {
94        self.no_delay = Some(enabled);
95        self
96    }
97
98    /// Set SO_KEEPALIVE duration
99    pub fn keep_alive(mut self, duration: Option<std::time::Duration>) -> Self {
100        self.keep_alive = Some(duration);
101        self
102    }
103
104    /// Build the TcpServerConfig
105    pub fn build(self) -> TcpServerConfig {
106        let default = TcpServerConfig::default();
107        TcpServerConfig {
108            address: self.address.unwrap_or(default.address),
109            buffer_size: self.buffer_size.unwrap_or(default.buffer_size),
110            max_connections: self.max_connections.or(default.max_connections),
111            no_delay: self.no_delay.unwrap_or(default.no_delay),
112            keep_alive: self.keep_alive.unwrap_or(default.keep_alive),
113        }
114    }
115}
116
117impl Default for TcpServerConfigBuilder {
118    fn default() -> Self {
119        let default = TcpServerConfig::default();
120        Self {
121            address: Some(default.address),
122            buffer_size: Some(default.buffer_size),
123            max_connections: default.max_connections,
124            no_delay: Some(default.no_delay),
125            keep_alive: Some(default.keep_alive),
126        }
127    }
128}