Skip to content

Commit 6b6241f

Browse files
committed
Add MPTCP support
1 parent d19f32e commit 6b6241f

3 files changed

Lines changed: 50 additions & 2 deletions

File tree

channel.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

channel_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,28 @@ func TestNewChannel(t *testing.T) {
6464
}, ch.PeerInfo(), "Wrong local peer info")
6565
}
6666

67+
func TestNewChannelEnableMPTCP(t *testing.T) {
68+
ch, err := NewChannel("svc", &ChannelOptions{
69+
ProcessName: "pname",
70+
EnableMPTCP: true,
71+
})
72+
require.NoError(t, err, "NewChannel failed")
73+
74+
assert.Equal(t, LocalPeerInfo{
75+
ServiceName: "svc",
76+
PeerInfo: PeerInfo{
77+
ProcessName: "pname",
78+
HostPort: ephemeralHostPort,
79+
IsEphemeral: true,
80+
Version: PeerVersion{
81+
Language: "go",
82+
LanguageVersion: strings.TrimPrefix(runtime.Version(), "go"),
83+
TChannelVersion: VersionInfo,
84+
},
85+
},
86+
}, ch.PeerInfo(), "Wrong local peer info")
87+
}
88+
6789
func TestLoggers(t *testing.T) {
6890
ch, err := NewChannel("svc", &ChannelOptions{
6991
Logger: NewLogger(ioutil.Discard),

dial_17.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,9 @@ func dialContext(ctx context.Context, hostPort string) (net.Conn, error) {
3232
d := net.Dialer{}
3333
return d.DialContext(ctx, "tcp", hostPort)
3434
}
35+
36+
func dialMPTCPContext(ctx context.Context, hostPort string) (net.Conn, error) {
37+
d := net.Dialer{}
38+
d.SetMultipathTCP(true)
39+
return d.DialContext(ctx, "tcp", hostPort)
40+
}

0 commit comments

Comments
 (0)