Skip to content

Commit

Permalink
refactor deleteMutex usage
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Kruse <[email protected]>
  • Loading branch information
c-kruse committed Sep 21, 2023
1 parent 331b6ea commit cb8215d
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions pkg/processor/k8sprocessor/kube/owner.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,20 +497,7 @@ func (op *OwnerCache) deleteLoop(interval time.Duration, gracePeriod time.Durati
for {
select {
case <-ticker.C:
var cutoff int
now := time.Now()
op.deleteMu.Lock()
for i, d := range op.deleteQueue {
if d.ts.Add(gracePeriod).After(now) {
break
}
cutoff = i + 1
}
toDelete := op.deleteQueue[:cutoff]
op.deleteQueue = op.deleteQueue[cutoff:]
op.deleteMu.Unlock()

for _, d := range toDelete {
for _, d := range op.nextDeleteQueue(gracePeriod) {
d.evict()
}
case <-op.stopCh:
Expand All @@ -519,6 +506,22 @@ func (op *OwnerCache) deleteLoop(interval time.Duration, gracePeriod time.Durati
}
}

func (op *OwnerCache) nextDeleteQueue(gracePeriod time.Duration) []ownerCacheEviction {
var cutoff int
now := time.Now()
op.deleteMu.Lock()
defer op.deleteMu.Unlock()
for i, d := range op.deleteQueue {
if d.ts.Add(gracePeriod).After(now) {
break
}
cutoff = i + 1
}
toDelete := op.deleteQueue[:cutoff]
op.deleteQueue = op.deleteQueue[cutoff:]
return toDelete
}

type ownerCacheEviction struct {
ts time.Time
evict func()
Expand Down

0 comments on commit cb8215d

Please sign in to comment.