hyper_server/addr_incoming_config.rs
1use std::time::Duration;
2
3/// Configuration settings for the `AddrIncoming`.
4///
5/// This configuration structure is designed to be used in conjunction with the
6/// [`AddrIncoming`](hyper::server::conn::AddrIncoming) type from the Hyper crate.
7/// It provides a mechanism to customize server settings like TCP keepalive probes,
8/// error handling, and other TCP socket-level configurations.
9#[derive(Debug, Clone)]
10pub struct AddrIncomingConfig {
11 pub(crate) tcp_sleep_on_accept_errors: bool,
12 pub(crate) tcp_keepalive: Option<Duration>,
13 pub(crate) tcp_keepalive_interval: Option<Duration>,
14 pub(crate) tcp_keepalive_retries: Option<u32>,
15 pub(crate) tcp_nodelay: bool,
16}
17
18impl Default for AddrIncomingConfig {
19 fn default() -> Self {
20 Self::new()
21 }
22}
23
24impl AddrIncomingConfig {
25 /// Creates a new `AddrIncomingConfig` with default settings.
26 ///
27 /// # Default Settings
28 /// - Sleep on accept errors: `true`
29 /// - TCP keepalive probes: Disabled (`None`)
30 /// - Duration between keepalive retransmissions: None
31 /// - Number of keepalive retransmissions: None
32 /// - `TCP_NODELAY` option: `false`
33 ///
34 /// # Returns
35 ///
36 /// A new `AddrIncomingConfig` instance with default settings.
37 pub fn new() -> AddrIncomingConfig {
38 Self {
39 tcp_sleep_on_accept_errors: true,
40 tcp_keepalive: None,
41 tcp_keepalive_interval: None,
42 tcp_keepalive_retries: None,
43 tcp_nodelay: false,
44 }
45 }
46
47 /// Creates a cloned copy of the current configuration.
48 ///
49 /// This method can be useful when you want to preserve the original settings and
50 /// create a modified configuration based on the current one.
51 ///
52 /// # Returns
53 ///
54 /// A cloned `AddrIncomingConfig`.
55 pub fn build(&mut self) -> Self {
56 self.clone()
57 }
58
59 /// Specifies whether to pause (sleep) when an error occurs while accepting a connection.
60 ///
61 /// This can be useful to prevent rapidly exhausting file descriptors in scenarios
62 /// where errors might be transient or frequent.
63 ///
64 /// # Parameters
65 ///
66 /// - `val`: Whether to sleep on accept errors. Default is `true`.
67 ///
68 /// # Returns
69 ///
70 /// A mutable reference to the current `AddrIncomingConfig`.
71 pub fn tcp_sleep_on_accept_errors(&mut self, val: bool) -> &mut Self {
72 self.tcp_sleep_on_accept_errors = val;
73 self
74 }
75
76 /// Configures the frequency of TCP keepalive probes.
77 ///
78 /// TCP keepalive probes are used to detect whether a peer is still connected.
79 ///
80 /// # Parameters
81 ///
82 /// - `val`: Duration between keepalive probes. Setting to `None` disables keepalive probes. Default is `None`.
83 ///
84 /// # Returns
85 ///
86 /// A mutable reference to the current `AddrIncomingConfig`.
87 pub fn tcp_keepalive(&mut self, val: Option<Duration>) -> &mut Self {
88 self.tcp_keepalive = val;
89 self
90 }
91
92 /// Configures the duration between two successive TCP keepalive retransmissions.
93 ///
94 /// If an acknowledgment to a previous keepalive probe isn't received within this duration,
95 /// a new probe will be sent.
96 ///
97 /// # Parameters
98 ///
99 /// - `val`: Duration between keepalive retransmissions. Default is no interval (`None`).
100 ///
101 /// # Returns
102 ///
103 /// A mutable reference to the current `AddrIncomingConfig`.
104 pub fn tcp_keepalive_interval(&mut self, val: Option<Duration>) -> &mut Self {
105 self.tcp_keepalive_interval = val;
106 self
107 }
108
109 /// Configures the number of times to retransmit a TCP keepalive probe if no acknowledgment is received.
110 ///
111 /// After the specified number of retransmissions, the remote end is considered unavailable.
112 ///
113 /// # Parameters
114 ///
115 /// - `val`: Number of retransmissions before considering the remote end unavailable. Default is no retry (`None`).
116 ///
117 /// # Returns
118 ///
119 /// A mutable reference to the current `AddrIncomingConfig`.
120 pub fn tcp_keepalive_retries(&mut self, val: Option<u32>) -> &mut Self {
121 self.tcp_keepalive_retries = val;
122 self
123 }
124
125 /// Configures the `TCP_NODELAY` option for accepted connections.
126 ///
127 /// When enabled, this option disables Nagle's algorithm, which can reduce latencies for small packets.
128 ///
129 /// # Parameters
130 ///
131 /// - `val`: Whether to enable `TCP_NODELAY`. Default is `false`.
132 ///
133 /// # Returns
134 ///
135 /// A mutable reference to the current `AddrIncomingConfig`.
136 pub fn tcp_nodelay(&mut self, val: bool) -> &mut Self {
137 self.tcp_nodelay = val;
138 self
139 }
140}