From c66158093766da032adcce837867559eda9029a0 Mon Sep 17 00:00:00 2001 From: Vojtech Vitek Date: Wed, 24 Jul 2024 19:57:11 +0200 Subject: [PATCH] localCounter: Use RWMutex to speed up lookups --- local_counter.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/local_counter.go b/local_counter.go index 7c55c7c..7c49858 100644 --- a/local_counter.go +++ b/local_counter.go @@ -14,7 +14,7 @@ type localCounter struct { counters map[uint64]*count windowLength time.Duration lastEvict time.Time - mu sync.Mutex + mu sync.RWMutex } type count struct { @@ -25,6 +25,7 @@ type count struct { func (c *localCounter) Config(requestLimit int, windowLength time.Duration) { c.mu.Lock() defer c.mu.Unlock() + c.windowLength = windowLength } @@ -52,8 +53,8 @@ func (c *localCounter) IncrementBy(key string, currentWindow time.Time, amount i } func (c *localCounter) Get(key string, currentWindow, previousWindow time.Time) (int, int, error) { - c.mu.Lock() - defer c.mu.Unlock() + c.mu.RLock() + defer c.mu.RUnlock() curr, ok := c.counters[LimitCounterKey(key, currentWindow)] if !ok {