diff --git a/clientv3/client.go b/clientv3/client.go index 3d36c3ab5..ecda65333 100644 --- a/clientv3/client.go +++ b/clientv3/client.go @@ -272,7 +272,7 @@ func (c *Client) dial(endpoint string, dopts ...grpc.DialOption) (*grpc.ClientCo tokenMu: &sync.RWMutex{}, } - err := c.getToken(context.TODO()) + err := c.getToken(c.ctx) if err != nil { return nil, err } @@ -307,7 +307,12 @@ func newClient(cfg *Config) (*Client, error) { } // use a temporary skeleton client to bootstrap first connection - ctx, cancel := context.WithCancel(context.TODO()) + baseCtx := context.TODO() + if cfg.Context != nil { + baseCtx = cfg.Context + } + + ctx, cancel := context.WithCancel(baseCtx) client := &Client{ conn: nil, cfg: *cfg, diff --git a/clientv3/config.go b/clientv3/config.go index 2082f7b91..69bfe5d80 100644 --- a/clientv3/config.go +++ b/clientv3/config.go @@ -18,6 +18,7 @@ import ( "crypto/tls" "time" + "golang.org/x/net/context" "google.golang.org/grpc" ) @@ -43,4 +44,8 @@ type Config struct { // DialOptions is a list of dial options for the grpc client (e.g., for interceptors). DialOptions []grpc.DialOption + + // Context is the default client context; it can be used to cancel grpc dial out and + // other operations that do not have an explicit context. + Context context.Context }