Skip to content

Commit d7673c4

Browse files
solve race
1 parent fa0d658 commit d7673c4

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

internal/xds/clients/xdsclient/xdsclient.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ func New(config Config) (*XDSClient, error) {
119119
// SetWatchExpiryTimeoutForTesting override the default watch expiry timeout
120120
// with provided timeout value.
121121
func (c *XDSClient) SetWatchExpiryTimeoutForTesting(watchExpiryTimeout time.Duration) {
122+
if watchExpiryTimeout <= 0 {
123+
watchExpiryTimeout = defaultWatchExpiryTimeout
124+
}
122125
c.watchExpiryTimeout = watchExpiryTimeout
123126
}
124127

internal/xds/xdsclient/pool.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func NewPool(config *bootstrap.Config) *Pool {
9999
// expected to invoke once they are done using the client. It is safe for the
100100
// caller to invoke this close function multiple times.
101101
func (p *Pool) NewClient(name string, metricsRecorder estats.MetricsRecorder) (XDSClient, func(), error) {
102-
return p.newRefCounted(name, metricsRecorder)
102+
return p.newRefCounted(name, metricsRecorder, 0)
103103
}
104104

105105
// NewClientForTesting returns an xDS client configured with the provided
@@ -126,11 +126,11 @@ func (p *Pool) NewClientForTesting(opts OptionsForTesting) (XDSClient, func(), e
126126
if opts.MetricsRecorder == nil {
127127
opts.MetricsRecorder = istats.NewMetricsRecorderList(nil)
128128
}
129-
c, cancel, err := p.newRefCounted(opts.Name, opts.MetricsRecorder)
129+
c, cancel, err := p.newRefCounted(opts.Name, opts.MetricsRecorder, opts.WatchExpiryTimeout)
130130
if err != nil {
131131
return nil, nil, err
132132
}
133-
c.SetWatchExpiryTimeoutForTesting(opts.WatchExpiryTimeout)
133+
// c.SetWatchExpiryTimeoutForTesting(opts.WatchExpiryTimeout)
134134
return c, cancel, nil
135135
}
136136

@@ -252,7 +252,7 @@ func (p *Pool) clientRefCountedClose(name string) {
252252
// newRefCounted creates a new reference counted xDS client implementation for
253253
// name, if one does not exist already. If an xDS client for the given name
254254
// exists, it gets a reference to it and returns it.
255-
func (p *Pool) newRefCounted(name string, metricsRecorder estats.MetricsRecorder) (*clientImpl, func(), error) {
255+
func (p *Pool) newRefCounted(name string, metricsRecorder estats.MetricsRecorder, watchExpiryTimeout time.Duration) (*clientImpl, func(), error) {
256256
p.mu.Lock()
257257
defer p.mu.Unlock()
258258

@@ -275,17 +275,19 @@ func (p *Pool) newRefCounted(name string, metricsRecorder estats.MetricsRecorder
275275
c.incrRef()
276276
return c, sync.OnceFunc(func() { p.clientRefCountedClose(name) }), nil
277277
}
278-
279278
c, err := newClientImpl(config, metricsRecorder, name)
280279
if err != nil {
281280
return nil, nil, err
282281
}
282+
c.XDSClient.SetWatchExpiryTimeoutForTesting(watchExpiryTimeout)
283283
if logger.V(2) {
284284
c.logger.Infof("Created client with name %q and bootstrap configuration:\n %s", name, config)
285285
}
286286
p.clients[name] = c
287287
xdsClientImplCreateHook(name)
288288

289289
logger.Infof("xDS node ID: %s", config.Node().GetId())
290-
return c, sync.OnceFunc(func() { p.clientRefCountedClose(name) }), nil
290+
return c, sync.OnceFunc(func() {
291+
p.clientRefCountedClose(name)
292+
}), nil
291293
}

0 commit comments

Comments
 (0)