clientv3: respect dial timeout when authenticating

Fixes #7627
release-3.2
Anthony Romano 2017-03-31 14:24:43 -07:00
parent 9ca7f22e84
commit 62d7bae496
1 changed files with 13 additions and 2 deletions

View File

@ -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
}
@ -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
@ -374,6 +384,7 @@ func newClient(cfg *Config) (*Client, error) {
default:
}
client.cancel()
client.balancer.Close()
conn.Close()
return nil, err
}