Skip to content

Commit

Permalink
clientv3: respect dial timeout when authenticating
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Romano committed Mar 31, 2017
1 parent 9ca7f22 commit 62d7bae
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions clientv3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,16 @@ func (c *Client) dial(endpoint string, dopts ...grpc.DialOption) (*grpc.ClientCo
tokenMu: &sync.RWMutex{},
}

err := c.getToken(c.ctx)
if err != nil {
ctx := c.ctx
if c.cfg.DialTimeout > 0 {
cctx, cancel := context.WithTimeout(ctx, c.cfg.DialTimeout)
defer cancel()
ctx = cctx
}
if err := c.getToken(ctx); err != nil {
if err == ctx.Err() && ctx.Err() != c.ctx.Err() {
err = grpc.ErrClientConnTimeout
}
return nil, err
}

Expand Down Expand Up @@ -351,6 +359,8 @@ func newClient(cfg *Config) (*Client, error) {
client.balancer = newSimpleBalancer(cfg.Endpoints)
conn, err := client.dial(cfg.Endpoints[0], grpc.WithBalancer(client.balancer))
if err != nil {
client.cancel()
client.balancer.Close()
return nil, err
}
client.conn = conn
Expand All @@ -374,6 +384,7 @@ func newClient(cfg *Config) (*Client, error) {
default:
}
client.cancel()
client.balancer.Close()
conn.Close()
return nil, err
}
Expand Down

0 comments on commit 62d7bae

Please sign in to comment.