clientv3: let user provide a client context through Config

This commit is contained in:
Anthony Romano
2017-02-16 13:16:10 -08:00
parent 5d3597a5f2
commit c9452c6ad4
2 changed files with 12 additions and 2 deletions

View File

@ -272,7 +272,7 @@ func (c *Client) dial(endpoint string, dopts ...grpc.DialOption) (*grpc.ClientCo
tokenMu: &sync.RWMutex{}, tokenMu: &sync.RWMutex{},
} }
err := c.getToken(context.TODO()) err := c.getToken(c.ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -307,7 +307,12 @@ func newClient(cfg *Config) (*Client, error) {
} }
// use a temporary skeleton client to bootstrap first connection // 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{ client := &Client{
conn: nil, conn: nil,
cfg: *cfg, cfg: *cfg,

View File

@ -18,6 +18,7 @@ import (
"crypto/tls" "crypto/tls"
"time" "time"
"golang.org/x/net/context"
"google.golang.org/grpc" "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 is a list of dial options for the grpc client (e.g., for interceptors).
DialOptions []grpc.DialOption 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
} }