Skip to content

Commit

Permalink
clientv3: add DialNoWait to Config for non-blocking New()
Browse files Browse the repository at this point in the history
  • Loading branch information
heyitsanthony committed Aug 12, 2016
1 parent 965b290 commit 1902a14
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
12 changes: 7 additions & 5 deletions clientv3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,13 @@ func (c *Client) dialTarget(endpoint string) (proto string, host string, creds *
return
}

// dialSetupOpts gives the dial opts prioer to any authentication
func (c *Client) dialSetupOpts(endpoint string, dopts ...grpc.DialOption) []grpc.DialOption {
opts := []grpc.DialOption{
grpc.WithBlock(),
grpc.WithTimeout(c.cfg.DialTimeout),
// dialSetupOpts gives the dial opts prior to any authentication
func (c *Client) dialSetupOpts(endpoint string, dopts ...grpc.DialOption) (opts []grpc.DialOption) {
if c.cfg.DialTimeout > 0 {
opts = append(opts, grpc.WithTimeout(c.cfg.DialTimeout))
}
if !c.cfg.DialNoWait {
opts = append(opts, grpc.WithBlock())
}
opts = append(opts, dopts...)

Expand Down
9 changes: 9 additions & 0 deletions clientv3/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ func TestDialTimeout(t *testing.T) {
}
}

func TestDialNoWait(t *testing.T) {
cfg := Config{Endpoints: []string{"foo:12345"}, DialNoWait: true}
c, err := New(cfg)
if c == nil || err != nil {
t.Fatalf("new client with DialNoWait should succeed, got %v", err)
}
c.Close()
}

func TestIsHaltErr(t *testing.T) {
if !isHaltErr(nil, fmt.Errorf("etcdserver: some etcdserver error")) {
t.Errorf(`error prefixed with "etcdserver: " should be Halted by default`)
Expand Down
3 changes: 3 additions & 0 deletions clientv3/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type Config struct {
// DialTimeout is the timeout for failing to establish a connection.
DialTimeout time.Duration

// DialNoWait, when true, returns a Client before establishing a connection.
DialNoWait bool

// TLS holds the client secure credentials, if any.
TLS *tls.Config

Expand Down

0 comments on commit 1902a14

Please sign in to comment.