From ce864d5b8cb0b2c9446028abee41844c1a7dd5ee Mon Sep 17 00:00:00 2001 From: Donald Plummer Date: Thu, 31 Mar 2022 15:54:44 -0700 Subject: [PATCH] make logger threadsafe (#9) l.events is being accessed in a thread, so needs to be made threadsafe. Adding a mutex ensures only one thread accesses it at a time. --- logger.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/logger.go b/logger.go index 7a9c291..9a13aa6 100644 --- a/logger.go +++ b/logger.go @@ -2,6 +2,7 @@ package statsig import ( "strconv" + "sync" "time" ) @@ -31,6 +32,7 @@ type logger struct { events []interface{} transport *transport tick *time.Ticker + mu sync.Mutex } func newLogger(transport *transport) *logger { @@ -68,6 +70,9 @@ func (l *logger) logExposure(evt exposureEvent) { } func (l *logger) logInternal(evt interface{}) { + l.mu.Lock() + defer l.mu.Unlock() + l.events = append(l.events, evt) if len(l.events) >= maxEvents { l.flush(false) @@ -113,6 +118,9 @@ func (l *logger) logConfigExposure( } func (l *logger) flush(closing bool) { + l.mu.Lock() + defer l.mu.Unlock() + if closing { l.tick.Stop() }