From 26acf5689e19eeb15248a12eb5e4a3a8477e6fda Mon Sep 17 00:00:00 2001 From: Alejandro Lopez Date: Fri, 30 Jun 2023 10:12:17 +0300 Subject: [PATCH] [#92] Ensure policer objects cannot be worked on concurrently Signed-off-by: Alejandro Lopez --- pkg/services/policer/policer.go | 4 +++- pkg/services/policer/process.go | 11 +++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/pkg/services/policer/policer.go b/pkg/services/policer/policer.go index d49ad4632c..a68b194d4b 100644 --- a/pkg/services/policer/policer.go +++ b/pkg/services/policer/policer.go @@ -29,10 +29,12 @@ func (oiw *objectsInWork) remove(addr oid.Address) { oiw.Unlock() } -func (oiw *objectsInWork) add(addr oid.Address) { +func (oiw *objectsInWork) add(addr oid.Address) bool { oiw.Lock() + _, exists := oiw.objs[addr] oiw.objs[addr] = struct{}{} oiw.Unlock() + return !exists } // Policer represents the utility that verifies diff --git a/pkg/services/policer/process.go b/pkg/services/policer/process.go index 6bd38f6180..39b61c8a09 100644 --- a/pkg/services/policer/process.go +++ b/pkg/services/policer/process.go @@ -51,12 +51,11 @@ func (p *Policer) shardPolicyWorker(ctx context.Context) { return } - p.objsInWork.add(addr.Address) - - p.processObject(ctx, addr) - - p.cache.Add(addr.Address, time.Now()) - p.objsInWork.remove(addr.Address) + if p.objsInWork.add(addr.Address) { + p.processObject(ctx, addr) + p.cache.Add(addr.Address, time.Now()) + p.objsInWork.remove(addr.Address) + } }) if err != nil { p.log.Warn(logs.PolicerPoolSubmission, zap.Error(err))