Skip to content

Commit

Permalink
Set linger on p2p connections (#2646)
Browse files Browse the repository at this point in the history
Co-authored-by: Patrick O'Grady <prohb125@gmail.com>
  • Loading branch information
joshua-kim and patrick-ogrady committed Feb 28, 2023
1 parent 379ac81 commit 1a2dca1
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions network/network.go
Expand Up @@ -49,6 +49,11 @@ const (
TimeSinceLastMsgReceivedKey = "timeSinceLastMsgReceived"
TimeSinceLastMsgSentKey = "timeSinceLastMsgSent"
SendFailRateKey = "sendFailRate"

// lingerTimeout is the amount of time (in seconds) we allow for the
// remaining data in a connection to be flushed before forcibly closing the
// connection (TCP RST).
lingerTimeout = 15
)

var (
Expand Down Expand Up @@ -731,6 +736,7 @@ func (n *network) Dispatch() error {
n.metrics.acceptFailed.Inc()
continue
}
n.setLinger(conn)

// Note: listener.Accept is rate limited outside of this package, so a
// peer can not just arbitrarily spin up goroutines here.
Expand Down Expand Up @@ -1124,6 +1130,7 @@ func (n *network) dial(ctx context.Context, nodeID ids.NodeID, ip *trackedIP) {
)
continue
}
n.setLinger(conn)

n.peerConfig.Log.Verbo("starting to upgrade connection",
zap.String("direction", "outbound"),
Expand All @@ -1144,6 +1151,24 @@ func (n *network) dial(ctx context.Context, nodeID ids.NodeID, ip *trackedIP) {
}()
}

// setLinger sets the linger on [conn], if it is a [*net.TCPConn], to
// [lingerTimeout].
func (n *network) setLinger(conn net.Conn) {
tcpConn, ok := conn.(*net.TCPConn)
if !ok {
return
}

// If a connection is closed, we allow a grace period for the unsent
// data in the connection to be flushed before forcibly closing the
// connection (TCP RST).
if err := tcpConn.SetLinger(lingerTimeout); err != nil {
n.peerConfig.Log.Warn("failed to set no linger",
zap.Error(err),
)
}
}

// upgrade the provided connection, which may be an inbound connection or an
// outbound connection, with the provided [upgrader].
//
Expand Down

0 comments on commit 1a2dca1

Please sign in to comment.