fix: resolve race condition in mock mode update loop
This commit is contained in:
parent
219fcc6de5
commit
f7c4e9cdde
1 changed files with 9 additions and 4 deletions
|
|
@ -20,6 +20,7 @@ var (
|
||||||
enabled atomic.Bool
|
enabled atomic.Bool
|
||||||
updateTicker *time.Ticker
|
updateTicker *time.Ticker
|
||||||
stopUpdatesCh chan struct{}
|
stopUpdatesCh chan struct{}
|
||||||
|
updateLoopWg sync.WaitGroup
|
||||||
)
|
)
|
||||||
|
|
||||||
const updateInterval = 2 * time.Second
|
const updateInterval = 2 * time.Second
|
||||||
|
|
@ -125,7 +126,9 @@ func startUpdateLoopLocked() {
|
||||||
stopUpdatesCh = stopCh
|
stopUpdatesCh = stopCh
|
||||||
updateTicker = ticker
|
updateTicker = ticker
|
||||||
|
|
||||||
|
updateLoopWg.Add(1)
|
||||||
go func(stop <-chan struct{}, tick *time.Ticker) {
|
go func(stop <-chan struct{}, tick *time.Ticker) {
|
||||||
|
defer updateLoopWg.Done()
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-tick.C:
|
case <-tick.C:
|
||||||
|
|
@ -139,14 +142,16 @@ func startUpdateLoopLocked() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func stopUpdateLoopLocked() {
|
func stopUpdateLoopLocked() {
|
||||||
if ticker := updateTicker; ticker != nil {
|
|
||||||
ticker.Stop()
|
|
||||||
updateTicker = nil
|
|
||||||
}
|
|
||||||
if ch := stopUpdatesCh; ch != nil {
|
if ch := stopUpdatesCh; ch != nil {
|
||||||
close(ch)
|
close(ch)
|
||||||
stopUpdatesCh = nil
|
stopUpdatesCh = nil
|
||||||
}
|
}
|
||||||
|
if ticker := updateTicker; ticker != nil {
|
||||||
|
ticker.Stop()
|
||||||
|
updateTicker = nil
|
||||||
|
}
|
||||||
|
// Wait for the update goroutine to exit
|
||||||
|
updateLoopWg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateMetrics(cfg MockConfig) {
|
func updateMetrics(cfg MockConfig) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue