Fixing data race in count(..) method by passing sync.Map by pointer

This commit is contained in:
Pavel Shevaev 2022-10-14 14:59:16 +03:00
parent 077086ec59
commit 421eeb3750
1 changed files with 3 additions and 3 deletions

View File

@ -16,7 +16,7 @@ const maxStackLen = 1000
//NOTE: for simplicity not making it an atomic variable
var enabled bool = false
var res2track = sync.Map{}
var res2track = &sync.Map{}
type TrackedInfo struct {
Time time.Time
@ -70,7 +70,7 @@ func Enable(flag bool) {
}
func Reset() {
res2track = sync.Map{}
res2track = &sync.Map{}
}
func IsOn() bool {
@ -81,7 +81,7 @@ func Count() int {
return count(res2track)
}
func count(m sync.Map) int {
func count(m *sync.Map) int {
var i int
m.Range(func(k, v interface{}) bool {
i++