From 3c28380bc651885ea79deab21c20a40b10c4ac7c Mon Sep 17 00:00:00 2001 From: Cecylia Bocovich Date: Fri, 13 Sep 2019 12:26:27 -0400 Subject: [PATCH] Add locks to safelog The safelog Write function can be called from multiple go routines, and it was not thread safe. These locks in particular allow us to pass the logscrubber's output io.Writer to other libraries, such as pion. --- common/safelog/log.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/common/safelog/log.go b/common/safelog/log.go index 1241676..9148e53 100644 --- a/common/safelog/log.go +++ b/common/safelog/log.go @@ -7,6 +7,7 @@ import ( "bytes" "io" "regexp" + "sync" ) const ipv4Address = `\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}` @@ -30,8 +31,13 @@ var addressRegexp = regexp.MustCompile(addressPattern) type LogScrubber struct { Output io.Writer buffer []byte + + lock sync.Mutex } +func (ls *LogScrubber) Lock() { (*ls).lock.Lock() } +func (ls *LogScrubber) Unlock() { (*ls).lock.Unlock() } + func scrub(b []byte) []byte { scrubbedBytes := b for _, pattern := range scrubberPatterns { @@ -45,6 +51,9 @@ func scrub(b []byte) []byte { } func (ls *LogScrubber) Write(b []byte) (n int, err error) { + ls.Lock() + defer ls.Unlock() + n = len(b) ls.buffer = append(ls.buffer, b...) for {