Skip to content

Commit

Permalink
Merge pull request #36 from libp2p/fix/linger
Browse files Browse the repository at this point in the history
set linger to 0 for both inbound and outbound connections
  • Loading branch information
Stebalien authored Apr 4, 2019
2 parents a2176a3 + 87f331a commit 2785529
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions p2p/transport/tcp/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tcp

import (
"context"
"net"
"time"

logging "github.com/ipfs/go-log"
Expand All @@ -20,6 +21,29 @@ var DefaultConnectTimeout = 5 * time.Second

var log = logging.Logger("tcp-tpt")

// try to set linger on the connection, if possible.
func tryLinger(conn net.Conn, sec int) {
if lingerConn, ok := conn.(interface {
SetLinger(int) error
}); ok {
_ = lingerConn.SetLinger(sec)
}
}

type lingerListener struct {
manet.Listener
sec int
}

func (ll *lingerListener) Accept() (manet.Conn, error) {
c, err := ll.Listener.Accept()
if err != nil {
return nil, err
}
tryLinger(c, ll.sec)
return c, nil
}

// TcpTransport is the TCP transport.
type TcpTransport struct {
// Connection upgrader for upgrading insecure stream connections to
Expand Down Expand Up @@ -73,6 +97,10 @@ func (t *TcpTransport) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID)
if err != nil {
return nil, err
}
// Set linger to 0 so we never get stuck in the TIME-WAIT state. When
// linger is 0, connections are _reset_ instead of closed with a FIN.
// This means we can immediately reuse the 5-tuple and reconnect.
tryLinger(conn, 0)
return t.Upgrader.UpgradeOutbound(ctx, t, conn, p)
}

Expand All @@ -94,6 +122,7 @@ func (t *TcpTransport) Listen(laddr ma.Multiaddr) (tpt.Listener, error) {
if err != nil {
return nil, err
}
list = &lingerListener{list, 0}
return t.Upgrader.UpgradeListener(t, list), nil
}

Expand Down

0 comments on commit 2785529

Please sign in to comment.