@@ -101,11 +101,6 @@ const (
101101 defaultReadBufSize = 32 * 1024
102102)
103103
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-
109104type defaultConfigSelector struct {
110105 sc * ServiceConfig
111106}
@@ -117,11 +112,22 @@ func (dcs *defaultConfigSelector) SelectConfig(rpcInfo iresolver.RPCInfo) (*ires
117112 }, nil
118113}
119114
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 ) {
121127 cc := & ClientConn {
122128 target : target ,
123129 conns : make (map [* addrConn ]struct {}),
124- dopts : defaultDialOptions (defaultScheme ),
130+ dopts : defaultDialOptions (),
125131 czData : new (channelzData ),
126132 }
127133
@@ -190,45 +196,36 @@ func newClient(target, defaultScheme string, opts ...DialOption) (conn *ClientCo
190196 return cc , nil
191197}
192198
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 ... )
196204}
197205
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.
202209//
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.
205215//
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.
214217func 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 ... )
217222 if err != nil {
218223 return nil , err
219224 }
220225
221226 // 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.
232229 defer func () {
233230 if err != nil {
234231 cc .Close ()
@@ -712,15 +709,15 @@ func init() {
712709 }
713710}
714711
715- func (cc * ClientConn ) maybeApplyDefaultServiceConfig (addrs []resolver. Address ) {
712+ func (cc * ClientConn ) maybeApplyDefaultServiceConfig () {
716713 if cc .sc != nil {
717- cc .applyServiceConfigAndBalancer (cc .sc , nil , addrs )
714+ cc .applyServiceConfigAndBalancer (cc .sc , nil )
718715 return
719716 }
720717 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 })
722719 } else {
723- cc .applyServiceConfigAndBalancer (emptyServiceConfig , & defaultConfigSelector {emptyServiceConfig }, addrs )
720+ cc .applyServiceConfigAndBalancer (emptyServiceConfig , & defaultConfigSelector {emptyServiceConfig })
724721 }
725722}
726723
@@ -738,7 +735,7 @@ func (cc *ClientConn) updateResolverStateAndUnlock(s resolver.State, err error)
738735 // May need to apply the initial service config in case the resolver
739736 // doesn't support service configs, or doesn't provide a service config
740737 // with the new addresses.
741- cc .maybeApplyDefaultServiceConfig (nil )
738+ cc .maybeApplyDefaultServiceConfig ()
742739
743740 cc .balancerWrapper .resolverError (err )
744741
@@ -750,9 +747,9 @@ func (cc *ClientConn) updateResolverStateAndUnlock(s resolver.State, err error)
750747 var ret error
751748 if cc .dopts .disableServiceConfig {
752749 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 ()
754751 } else if s .ServiceConfig == nil {
755- cc .maybeApplyDefaultServiceConfig (s . Addresses )
752+ cc .maybeApplyDefaultServiceConfig ()
756753 // TODO: do we need to apply a failing LB policy if there is no
757754 // default, per the error handling design?
758755 } else {
@@ -765,7 +762,7 @@ func (cc *ClientConn) updateResolverStateAndUnlock(s resolver.State, err error)
765762 } else {
766763 configSelector = & defaultConfigSelector {sc }
767764 }
768- cc .applyServiceConfigAndBalancer (sc , configSelector , s . Addresses )
765+ cc .applyServiceConfigAndBalancer (sc , configSelector )
769766 } else {
770767 ret = balancer .ErrBadResolverState
771768 if cc .sc == nil {
@@ -1072,7 +1069,7 @@ func (cc *ClientConn) getTransport(ctx context.Context, failfast bool, method st
10721069 })
10731070}
10741071
1075- func (cc * ClientConn ) applyServiceConfigAndBalancer (sc * ServiceConfig , configSelector iresolver.ConfigSelector , addrs []resolver. Address ) {
1072+ func (cc * ClientConn ) applyServiceConfigAndBalancer (sc * ServiceConfig , configSelector iresolver.ConfigSelector ) {
10761073 if sc == nil {
10771074 // should never reach here.
10781075 return
@@ -1747,7 +1744,7 @@ func (cc *ClientConn) parseTargetAndFindResolver() error {
17471744 // scheme, except when a custom dialer is specified in which case, we should
17481745 // always use passthrough scheme. For either case, we need to respect any overridden
17491746 // global defaults set by the user.
1750- defScheme := cc .dopts .defScheme
1747+ defScheme := cc .dopts .defaultScheme
17511748 if internal .UserSetDefaultScheme {
17521749 defScheme = resolver .GetDefaultScheme ()
17531750 }
0 commit comments