mirror of https://github.com/grpc/grpc-go.git
Detach user-passed context (via DialContext(...)) with the context of ClientConn
This commit is contained in:
parent
7f061361d3
commit
60c350b0e9
|
@ -220,14 +220,23 @@ func Dial(target string, opts ...DialOption) (*ClientConn, error) {
|
||||||
return DialContext(context.Background(), target, opts...)
|
return DialContext(context.Background(), target, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialContext creates a client connection to the given target
|
// DialContext creates a client connection to the given target. ctx can be used to
|
||||||
// using the supplied context.
|
// cancel or expire the pending connecting. Once the initial connection is done,
|
||||||
|
// the cancellation and expiration of ctx will not affect the connection.
|
||||||
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(ctx)
|
cc.ctx, cc.cancel = context.WithCancel(context.Background())
|
||||||
|
go func() {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
cc.Close()
|
||||||
|
case <-cc.ctx.Done():
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
opt(&cc.dopts)
|
opt(&cc.dopts)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue