From 3a50a767a3952a65d041e3fb9e1255aa7f7a5d26 Mon Sep 17 00:00:00 2001 From: Albert Teoh Date: Mon, 16 Dec 2024 21:49:42 +1100 Subject: [PATCH 1/3] Wrap map mutations with mutex lock --- util.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/util.go b/util.go index a6264c6..93758f1 100644 --- a/util.go +++ b/util.go @@ -1,8 +1,13 @@ package posthog +import ( + "sync" +) + type SizeLimitedMap struct { ids map[string][]string size int + mu sync.Mutex } func newSizeLimitedMap(size int) *SizeLimitedMap { @@ -15,6 +20,8 @@ func newSizeLimitedMap(size int) *SizeLimitedMap { } func (sizeLimitedMap *SizeLimitedMap) add(key string, element string) { + sizeLimitedMap.mu.Lock() + defer sizeLimitedMap.mu.Unlock() if len(sizeLimitedMap.ids) >= sizeLimitedMap.size { sizeLimitedMap.ids = map[string][]string{} From 0d0f64bf704fd91f58dd3442582c5e3c496bfeb1 Mon Sep 17 00:00:00 2001 From: Albert Teoh Date: Mon, 16 Dec 2024 22:01:39 +1100 Subject: [PATCH 2/3] Use RWMutex --- util.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/util.go b/util.go index 93758f1..2841f19 100644 --- a/util.go +++ b/util.go @@ -7,7 +7,7 @@ import ( type SizeLimitedMap struct { ids map[string][]string size int - mu sync.Mutex + mu sync.RWMutex } func newSizeLimitedMap(size int) *SizeLimitedMap { @@ -35,6 +35,9 @@ func (sizeLimitedMap *SizeLimitedMap) add(key string, element string) { } func (sizeLimitedMap *SizeLimitedMap) contains(key string, element string) bool { + sizeLimitedMap.mu.RLock() + defer sizeLimitedMap.mu.RUnlock() + if val, ok := sizeLimitedMap.ids[key]; ok { for _, v := range val { if v == element { @@ -47,5 +50,8 @@ func (sizeLimitedMap *SizeLimitedMap) contains(key string, element string) bool } func (sizeLimitedMap *SizeLimitedMap) count() int { + sizeLimitedMap.mu.RLock() + defer sizeLimitedMap.mu.RUnlock() + return len(sizeLimitedMap.ids) } From c81ee057ef59f1448a83b6c623c79cb0a3d63e13 Mon Sep 17 00:00:00 2001 From: Albert Teoh Date: Mon, 16 Dec 2024 22:07:11 +1100 Subject: [PATCH 3/3] Use standard lock --- util.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/util.go b/util.go index 2841f19..f15927a 100644 --- a/util.go +++ b/util.go @@ -7,7 +7,7 @@ import ( type SizeLimitedMap struct { ids map[string][]string size int - mu sync.RWMutex + mu sync.Mutex } func newSizeLimitedMap(size int) *SizeLimitedMap { @@ -35,8 +35,8 @@ func (sizeLimitedMap *SizeLimitedMap) add(key string, element string) { } func (sizeLimitedMap *SizeLimitedMap) contains(key string, element string) bool { - sizeLimitedMap.mu.RLock() - defer sizeLimitedMap.mu.RUnlock() + sizeLimitedMap.mu.Lock() + defer sizeLimitedMap.mu.Unlock() if val, ok := sizeLimitedMap.ids[key]; ok { for _, v := range val { @@ -50,8 +50,8 @@ func (sizeLimitedMap *SizeLimitedMap) contains(key string, element string) bool } func (sizeLimitedMap *SizeLimitedMap) count() int { - sizeLimitedMap.mu.RLock() - defer sizeLimitedMap.mu.RUnlock() + sizeLimitedMap.mu.Lock() + defer sizeLimitedMap.mu.Unlock() return len(sizeLimitedMap.ids) }