Skip to content

Commit

Permalink
Fix FailOnNonTempDialError and adds a test for it
Browse files Browse the repository at this point in the history
- Fixes grpc#2266
  • Loading branch information
virtuald committed Aug 25, 2018
1 parent e00d249 commit 222a6be
Show file tree
Hide file tree
Showing 2 changed files with 27 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
20 changes: 20 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,25 @@ 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 := context.Background()
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

0 comments on commit 222a6be

Please sign in to comment.