Skip to content

Commit

Permalink
client: fix FailOnNonTempDialError and add a test for it (#2276)
Browse files Browse the repository at this point in the history
  • Loading branch information
virtuald authored and dfawley committed Aug 27, 2018
1 parent e00d249 commit 91c7ef8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions clientconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,13 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *
s := cc.GetState()
if s == connectivity.Ready {
break
} else if cc.dopts.copts.FailOnNonTempDialError && s == connectivity.TransientFailure {
if err = cc.blockingpicker.connectionError(); err != nil {
terr, ok := err.(interface{ Temporary() bool })
if ok && !terr.Temporary() {
return nil, err
}
}
}
if !cc.WaitForStateChange(ctx, s) {
// ctx got timeout or canceled.
Expand Down
21 changes: 21 additions & 0 deletions clientconn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/internal/backoff"
"google.golang.org/grpc/internal/leakcheck"
"google.golang.org/grpc/internal/transport"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/naming"
"google.golang.org/grpc/resolver"
Expand Down Expand Up @@ -443,6 +444,26 @@ func TestDialContextCancel(t *testing.T) {
}
}

type failFastError struct{}

func (failFastError) Error() string { return "failfast" }
func (failFastError) Temporary() bool { return false }

func TestDialContextFailFast(t *testing.T) {
defer leakcheck.Check(t)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
failErr := failFastError{}
dialer := func(string, time.Duration) (net.Conn, error) {
return nil, failErr
}

_, err := DialContext(ctx, "Non-Existent.Server:80", WithBlock(), WithInsecure(), WithDialer(dialer), FailOnNonTempDialError(true))
if terr, ok := err.(transport.ConnectionError); !ok || terr.Origin() != failErr {
t.Fatalf("DialContext() = _, %v, want _, %v", err, failErr)
}
}

// blockingBalancer mimics the behavior of balancers whose initialization takes a long time.
// In this test, reading from blockingBalancer.Notify() blocks forever.
type blockingBalancer struct {
Expand Down
3 changes: 3 additions & 0 deletions dialoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,9 @@ func WithStatsHandler(h stats.Handler) DialOption {
// error, gRPC will fail the connection to the network address and won't try to
// reconnect. The default value of FailOnNonTempDialError is false.
//
// FailOnNonTempDialError only affects the initial dial, and does not do
// anything useful unless you are also using WithBlock().
//
// This is an EXPERIMENTAL API.
func FailOnNonTempDialError(f bool) DialOption {
return newFuncDialOption(func(o *dialOptions) {
Expand Down

0 comments on commit 91c7ef8

Please sign in to comment.