Skip to content

Commit

Permalink
util/slowset: fix data race
Browse files Browse the repository at this point in the history
Reported by `go test -race`.
Also use time.Ticker to response faster to stopCh.

Fixes: 99cf66f
  • Loading branch information
huww98 committed Aug 19, 2024
1 parent 29c857e commit 1744e4e
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions pkg/util/slowset.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,25 @@ func (s *SlowSet) TimeRemaining(key string) time.Duration {
return 0
}

func (s *SlowSet) removeAllExpired() {
s.Lock()
defer s.Unlock()
for key, t := range s.workSet {
if time.Since(t) > s.retentionTime {
delete(s.workSet, key)
}
}
}

func (s *SlowSet) Run(stopCh <-chan struct{}) {
ticker := time.NewTicker(s.resyncPeriod)
defer ticker.Stop()
for {
select {
case <-stopCh:
return
default:
time.Sleep(s.resyncPeriod)
for key, t := range s.workSet {
if time.Since(t) > s.retentionTime {
s.Remove(key)
}
}
case <-ticker.C:
s.removeAllExpired()
}
}
}

0 comments on commit 1744e4e

Please sign in to comment.