Skip to content

Commit

Permalink
Disable TCP keepalives when grpc keelapives are enabled. grpc#6250
Browse files Browse the repository at this point in the history
When setting TCP_USER_TIMEOUT to the default value of 20 seconds, the
tcp connection resets after only a single lost keepalive packet.
This is the result of golang tcp sockets defaulting to a tcp keepalive
interval of just 15 seconds.
  • Loading branch information
Lucaber committed May 11, 2023
1 parent afcbdc9 commit 87df68d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/transport/http2_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ func newHTTP2Client(connectCtx, ctx context.Context, addr resolver.Address, opts
}
keepaliveEnabled := false
if kp.Time != infinity {
if tcpConn, isTcpConn := conn.(*net.TCPConn); isTcpConn {
if err = tcpConn.SetKeepAlive(false); err != nil {
return nil, connectionErrorf(false, err, "transport: failed to disable tcp keepalive: %v", err)
}
}
if err = syscall.SetTCPUserTimeout(conn, kp.Timeout); err != nil {
return nil, connectionErrorf(false, err, "transport: failed to set TCP_USER_TIMEOUT: %v", err)
}
Expand Down
5 changes: 5 additions & 0 deletions internal/transport/http2_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ func NewServerTransport(conn net.Conn, config *ServerConfig) (_ ServerTransport,
kp.Timeout = defaultServerKeepaliveTimeout
}
if kp.Time != infinity {
if tcpConn, isTcpConn := conn.(*net.TCPConn); isTcpConn {
if err = tcpConn.SetKeepAlive(false); err != nil {
return nil, connectionErrorf(false, err, "transport: failed to disable tcp keepalive: %v", err)
}
}
if err = syscall.SetTCPUserTimeout(conn, kp.Timeout); err != nil {
return nil, connectionErrorf(false, err, "transport: failed to set TCP_USER_TIMEOUT: %v", err)
}
Expand Down

0 comments on commit 87df68d

Please sign in to comment.