8000 localCounter: Use RWMutex to speed up lookups (#33) · go-chi/httprate@c6b43ff · GitHub
[go: up one dir, main page]

Skip to content

Commit c6b43ff

Browse files
authored
localCounter: Use RWMutex to speed up lookups (#33)
1 parent 7228fd5 commit c6b43ff

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

local_counter.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type localCounter struct {
1414
counters map[uint64]*count
1515
windowLength time.Duration
1616
lastEvict time.Time
17-
mu sync.Mutex
17+
mu sync.RWMutex
1818
}
1919

2020
type count struct {
@@ -25,6 +25,7 @@ type count struct {
2525
func (c *localCounter) Config(requestLimit int, windowLength time.Duration) {
2626
c.mu.Lock()
2727
defer c.mu.Unlock()
28+
2829
c.windowLength = windowLength
2930
}
3031

@@ -52,8 +53,8 @@ func (c *localCounter) IncrementBy(key string, currentWindow time.Time, amount i
5253
}
5354

5455
func (c *localCounter) Get(key string, currentWindow, previousWindow time.Time) (int, int, error) {
55-
c.mu.Lock()
56-
defer c.mu.Unlock()
56+
c.mu.RLock()
57+
defer c.mu.RUnlock()
5758

5859
curr, ok := c.counters[LimitCounterKey(key, currentWindow)]
5960
if !ok {

0 commit comments

Comments
 (0)
0