clientv3: respect dial timeout when authenticating
Fixes
@@ -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
}
