Skip to content

Commit

Permalink
make logger threadsafe (#9)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dplummer authored Mar 31, 2022
1 parent c7d0e3d commit ce864d5
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package statsig

import (
"strconv"
"sync"
"time"
)

Expand Down Expand Up @@ -31,6 +32,7 @@ type logger struct {
events []interface{}
transport *transport
tick *time.Ticker
mu sync.Mutex
}

func newLogger(transport *transport) *logger {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit ce864d5

Please sign in to comment.