Skip to content

Commit

Permalink
[fix] Pass context to goroutine (#2823)
Browse files Browse the repository at this point in the history
* Pass context to goroutine instead of through struct

Signed-off-by: albertteoh <[email protected]>

* Fix comment grammar

Signed-off-by: albertteoh <[email protected]>
  • Loading branch information
albertteoh authored Feb 22, 2021
1 parent bfd17cd commit d9453e0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
8 changes: 3 additions & 5 deletions plugin/sampling/strategystore/static/strategy_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ type strategyStore struct {

storedStrategies atomic.Value // holds *storedStrategies

ctx context.Context
cancelFunc context.CancelFunc
}

Expand All @@ -58,7 +57,6 @@ func NewStrategyStore(options Options, logger *zap.Logger) (ss.StrategyStore, er
ctx, cancelFunc := context.WithCancel(context.Background())
h := &strategyStore{
logger: logger,
ctx: ctx,
cancelFunc: cancelFunc,
}
h.storedStrategies.Store(defaultStrategies())
Expand All @@ -76,7 +74,7 @@ func NewStrategyStore(options Options, logger *zap.Logger) (ss.StrategyStore, er
h.parseStrategies(strategies)

if options.ReloadInterval > 0 {
go h.autoUpdateStrategies(options.ReloadInterval, loadFn)
go h.autoUpdateStrategies(ctx, options.ReloadInterval, loadFn)
}
return h, nil
}
Expand Down Expand Up @@ -144,15 +142,15 @@ func samplingStrategyLoader(strategiesFile string) strategyLoader {
}
}

func (h *strategyStore) autoUpdateStrategies(interval time.Duration, loader strategyLoader) {
func (h *strategyStore) autoUpdateStrategies(ctx context.Context, interval time.Duration, loader strategyLoader) {
lastValue := string(nullJSON)
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
lastValue = h.reloadSamplingStrategy(loader, lastValue)
case <-h.ctx.Done():
case <-ctx.Done():
return
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func TestAutoUpdateStrategyWithFile(t *testing.T) {
require.NoError(t, err)
assert.EqualValues(t, makeResponse(sampling.SamplingStrategyType_PROBABILISTIC, 0.8), *s)

// verify that reloading in no-op
// verify that reloading is a no-op
value := store.reloadSamplingStrategy(samplingStrategyLoader(dstFile), string(srcBytes))
assert.Equal(t, string(srcBytes), value)

Expand Down

0 comments on commit d9453e0

Please sign in to comment.