NewClient creates a new gRPC "channel" for the target URI provided. No I/O is performed. Use of the ClientConn for RPCs will automatically cause it to connect. The Connect method may be called to manually create a connection, but for most users this should be unnecessary. The target name syntax
(target string, opts ...DialOption)
| 183 | // WithReturnConnectionError, and FailOnNonTempDialError are ignored by this |
| 184 | // function. |
| 185 | func NewClient(target string, opts ...DialOption) (conn *ClientConn, err error) { |
| 186 | cc := &ClientConn{ |
| 187 | target: target, |
| 188 | conns: make(map[*addrConn]struct{}), |
| 189 | dopts: defaultDialOptions(), |
| 190 | } |
| 191 | |
| 192 | cc.retryThrottler.Store((*retryThrottler)(nil)) |
| 193 | cc.safeConfigSelector.UpdateConfigSelector(&defaultConfigSelector{nil}) |
| 194 | cc.ctx, cc.cancel = context.WithCancel(context.Background()) |
| 195 | |
| 196 | // Apply dial options. |
| 197 | disableGlobalOpts := false |
| 198 | for _, opt := range opts { |
| 199 | if _, ok := opt.(*disableGlobalDialOptions); ok { |
| 200 | disableGlobalOpts = true |
| 201 | break |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | if !disableGlobalOpts { |
| 206 | for _, opt := range globalDialOptions { |
| 207 | opt.apply(&cc.dopts) |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | for _, opt := range opts { |
| 212 | opt.apply(&cc.dopts) |
| 213 | } |
| 214 | |
| 215 | // Determine the resolver to use. |
| 216 | if err := cc.initParsedTargetAndResolverBuilder(); err != nil { |
| 217 | return nil, err |
| 218 | } |
| 219 | |
| 220 | for _, opt := range globalPerTargetDialOptions { |
| 221 | opt.DialOptionForTarget(cc.parsedTarget.URL).apply(&cc.dopts) |
| 222 | } |
| 223 | |
| 224 | chainUnaryClientInterceptors(cc) |
| 225 | chainStreamClientInterceptors(cc) |
| 226 | |
| 227 | if err := cc.validateTransportCredentials(); err != nil { |
| 228 | return nil, err |
| 229 | } |
| 230 | |
| 231 | if cc.dopts.defaultServiceConfigRawJSON != nil { |
| 232 | scpr := parseServiceConfig(*cc.dopts.defaultServiceConfigRawJSON, cc.dopts.maxCallAttempts) |
| 233 | if scpr.Err != nil { |
| 234 | return nil, fmt.Errorf("%s: %v", invalidDefaultServiceConfigErrPrefix, scpr.Err) |
| 235 | } |
| 236 | cc.dopts.defaultServiceConfig, _ = scpr.Config.(*ServiceConfig) |
| 237 | } |
| 238 | cc.keepaliveParams = cc.dopts.copts.KeepaliveParams |
| 239 | |
| 240 | if err = cc.initAuthority(); err != nil { |
| 241 | return nil, err |
| 242 | } |