@@ -101,11 +101,6 @@ const (
101
101
defaultReadBufSize = 32 * 1024
102
102
)
103
103
104
- // Dial creates a client connection to the given target.
105
- func Dial (target string , opts ... DialOption ) (* ClientConn , error ) {
106
- return DialContext (context .Background (), target , opts ... )
107
- }
108
-
109
104
type defaultConfigSelector struct {
110
105
sc * ServiceConfig
111
106
}
@@ -117,11 +112,22 @@ func (dcs *defaultConfigSelector) SelectConfig(rpcInfo iresolver.RPCInfo) (*ires
117
112
}, nil
118
113
}
119
114
120
- func newClient (target , defaultScheme string , opts ... DialOption ) (conn * ClientConn , err error ) {
115
+ // NewClient creates a new gRPC "channel" for the target URI provided. No I/O
116
+ // is performed. Use of the ClientConn for RPCs will automatically cause it to
117
+ // connect. Connect may be used to manually create a connection, but for most
118
+ // users this is unnecessary.
119
+ //
120
+ // The target name syntax is defined in
121
+ // https://github.com/grpc/grpc/blob/master/doc/naming.md. e.g. to use dns
122
+ // resolver, a "dns:///" prefix should be applied to the target.
123
+ //
124
+ // The DialOptions returned by WithBlock, WithTimeout, and
125
+ // WithReturnConnectionError are ignored by this function.
126
+ func NewClient (target string , opts ... DialOption ) (conn * ClientConn , err error ) {
121
127
cc := & ClientConn {
122
128
target : target ,
123
129
conns : make (map [* addrConn ]struct {}),
124
- dopts : defaultDialOptions (defaultScheme ),
130
+ dopts : defaultDialOptions (),
125
131
czData : new (channelzData ),
126
132
}
127
133
@@ -190,45 +196,36 @@ func newClient(target, defaultScheme string, opts ...DialOption) (conn *ClientCo
190
196
return cc , nil
191
197
}
192
198
193
- // NewClient returns a new client in idle mode.
194
- func NewClient (target string , opts ... DialOption ) (conn * ClientConn , err error ) {
195
- return newClient (target , "dns" , opts ... )
199
+ // Dial calls DialContext(context.Background(), target, opts...).
200
+ //
201
+ // Deprecated: use NewClient instead. Will be supported throughout 1.x.
202
+ func Dial (target string , opts ... DialOption ) (* ClientConn , error ) {
203
+ return DialContext (context .Background (), target , opts ... )
196
204
}
197
205
198
- // DialContext creates a client connection to the given target. By default, it's
199
- // a non-blocking dial (the function won't wait for connections to be
200
- // established, and connecting happens in the background). To make it a blocking
201
- // dial, use WithBlock() dial option.
206
+ // DialContext calls NewClient and then exits idle mode. If WithBlock(true) is
207
+ // used, it calls Connect and WaitForStateChange until either the context
208
+ // expires or the state of the ClientConn is Ready.
202
209
//
203
- // In the non-blocking case, the ctx does not act against the connection. It
204
- // only controls the setup steps.
210
+ // One subtle difference between NewClient and Dial and DialContext is that the
211
+ // former uses "dns" as the default name resolver, while the latter use
212
+ // "passthrough" for backward compatibility. This distinction should not matter
213
+ // to most users, but could matter to legacy users that specify a custom dialer
214
+ // and expect it to receive the target string directly.
205
215
//
206
- // In the blocking case, ctx can be used to cancel or expire the pending
207
- // connection. Once this function returns, the cancellation and expiration of
208
- // ctx will be noop. Users should call ClientConn.Close to terminate all the
209
- // pending operations after this function returns.
210
- //
211
- // The target name syntax is defined in
212
- // https://github.com/grpc/grpc/blob/master/doc/naming.md.
213
- // e.g. to use dns resolver, a "dns:///" prefix should be applied to the target.
216
+ // Deprecated: use NewClient instead. Will be supported throughout 1.x.
214
217
func DialContext (ctx context.Context , target string , opts ... DialOption ) (conn * ClientConn , err error ) {
215
- // At the end of this method, we kick the channel out of idle, rather than waiting for the first rpc.
216
- cc , err := newClient (target , "passthrough" , opts ... )
218
+ // At the end of this method, we kick the channel out of idle, rather than
219
+ // waiting for the first rpc.
220
+ opts = append ([]DialOption {withDefaultScheme ("passthrough" )}, opts ... )
221
+ cc , err := NewClient (target , opts ... )
217
222
if err != nil {
218
223
return nil , err
219
224
}
220
225
221
226
// We start the channel off in idle mode, but kick it out of idle now,
222
- // instead of waiting for the first RPC. Other gRPC implementations do wait
223
- // for the first RPC to kick the channel out of idle. But doing so would be
224
- // a major behavior change for our users who are used to seeing the channel
225
- // active after Dial.
226
- //
227
- // Taking this approach of kicking it out of idle at the end of this method
228
- // allows us to share the code between channel creation and exiting idle
229
- // mode. This will also make it easy for us to switch to starting the
230
- // channel off in idle, i.e. by making newClient exported.
231
-
227
+ // instead of waiting for the first RPC. This is the legacy behavior of
228
+ // Dial.
232
229
defer func () {
233
230
if err != nil {
234
231
cc .Close ()
@@ -712,15 +709,15 @@ func init() {
712
709
}
713
710
}
714
711
715
- func (cc * ClientConn ) maybeApplyDefaultServiceConfig (addrs []resolver. Address ) {
712
+ func (cc * ClientConn ) maybeApplyDefaultServiceConfig () {
716
713
if cc .sc != nil {
717
- cc .applyServiceConfigAndBalancer (cc .sc , nil , addrs )
714
+ cc .applyServiceConfigAndBalancer (cc .sc , nil )
718
715
return
719
716
}
720
717
if cc .dopts .defaultServiceConfig != nil {
721
- cc .applyServiceConfigAndBalancer (cc .dopts .defaultServiceConfig , & defaultConfigSelector {cc .dopts .defaultServiceConfig }, addrs )
718
+ cc .applyServiceConfigAndBalancer (cc .dopts .defaultServiceConfig , & defaultConfigSelector {cc .dopts .defaultServiceConfig })
722
719
} else {
723
- cc .applyServiceConfigAndBalancer (emptyServiceConfig , & defaultConfigSelector {emptyServiceConfig }, addrs )
720
+ cc .applyServiceConfigAndBalancer (emptyServiceConfig , & defaultConfigSelector {emptyServiceConfig })
724
721
}
725
722
}
726
723
@@ -738,7 +735,7 @@ func (cc *ClientConn) updateResolverStateAndUnlock(s resolver.State, err error)
738
735
// May need to apply the initial service config in case the resolver
739
736
// doesn't support service configs, or doesn't provide a service config
740
737
// with the new addresses.
741
- cc .maybeApplyDefaultServiceConfig (nil )
738
+ cc .maybeApplyDefaultServiceConfig ()
742
739
743
740
cc .balancerWrapper .resolverError (err )
744
741
@@ -750,9 +747,9 @@ func (cc *ClientConn) updateResolverStateAndUnlock(s resolver.State, err error)
750
747
var ret error
751
748
if cc .dopts .disableServiceConfig {
752
749
channelz .Infof (logger , cc .channelzID , "ignoring service config from resolver (%v) and applying the default because service config is disabled" , s .ServiceConfig )
753
- cc .maybeApplyDefaultServiceConfig (s . Addresses )
750
+ cc .maybeApplyDefaultServiceConfig ()
754
751
} else if s .ServiceConfig == nil {
755
- cc .maybeApplyDefaultServiceConfig (s . Addresses )
752
+ cc .maybeApplyDefaultServiceConfig ()
756
753
// TODO: do we need to apply a failing LB policy if there is no
757
754
// default, per the error handling design?
758
755
} else {
@@ -765,7 +762,7 @@ func (cc *ClientConn) updateResolverStateAndUnlock(s resolver.State, err error)
765
762
} else {
766
763
configSelector = & defaultConfigSelector {sc }
767
764
}
768
- cc .applyServiceConfigAndBalancer (sc , configSelector , s . Addresses )
765
+ cc .applyServiceConfigAndBalancer (sc , configSelector )
769
766
} else {
770
767
ret = balancer .ErrBadResolverState
771
768
if cc .sc == nil {
@@ -1072,7 +1069,7 @@ func (cc *ClientConn) getTransport(ctx context.Context, failfast bool, method st
1072
1069
})
1073
1070
}
1074
1071
1075
- func (cc * ClientConn ) applyServiceConfigAndBalancer (sc * ServiceConfig , configSelector iresolver.ConfigSelector , addrs []resolver. Address ) {
1072
+ func (cc * ClientConn ) applyServiceConfigAndBalancer (sc * ServiceConfig , configSelector iresolver.ConfigSelector ) {
1076
1073
if sc == nil {
1077
1074
// should never reach here.
1078
1075
return
@@ -1747,7 +1744,7 @@ func (cc *ClientConn) parseTargetAndFindResolver() error {
1747
1744
// scheme, except when a custom dialer is specified in which case, we should
1748
1745
// always use passthrough scheme. For either case, we need to respect any overridden
1749
1746
// global defaults set by the user.
1750
- defScheme := cc .dopts .defScheme
1747
+ defScheme := cc .dopts .defaultScheme
1751
1748
if internal .UserSetDefaultScheme {
1752
1749
defScheme = resolver .GetDefaultScheme ()
1753
1750
}
0 commit comments