Skip to content

Commit

Permalink
Merge pull request #433 from huww98/fix-race-upstream
Browse files Browse the repository at this point in the history
util/slowset: fix data race
  • Loading branch information
k8s-ci-robot committed Aug 19, 2024
2 parents 29c857e + 1744e4e commit 8393eb7
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 8393eb7

Please sign in to comment.