newRefCounted creates a new reference counted xDS client implementation for name, if one does not exist already. If an xDS client for the given name exists, it gets a reference to it and returns it.
(name string, metricsRecorder estats.MetricsRecorder, watchExpiryTimeout time.Duration, bConfig *bootstrap.Config)
| 269 | // name, if one does not exist already. If an xDS client for the given name |
| 270 | // exists, it gets a reference to it and returns it. |
| 271 | func (p *Pool) newRefCounted(name string, metricsRecorder estats.MetricsRecorder, watchExpiryTimeout time.Duration, bConfig *bootstrap.Config) (*clientImpl, func(), error) { |
| 272 | p.mu.Lock() |
| 273 | defer p.mu.Unlock() |
| 274 | |
| 275 | if c := p.clients[name]; c != nil { |
| 276 | c.incrRef() |
| 277 | return c, sync.OnceFunc(func() { p.clientRefCountedClose(name) }), nil |
| 278 | } |
| 279 | |
| 280 | config := bConfig |
| 281 | if config == nil { |
| 282 | var err error |
| 283 | config, err = p.getConfiguration() |
| 284 | if err != nil { |
| 285 | return nil, nil, fmt.Errorf("xds: failed to read xDS bootstrap config from env vars: %v", err) |
| 286 | } |
| 287 | if config == nil { |
| 288 | // If the environment variables are not set, then fallback bootstrap |
| 289 | // configuration should be set before attempting to create an xDS client, |
| 290 | // else xDS client creation will fail. |
| 291 | config = p.fallbackConfig |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | if config == nil { |
| 296 | return nil, nil, fmt.Errorf("failed to read xDS bootstrap config from env vars: bootstrap environment variables (%q or %q) not defined and fallback config not set", envconfig.XDSBootstrapFileNameEnv, envconfig.XDSBootstrapFileContentEnv) |
| 297 | } |
| 298 | |
| 299 | c, err := newClientImpl(config, metricsRecorder, name, watchExpiryTimeout) |
| 300 | if err != nil { |
| 301 | return nil, nil, err |
| 302 | } |
| 303 | if logger.V(2) { |
| 304 | c.logger.Infof("Created client with name %q and bootstrap configuration:\n %s", name, config) |
| 305 | } |
| 306 | p.clients[name] = c |
| 307 | xdsClientImplCreateHook(name) |
| 308 | |
| 309 | logger.Infof("xDS node ID: %s", config.Node().GetId()) |
| 310 | return c, sync.OnceFunc(func() { p.clientRefCountedClose(name) }), nil |
| 311 | } |
no test coverage detected