Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client: set TCP_USER_TIMEOUT socket option for linux #2307

Merged
merged 10 commits into from
Nov 5, 2018
23 changes: 23 additions & 0 deletions internal/syscall/syscall_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
package syscall

import (
"errors"
"fmt"
"net"
"syscall"
"time"

"golang.org/x/sys/unix"
"google.golang.org/grpc/grpclog"
Expand Down Expand Up @@ -65,3 +69,22 @@ func CPUTimeDiff(first *Rusage, latest *Rusage) (float64, float64) {

return uTimeElapsed, sTimeElapsed
}

// SetTCPUserTimeout sets the TCP user timeout on a connection's socket
func SetTCPUserTimeout(conn net.Conn, timeout time.Duration) error {
tcpconn, ok := conn.(*net.TCPConn)
if !ok {
return errors.New("error casting *net.Conn to *net.TCPConn")
}
file, err := tcpconn.File()
if err != nil {
return fmt.Errorf("error getting file for connection: %v", err)
}
err = syscall.SetsockoptInt(int(file.Fd()), syscall.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, int(timeout/time.Millisecond))
file.Close()
if err != nil {
return fmt.Errorf("error setting option on socket: %v", err)
}

return nil
}
12 changes: 11 additions & 1 deletion internal/syscall/syscall_nonlinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@

package syscall

import "google.golang.org/grpc/grpclog"
import (
"net"
"time"

"google.golang.org/grpc/grpclog"
)

func init() {
grpclog.Info("CPU time info is unavailable on non-linux or appengine environment.")
Expand All @@ -45,3 +50,8 @@ func GetRusage() (rusage *Rusage) {
func CPUTimeDiff(first *Rusage, latest *Rusage) (float64, float64) {
return 0, 0
}

// SetTCPUserTimeout is a no-op function under non-linux or appengine environments
func SetTCPUserTimeout(conn net.Conn, timeout time.Duration) error {
return nil
}
4 changes: 4 additions & 0 deletions internal/transport/http2_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/internal/channelz"
"google.golang.org/grpc/internal/syscall"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
Expand Down Expand Up @@ -252,6 +253,9 @@ func newHTTP2Client(connectCtx, ctx context.Context, addr TargetInfo, opts Conne
t.channelzID = channelz.RegisterNormalSocket(t, opts.ChannelzParentID, "")
}
if t.kp.Time != infinity {
if err = syscall.SetTCPUserTimeout(t.conn, kp.Timeout); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need to be done before handshaking ~line 170, otherwise you won't have a TCPConn anymore.

Also, you need to only do this for TCP connections. The dialer could return a net.Conn backed by a UDS instead.

return nil, connectionErrorf(false, err, "transport: failed to set TCP_USER_TIMEOUT: %v", err)
}
t.keepaliveEnabled = true
go t.keepalive()
}
Expand Down