@@ -12,13 +12,30 @@ use crate::{
12
12
Server ,
13
13
} ;
14
14
15
+ /// Multipath TCP (MPTCP) preference.
16
+ ///
17
+ /// Also see [`ServerBuilder::mptcp()`].
18
+ #[ derive( Debug , Clone ) ]
19
+ pub enum MpTcp {
20
+ /// MPTCP will not be used when binding sockets.
21
+ Disabled ,
22
+
23
+ /// MPTCP will be attempted when binding sockets. If errors occur, regular TCP will be
24
+ /// attempted, too.
25
+ TcpFallback ,
26
+
27
+ /// MPTCP will be used when binding sockets (with no fallback).
28
+ NoFallback ,
29
+ }
30
+
15
31
/// [Server] builder.
16
32
pub struct ServerBuilder {
17
33
pub ( crate ) threads : usize ,
18
34
pub ( crate ) token : usize ,
19
35
pub ( crate ) backlog : u32 ,
20
36
pub ( crate ) factories : Vec < Box < dyn InternalServiceFactory > > ,
21
37
pub ( crate ) sockets : Vec < ( usize , String , MioListener ) > ,
38
+ pub ( crate ) mptcp : MpTcp ,
22
39
pub ( crate ) exit : bool ,
23
40
pub ( crate ) listen_os_signals : bool ,
24
41
pub ( crate ) cmd_tx : UnboundedSender < ServerCommand > ,
@@ -43,6 +60,7 @@ impl ServerBuilder {
43
60
factories : Vec :: new ( ) ,
44
61
sockets : Vec :: new ( ) ,
45
62
backlog : 2048 ,
63
+ mptcp : MpTcp :: Disabled ,
46
64
exit : false ,
47
65
listen_os_signals : true ,
48
66
cmd_tx,
@@ -96,6 +114,24 @@ impl ServerBuilder {
96
114
self
97
115
}
98
116
117
+ /// Sets MultiPath TCP (MPTCP) preference on bound sockets.
118
+ ///
119
+ /// Multipath TCP (MPTCP) builds on top of TCP to improve connection redundancy and performance
120
+ /// by sharing a network data stream across multiple underlying TCP sessions. See [mptcp.dev]
121
+ /// for more info about MPTCP itself.
122
+ ///
123
+ /// MPTCP is available on Linux kernel version 5.6 and higher. In addition, you'll also need to
124
+ /// ensure the kernel option is enabled using `sysctl net.mptcp.enabled=1`.
125
+ ///
126
+ /// This method will have no effect if called after a `bind()`.
127
+ ///
128
+ /// [mptcp.dev]: https://www.mptcp.dev
129
+ #[ cfg( target_os = "linux" ) ]
130
+ pub fn mptcp ( mut self , mptcp_enabled : MpTcp ) -> Self {
131
+ self . mptcp = mptcp_enabled;
132
+ self
133
+ }
134
+
99
135
/// Sets the maximum per-worker number of concurrent connections.
100
136
///
101
137
/// All socket listeners will stop accepting connections when this limit is reached for
@@ -144,7 +180,7 @@ impl ServerBuilder {
144
180
U : ToSocketAddrs ,
145
181
N : AsRef < str > ,
146
182
{
147
- let sockets = bind_addr ( addr, self . backlog ) ?;
183
+ let sockets = bind_addr ( addr, self . backlog , & self . mptcp ) ?;
148
184
149
185
trace ! ( "binding server to: {:?}" , & sockets) ;
150
186
@@ -260,13 +296,14 @@ impl ServerBuilder {
260
296
pub ( super ) fn bind_addr < S : ToSocketAddrs > (
261
297
addr : S ,
262
298
backlog : u32 ,
299
+ mptcp : & MpTcp ,
263
300
) -> io:: Result < Vec < MioTcpListener > > {
264
301
let mut opt_err = None ;
265
302
let mut success = false ;
266
303
let mut sockets = Vec :: new ( ) ;
267
304
268
305
for addr in addr. to_socket_addrs ( ) ? {
269
- match create_mio_tcp_listener ( addr, backlog) {
306
+ match create_mio_tcp_listener ( addr, backlog, mptcp ) {
270
307
Ok ( lst) => {
271
308
success = true ;
272
309
sockets. push ( lst) ;
0 commit comments