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
35 changes: 31 additions & 4 deletions internal/syscall/syscall_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package syscall

import (
"errors"
"fmt"
"net"
"syscall"
Expand All @@ -32,6 +33,8 @@ import (
"google.golang.org/grpc/grpclog"
)

var GetTCPUserTimeoutNoopError = errors.New("placeholder error")

// GetCPUTime returns the how much CPU time has passed since the start of this process.
func GetCPUTime() int64 {
var ts unix.Timespec
Expand Down Expand Up @@ -76,15 +79,39 @@ func SetTCPUserTimeout(conn net.Conn, timeout time.Duration) error {
// not a TCP connection. exit early
return nil
}
file, err := tcpconn.File()
rawConn, err := tcpconn.SyscallConn()
if err != nil {
return fmt.Errorf("error getting file for connection: %v", err)
return fmt.Errorf("error getting raw connection: %v", err)
}
err = syscall.SetsockoptInt(int(file.Fd()), syscall.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, int(timeout/time.Millisecond))
file.Close()
err = rawConn.Control(func(fd uintptr) {
err = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, int(timeout/time.Millisecond))
})
if err != nil {
return fmt.Errorf("error setting option on socket: %v", err)
}

return nil
}

// GetTCPUserTimeout gets the TCP user timeout on a connection's socket
func GetTCPUserTimeout(conn net.Conn) (opt int, err error) {
tcpconn, ok := conn.(*net.TCPConn)
if !ok {
err = fmt.Errorf("conn is not *net.TCPConn. got %T", conn)
return
}
rawConn, err := tcpconn.SyscallConn()
if err != nil {
err = fmt.Errorf("error getting raw connection: %v", err)
return
}
err = rawConn.Control(func(fd uintptr) {
opt, err = syscall.GetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_USER_TIMEOUT)
})
if err != nil {
err = fmt.Errorf("error getting option on socket: %v", err)
return
}

return
}
8 changes: 8 additions & 0 deletions internal/syscall/syscall_nonlinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
package syscall

import (
"errors"
"net"
"time"

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

var GetTCPUserTimeoutNoopError = errors.New("GetTCPUserTimeout is a no-op on non-linux or appengine environments")

func init() {
grpclog.Info("CPU time info is unavailable on non-linux or appengine environment.")
}
Expand Down Expand Up @@ -55,3 +58,8 @@ func CPUTimeDiff(first *Rusage, latest *Rusage) (float64, float64) {
func SetTCPUserTimeout(conn net.Conn, timeout time.Duration) error {
return nil
}

// GetTCPUserTimeout is a no-op function under non-linux or appengine environments
func GetTCPUserTimeout(conn net.Conn) (int, error) {
return 0, GetTCPUserTimeoutNoopError
}
51 changes: 51 additions & 0 deletions internal/transport/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"golang.org/x/net/http2/hpack"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/internal/leakcheck"
"google.golang.org/grpc/internal/syscall"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/status"
)
Expand Down Expand Up @@ -2317,3 +2318,53 @@ func TestHeaderTblSize(t *testing.T) {
t.Fatalf("expected len(limits) = 2 within 10s, got != 2")
}
}

func TestTCPUserTimeout(t *testing.T) {
tests := []struct {
time time.Duration
timeout time.Duration
}{
{
10 * time.Second,
10 * time.Second,
},
{
0,
0,
},
}
for _, tt := range tests {
lis, err := net.Listen("tcp", "localhost:0")
Copy link
Member

Choose a reason for hiding this comment

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

You should only need to make the listener once, right (i.e. outside the loop)?

You also never call lis.Accept(), which seems like it should be necessary. You would also need to read from the conn on the server side, because the client writes its 24-byte preface before returning (we think this is the cause of a separate test flake we're having).

if err != nil {
t.Fatalf("Failed to listen. Err: %v", err)
}
defer lis.Close()
// TODO(deklerk): we can `defer cancel()` here after we drop Go 1.6 support. Until then,
// doing a `defer cancel()` could cause the dialer to become broken:
// https://github.com/golang/go/issues/15078, https://github.com/golang/go/issues/15035
connectCtx, cancel := context.WithDeadline(context.Background(), time.Now().Add(2*time.Second))
client, err := newHTTP2Client(connectCtx, context.Background(), TargetInfo{Addr: lis.Addr().String()}, ConnectOptions{
KeepaliveParams: keepalive.ClientParameters{
Time: tt.time,
Timeout: tt.timeout,
},
}, func() {}, func(GoAwayReason) {}, func() {})
if err != nil {
cancel() // Do not cancel in success path.
t.Fatalf("error creating client: %v", err)
}
defer client.Close()

opt, err := syscall.GetTCPUserTimeout(client.conn)
if err != nil {
if err == syscall.GetTCPUserTimeoutNoopError {
t.Skipf("skipping test on unsupported environment: %v", err)
}
t.Fatalf("GetTCPUserTimeout error: %v", err)
}
if timeoutMS := int(tt.timeout / time.Millisecond); timeoutMS != opt {
t.Fatalf("wrong TCP_USER_TIMEOUT set on conn. expected %d. got %d",
timeoutMS, opt)
}
}
}