Browse Source

clientv3: respect dial timeout when authenticating

Fixes #7627
Anthony Romano 8 years ago
parent
commit
62d7bae496
1 changed files with 13 additions and 2 deletions
  1. 13 2
      clientv3/client.go

+ 13 - 2
clientv3/client.go

@@ -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
 		}