Skip to content

Commit

Permalink
Add mutex to loggerPromise to prevent races
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelSpeed committed Apr 1, 2019
1 parent e4c40bc commit cf499c8
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions pkg/log/deleg.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package log

import (
"sync"

"github.com/go-logr/logr"
)

Expand All @@ -25,6 +27,7 @@ import (
type loggerPromise struct {
logger *DelegatingLogger
childPromises []*loggerPromise
promisesLock *sync.Mutex

name *string
tags []interface{}
Expand All @@ -33,19 +36,27 @@ type loggerPromise struct {
// WithName provides a new Logger with the name appended
func (p *loggerPromise) WithName(l *DelegatingLogger, name string) *loggerPromise {
res := &loggerPromise{
logger: l,
name: &name,
logger: l,
name: &name,
promisesLock: &sync.Mutex{},
}

p.promisesLock.Lock()
defer p.promisesLock.Unlock()
p.childPromises = append(p.childPromises, res)
return res
}

// WithValues provides a new Logger with the tags appended
func (p *loggerPromise) WithValues(l *DelegatingLogger, tags ...interface{}) *loggerPromise {
res := &loggerPromise{
logger: l,
tags: tags,
logger: l,
tags: tags,
promisesLock: &sync.Mutex{},
}

p.promisesLock.Lock()
defer p.promisesLock.Unlock()
p.childPromises = append(p.childPromises, res)
return res
}
Expand Down Expand Up @@ -119,7 +130,7 @@ func (l *DelegatingLogger) Fulfill(actual logr.Logger) {
func NewDelegatingLogger(initial logr.Logger) *DelegatingLogger {
l := &DelegatingLogger{
Logger: initial,
promise: &loggerPromise{},
promise: &loggerPromise{promisesLock: &sync.Mutex{}},
}
l.promise.logger = l
return l
Expand Down

0 comments on commit cf499c8

Please sign in to comment.