1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//! Socket options for wireframe clients.
use std::{io, time::Duration};
use socket2::{SockRef, TcpKeepalive};
use tokio::net::TcpSocket;
/// Socket options applied before connecting a client.
///
/// # Examples
///
/// ```
/// use std::time::Duration;
///
/// use wireframe::client::SocketOptions;
///
/// let options = SocketOptions::default()
/// .nodelay(true)
/// .keepalive(Some(Duration::from_secs(30)));
/// let expected = SocketOptions::default()
/// .nodelay(true)
/// .keepalive(Some(Duration::from_secs(30)));
/// assert_eq!(options, expected);
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct SocketOptions {
/// Whether small writes bypass Nagle coalescing.
nodelay: Option<bool>,
/// Keepalive policy; `None` means leave the platform default untouched.
keepalive: Option<KeepAliveSetting>,
/// Requested kernel send-buffer capacity.
send_buffer_size: Option<u32>,
/// Requested kernel receive-buffer capacity.
recv_buffer_size: Option<u32>,
/// Whether the address may be rebound while it is in use.
reuseaddr: Option<bool>,
#[cfg(all(
unix,
not(target_os = "solaris"),
not(target_os = "illumos"),
not(target_os = "cygwin"),
))]
/// Whether supported platforms may share a listening port.
reuseport: Option<bool>,
}
/// Internal representation of the optional TCP keepalive override.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum KeepAliveSetting {
/// Explicitly disable keepalive probes.
Disabled,
/// Enable probes after the supplied idle duration.
Duration(Duration),
}
impl KeepAliveSetting {
/// Convert the policy to the socket API's optional duration form.
const fn to_option(self) -> Option<Duration> {
match self {
Self::Disabled => None,
Self::Duration(value) => Some(value),
}
}
}
impl SocketOptions {
/// Configure `TCP_NODELAY` behaviour on the socket.
///
/// # Examples
///
/// ```
/// use wireframe::client::SocketOptions;
///
/// let options = SocketOptions::default().nodelay(true);
/// let expected = SocketOptions::default().nodelay(true);
/// assert_eq!(options, expected);
/// ```
#[must_use]
pub const fn nodelay(mut self, enabled: bool) -> Self {
self.nodelay = Some(enabled);
self
}
/// Configure `SO_KEEPALIVE` behaviour on the socket.
///
/// # Examples
///
/// ```
/// use std::time::Duration;
///
/// use wireframe::client::SocketOptions;
///
/// let options = SocketOptions::default().keepalive(Some(Duration::from_secs(30)));
/// let expected = SocketOptions::default().keepalive(Some(Duration::from_secs(30)));
/// assert_eq!(options, expected);
/// ```
#[must_use]
pub const fn keepalive(mut self, duration: Option<Duration>) -> Self {
let Some(value) = duration else {
self.keepalive = Some(KeepAliveSetting::Disabled);
return self;
};
self.keepalive = Some(KeepAliveSetting::Duration(value));
self
}
/// Configure the socket send buffer size.
///
/// # Examples
///
/// ```
/// use wireframe::client::SocketOptions;
///
/// let options = SocketOptions::default().send_buffer_size(4096);
/// let expected = SocketOptions::default().send_buffer_size(4096);
/// assert_eq!(options, expected);
/// ```
#[must_use]
pub const fn send_buffer_size(mut self, size: u32) -> Self {
self.send_buffer_size = Some(size);
self
}
/// Configure the socket receive buffer size.
///
/// # Examples
///
/// ```
/// use wireframe::client::SocketOptions;
///
/// let options = SocketOptions::default().recv_buffer_size(4096);
/// let expected = SocketOptions::default().recv_buffer_size(4096);
/// assert_eq!(options, expected);
/// ```
#[must_use]
pub const fn recv_buffer_size(mut self, size: u32) -> Self {
self.recv_buffer_size = Some(size);
self
}
/// Configure `SO_REUSEADDR` behaviour on the socket.
///
/// # Examples
///
/// ```
/// use wireframe::client::SocketOptions;
///
/// let options = SocketOptions::default().reuseaddr(true);
/// let expected = SocketOptions::default().reuseaddr(true);
/// assert_eq!(options, expected);
/// ```
#[must_use]
pub const fn reuseaddr(mut self, enabled: bool) -> Self {
self.reuseaddr = Some(enabled);
self
}
/// Configure `SO_REUSEPORT` behaviour on supported platforms.
///
/// # Examples
///
/// ```
/// use wireframe::client::SocketOptions;
///
/// let options = SocketOptions::default().reuseport(true);
/// let expected = SocketOptions::default().reuseport(true);
/// assert_eq!(options, expected);
/// ```
#[cfg(all(
unix,
not(target_os = "solaris"),
not(target_os = "illumos"),
not(target_os = "cygwin"),
))]
#[must_use]
pub const fn reuseport(mut self, enabled: bool) -> Self {
self.reuseport = Some(enabled);
self
}
/// Apply every configured option, preserving the first OS error.
pub(crate) fn apply(&self, socket: &TcpSocket) -> io::Result<()> {
self.apply_nodelay(socket)?;
self.apply_keepalive(socket)?;
self.apply_send_buffer_size(socket)?;
self.apply_recv_buffer_size(socket)?;
self.apply_reuseaddr(socket)?;
self.apply_reuseport(socket)?;
Ok(())
}
/// Apply `TCP_NODELAY` when the caller supplied an override.
fn apply_nodelay(&self, socket: &TcpSocket) -> io::Result<()> {
if let Some(enabled) = self.nodelay {
socket.set_nodelay(enabled)?;
}
Ok(())
}
/// Apply keepalive enablement and its platform-specific idle interval.
fn apply_keepalive(&self, socket: &TcpSocket) -> io::Result<()> {
if let Some(keepalive) = self.keepalive {
match keepalive.to_option() {
Some(duration) => {
socket.set_keepalive(true)?;
let sock_ref = SockRef::from(socket);
let config = TcpKeepalive::new().with_time(duration);
sock_ref.set_tcp_keepalive(&config)?;
}
None => {
socket.set_keepalive(false)?;
}
}
}
Ok(())
}
/// Apply the requested kernel send-buffer capacity.
fn apply_send_buffer_size(&self, socket: &TcpSocket) -> io::Result<()> {
if let Some(size) = self.send_buffer_size {
socket.set_send_buffer_size(size)?;
}
Ok(())
}
/// Apply the requested kernel receive-buffer capacity.
fn apply_recv_buffer_size(&self, socket: &TcpSocket) -> io::Result<()> {
if let Some(size) = self.recv_buffer_size {
socket.set_recv_buffer_size(size)?;
}
Ok(())
}
/// Apply address reuse before the connect attempt.
fn apply_reuseaddr(&self, socket: &TcpSocket) -> io::Result<()> {
if let Some(enabled) = self.reuseaddr {
socket.set_reuseaddr(enabled)?;
}
Ok(())
}
/// Apply port sharing on platforms that expose this option.
#[cfg(all(
unix,
not(target_os = "solaris"),
not(target_os = "illumos"),
not(target_os = "cygwin"),
))]
fn apply_reuseport(&self, socket: &TcpSocket) -> io::Result<()> {
if let Some(enabled) = self.reuseport {
socket.set_reuseport(enabled)?;
}
Ok(())
}
/// Keep the option pipeline portable where port sharing is unavailable.
#[cfg(not(all(
unix,
not(target_os = "solaris"),
not(target_os = "illumos"),
not(target_os = "cygwin"),
)))]
fn apply_reuseport(&self, _socket: &TcpSocket) -> io::Result<()> { Ok(()) }
}