Skip to content

Commit

Permalink
fix(logger): ensure full lock when flushing
Browse files Browse the repository at this point in the history
  • Loading branch information
geyslan authored and rafaeldtinoco committed Nov 20, 2023
1 parent 4f82ef0 commit 33622a1
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions pkg/logger/logcounter.go
Expand Up @@ -2,6 +2,8 @@ package logger

import (
"sync"

"golang.org/x/exp/maps"
)

type logOrigin struct {
Expand All @@ -19,26 +21,34 @@ type logCounter struct {
func (lc *logCounter) update(lo logOrigin) {
lc.rwMutex.Lock()
defer lc.rwMutex.Unlock()

lc.data[lo]++
}

func (lc *logCounter) Lookup(key logOrigin) (count uint32, found bool) {
lc.rwMutex.RLock()
defer lc.rwMutex.RUnlock()

count, found = lc.data[key]
return count, found
}

func (lc *logCounter) dump(flush bool) map[logOrigin]uint32 {
lc.rwMutex.RLock()
defer lc.rwMutex.RUnlock()
if flush {
lc.rwMutex.Lock()
defer lc.rwMutex.Unlock()
} else {
lc.rwMutex.RLock()
defer lc.rwMutex.RUnlock()
}

dump := make(map[logOrigin]uint32, len(lc.data))
for k, v := range lc.data {
dump[k] = v
if flush {
delete(lc.data, k)
}
maps.Copy(dump, lc.data)

if flush {
maps.Clear(lc.data)
}

return dump
}

Expand Down

0 comments on commit 33622a1

Please sign in to comment.