Skip to content

Commit

Permalink
client: surface connection errors to callers
Browse files Browse the repository at this point in the history
This commit allows blocking clients to receive a more informative error
message than "context deadline exceeded", which is especially helpful in
tracking down persistent client misconfiguration (such as an invalid TLS
certificate, an invalid server that's refusing connections, etc.)
  • Loading branch information
sethp-nr committed Feb 28, 2020
1 parent e139b47 commit 0f76d65
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
12 changes: 11 additions & 1 deletion clientconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,14 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *
defer func() {
select {
case <-ctx.Done():
conn, err = nil, ctx.Err()
switch {
case ctx.Err() == err:
conn = nil
case err == nil:
conn, err = nil, ctx.Err()
default:
conn, err = nil, fmt.Errorf("%v: %v", ctx.Err(), err)
}
default:
}
}()
Expand Down Expand Up @@ -321,6 +328,9 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *
}
if !cc.WaitForStateChange(ctx, s) {
// ctx got timeout or canceled.
if err = cc.blockingpicker.connectionError(); err != nil {
return nil, err
}
return nil, ctx.Err()
}
}
Expand Down
5 changes: 3 additions & 2 deletions clientconn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,9 @@ func (s) TestDialWaitsForServerSettingsAndFails(t *testing.T) {
client.Close()
t.Fatalf("Unexpected success (err=nil) while dialing")
}
if err != context.DeadlineExceeded {
t.Fatalf("DialContext(_) = %v; want context.DeadlineExceeded", err)
expectedMsg := "server handshake"
if !strings.Contains(err.Error(), context.DeadlineExceeded.Error()) || !strings.Contains(err.Error(), expectedMsg) {
t.Fatalf("DialContext(_) = %v; want a message that includes both %q and %q", err, context.DeadlineExceeded.Error(), expectedMsg)
}
<-done
if numConns < 2 {
Expand Down

0 comments on commit 0f76d65

Please sign in to comment.