forked from AprilM36/AprilM36-google-api-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
transport: Add dialer with tcp user timeout
The new dialer is only supported by go1.11+, and is only used when GOOGLE_CLOUD_ENABLE_DIRECT_PATH contains specific services. e.g. GOOGLE_CLOUD_ENABLE_DIRECT_PATH=spanner,bigtable means only spanner and bigtable will be using this dialer. Note that currently this socketopt dialer will overwrite user defined dialer if any. I will update this logic once we have a way to introspect the dialoptions(grpc/grpc-go#2795). Change-Id: I1e3873cd52be4583f4c868771d3d2257637c400d Reviewed-on: https://code-review.googlesource.com/c/google-api-go-client/+/40272 Reviewed-by: Chris Broadfoot <[email protected]> Reviewed-by: kokoro <[email protected]>
- Loading branch information
1 parent
4d000f9
commit d1680c5
Showing
6 changed files
with
195 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2019 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// +build go1.11,linux | ||
|
||
package grpc | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"syscall" | ||
|
||
"golang.org/x/sys/unix" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
const ( | ||
// defaultTCPUserTimeout is the default TCP_USER_TIMEOUT socket option. By | ||
// default is 20 seconds. | ||
tcpUserTimeoutMilliseconds = 20000 | ||
) | ||
|
||
func init() { | ||
// timeoutDialerOption is a grpc.DialOption that contains dialer with | ||
// socket option TCP_USER_TIMEOUT. This dialer requires go versions 1.11+. | ||
timeoutDialerOption = grpc.WithContextDialer(dialTCPUserTimeout) | ||
} | ||
|
||
func dialTCPUserTimeout(ctx context.Context, addr string) (net.Conn, error) { | ||
control := func(network, address string, c syscall.RawConn) error { | ||
var syscallErr error | ||
controlErr := c.Control(func(fd uintptr) { | ||
syscallErr = syscall.SetsockoptInt( | ||
int(fd), syscall.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, tcpUserTimeoutMilliseconds) | ||
}) | ||
if syscallErr != nil { | ||
return syscallErr | ||
} | ||
if controlErr != nil { | ||
return controlErr | ||
} | ||
return nil | ||
} | ||
d := &net.Dialer{ | ||
Control: control, | ||
} | ||
return d.DialContext(ctx, "tcp", addr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Copyright 2019 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// +build go1.11,linux | ||
|
||
package grpc | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net" | ||
"os" | ||
"syscall" | ||
"testing" | ||
"time" | ||
|
||
"golang.org/x/oauth2" | ||
"golang.org/x/sys/unix" | ||
"google.golang.org/api/option" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
func TestDialTCPUserTimeout(t *testing.T) { | ||
l, err := net.Listen("tcp", ":3000") | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
defer l.Close() | ||
|
||
go func() { | ||
conn, err := l.Accept() | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
if err := conn.Close(); err != nil { | ||
t.Error(err) | ||
} | ||
}() | ||
|
||
conn, err := dialTCPUserTimeout(context.Background(), ":3000") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer conn.Close() | ||
|
||
timeout, err := getTCPUserTimeout(conn) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if timeout != tcpUserTimeoutMilliseconds { | ||
t.Fatalf("expected %v, got %v", tcpUserTimeoutMilliseconds, timeout) | ||
} | ||
} | ||
|
||
func getTCPUserTimeout(conn net.Conn) (int, error) { | ||
tcpconn, ok := conn.(*net.TCPConn) | ||
if !ok { | ||
return 0, fmt.Errorf("conn is not *net.TCPConn. got %T", conn) | ||
} | ||
rawConn, err := tcpconn.SyscallConn() | ||
if err != nil { | ||
return 0, err | ||
} | ||
var timeout int | ||
var syscalErr error | ||
controlErr := rawConn.Control(func(fd uintptr) { | ||
timeout, syscalErr = syscall.GetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_USER_TIMEOUT) | ||
}) | ||
if syscalErr != nil { | ||
return 0, syscalErr | ||
} | ||
if controlErr != nil { | ||
return 0, controlErr | ||
} | ||
return timeout, nil | ||
} | ||
|
||
// Check that tcp timeout dialer overwrites user defined dialer. | ||
func TestDialWithDirectPathEnabled(t *testing.T) { | ||
os.Setenv("GOOGLE_CLOUD_ENABLE_DIRECT_PATH", "example,other") | ||
defer os.Clearenv() | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) | ||
|
||
userDialer := grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) { | ||
t.Error("did not expect a call to user dialer, got one") | ||
cancel() | ||
return nil, errors.New("not expected") | ||
}) | ||
|
||
conn, err := Dial(ctx, | ||
option.WithTokenSource(oauth2.StaticTokenSource(nil)), // No creds. | ||
option.WithGRPCDialOption(userDialer), | ||
option.WithEndpoint("example.google.com:443")) | ||
if err != nil { | ||
t.Errorf("DialGRPC: error %v, want nil", err) | ||
} | ||
defer conn.Close() | ||
|
||
// gRPC doesn't connect before the first call. | ||
grpc.Invoke(ctx, "foo", nil, nil, conn) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters