Skip to content

Commit

Permalink
Merge pull request #7227 from heyitsanthony/clientv3-dial-ctx
Browse files Browse the repository at this point in the history
clientv3: use DialContext
  • Loading branch information
Anthony Romano authored Jan 25, 2017
2 parents 11619f8 + 56286cc commit 094be29
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
3 changes: 2 additions & 1 deletion clientv3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ func (c *Client) dialSetupOpts(endpoint string, dopts ...grpc.DialOption) (opts
return nil, c.ctx.Err()
default:
}
return net.DialTimeout(proto, host, t)
dialer := &net.Dialer{Timeout: t}
return dialer.DialContext(c.ctx, proto, host)
}
opts = append(opts, grpc.WithDialer(f))

Expand Down
42 changes: 42 additions & 0 deletions clientv3/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package clientv3

import (
"fmt"
"net"
"testing"
"time"

Expand All @@ -25,6 +26,47 @@ import (
"google.golang.org/grpc"
)

func TestDialCancel(t *testing.T) {
defer testutil.AfterTest(t)

// accept first connection so client is created with dial timeout
ln, err := net.Listen("unix", "dialcancel:12345")
if err != nil {
t.Fatal(err)
}
defer ln.Close()

ep := "unix://dialcancel:12345"
cfg := Config{
Endpoints: []string{ep},
DialTimeout: 30 * time.Second}
c, err := New(cfg)
if err != nil {
t.Fatal(err)
}

// connect to ipv4 blackhole so dial blocks
c.SetEndpoints("http://254.0.0.1:12345")

// issue Get to force redial attempts
go c.Get(context.TODO(), "abc")

// wait a little bit so client close is after dial starts
time.Sleep(100 * time.Millisecond)

donec := make(chan struct{})
go func() {
defer close(donec)
c.Close()
}()

select {
case <-time.After(5 * time.Second):
t.Fatalf("failed to close")
case <-donec:
}
}

func TestDialTimeout(t *testing.T) {
defer testutil.AfterTest(t)

Expand Down

0 comments on commit 094be29

Please sign in to comment.