From cb8215d53ced4885db2f76b18177d2664b1d4bcc Mon Sep 17 00:00:00 2001 From: Christian Kruse Date: Thu, 21 Sep 2023 13:14:42 -0700 Subject: [PATCH] refactor deleteMutex usage Signed-off-by: Christian Kruse --- pkg/processor/k8sprocessor/kube/owner.go | 31 +++++++++++++----------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/pkg/processor/k8sprocessor/kube/owner.go b/pkg/processor/k8sprocessor/kube/owner.go index 4dac0eebe7..cd9f49c0dd 100644 --- a/pkg/processor/k8sprocessor/kube/owner.go +++ b/pkg/processor/k8sprocessor/kube/owner.go @@ -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: @@ -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()