fix the issue
This commit is contained in:
@ -221,19 +221,29 @@ func Dial(target string, opts ...DialOption) (*ClientConn, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialContext creates a client connection to the given target. ctx can be used to
|
// DialContext creates a client connection to the given target. ctx can be used to
|
||||||
// cancel or expire the pending connecting. Once the initial connection is done,
|
// cancel or expire the pending connecting. Once this function returns, the
|
||||||
// the cancellation and expiration of ctx will not affect the connection.
|
// cancellation and expiration of ctx will be noop. Users should call ClientConn.Close
|
||||||
|
// to terminate all the pending operations after this function returns.
|
||||||
func DialContext(ctx context.Context, target string, opts ...DialOption) (*ClientConn, error) {
|
func DialContext(ctx context.Context, target string, opts ...DialOption) (*ClientConn, error) {
|
||||||
cc := &ClientConn{
|
cc := &ClientConn{
|
||||||
target: target,
|
target: target,
|
||||||
conns: make(map[Address]*addrConn),
|
conns: make(map[Address]*addrConn),
|
||||||
}
|
}
|
||||||
cc.ctx, cc.cancel = context.WithCancel(context.Background())
|
cc.ctx, cc.cancel = context.WithCancel(context.Background())
|
||||||
|
done := make(chan struct{})
|
||||||
|
defer close(done)
|
||||||
go func() {
|
go func() {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
cc.Close()
|
cc.Close()
|
||||||
|
case <-done:
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
cc.Close()
|
||||||
|
default:
|
||||||
|
}
|
||||||
case <-cc.ctx.Done():
|
case <-cc.ctx.Done():
|
||||||
|
// ClientConn.Close has been called.
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -71,13 +71,9 @@ func TestTLSDialTimeout(t *testing.T) {
|
|||||||
|
|
||||||
func TestDialContextCancel(t *testing.T) {
|
func TestDialContextCancel(t *testing.T) {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
go cancel()
|
cancel()
|
||||||
conn, err := DialContext(ctx, "Non-Existent.Server:80", WithBlock(), WithInsecure())
|
if _, err := DialContext(ctx, "Non-Existent.Server:80", WithBlock(), WithInsecure()); err != context.Canceled && err != ErrClientConnClosing {
|
||||||
if err == nil {
|
t.Fatalf("grpc.DialContext(%v, _) = _, %v, want _, %v or %v", ctx, err, context.Canceled, ErrClientConnClosing)
|
||||||
conn.Close()
|
|
||||||
}
|
|
||||||
if err != context.Canceled {
|
|
||||||
t.Fatalf("DialContext(_, _) = %v, %v, want %v", conn, err, context.Canceled)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user