@@ -94,6 +94,12 @@ type ChannelOptions struct {
9494 // This is an unstable API - breaking changes are likely.
9595 RelayTimerVerification bool
9696
97+ // EnableMPTCP enables MPTCP for TCP network connection to increase reliability.
98+ // It requires underlying operating system support MPTCP.
99+ // If EnableMPTCP is false or no MPTCP support, the connection will use normal TCP.
100+ // It's set to false by default.
101+ EnableMPTCP bool
102+
97103 // The reporter to use for reporting stats for this channel.
98104 StatsReporter StatsReporter
99105
@@ -184,6 +190,7 @@ type Channel struct {
184190 relayMaxConnTimeout time.Duration
185191 relayMaxTombs uint64
186192 relayTimerVerify bool
193+ enableMPTCP bool
187194 internalHandlers * handlerMap
188195 handler Handler
189196 onPeerStatusChanged func (* Peer )
@@ -275,8 +282,12 @@ func NewChannel(serviceName string, opts *ChannelOptions) (*Channel, error) {
275282 return nil , err
276283 }
277284
278- // Default to dialContext if dialer is not passed in as an option
285+ // Default to dialContext or dialMPTCPContex
286+ // if dialer is not passed in as an option
279287 dialCtx := dialContext
288+ if opts .EnableMPTCP {
289+ dialCtx = dialMPTCPContext
290+ }
280291 if opts .Dialer != nil {
281292 dialCtx = func (ctx context.Context , hostPort string ) (net.Conn , error ) {
282293 return opts .Dialer (ctx , "tcp" , hostPort )
@@ -306,6 +317,7 @@ func NewChannel(serviceName string, opts *ChannelOptions) (*Channel, error) {
306317 relayMaxConnTimeout : opts .RelayMaxConnectionTimeout ,
307318 relayMaxTombs : opts .RelayMaxTombs ,
308319 relayTimerVerify : opts .RelayTimerVerification ,
320+ enableMPTCP : opts .EnableMPTCP ,
309321 dialer : dialCtx ,
310322 connContext : opts .ConnContext ,
311323 closed : make (chan struct {}),
@@ -402,7 +414,15 @@ func (ch *Channel) ListenAndServe(hostPort string) error {
402414 return errAlreadyListening
403415 }
404416
405- l , err := net .Listen ("tcp" , hostPort )
417+ var l net.Listener
418+ var err error
419+ if ch .enableMPTCP {
420+ lc := & net.ListenConfig {}
421+ lc .SetMultipathTCP (true )
422+ l , err = lc .Listen (context .Background (), "tcp" , hostPort )
423+ } else {
424+ l , err = net .Listen ("tcp" , hostPort )
425+ }
406426 if err != nil {
407427 mutable .RUnlock ()
408428 return err
0 commit comments