fix: thread-safe in getWriter of logx (#2319)

master
Kevin Wan 2 years ago committed by GitHub
parent 8c72136631
commit dfeef5e497
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -358,19 +358,9 @@ func errorTextSync(msg string) {
}
func getWriter() Writer {
var w Writer
writer.lock.RLock()
w = writer.writer
writer.lock.RUnlock()
w := writer.Load()
if w == nil {
writer.lock.Lock()
if writer.writer == nil {
writer.writer = newConsoleWriter()
}
w = writer.writer
writer.lock.Unlock()
w = writer.StoreIfNil(newConsoleWriter())
}
return w

@ -67,6 +67,17 @@ func (w *atomicWriter) Store(v Writer) {
w.writer = v
}
func (w *atomicWriter) StoreIfNil(v Writer) Writer {
w.lock.Lock()
defer w.lock.Unlock()
if w.writer == nil {
w.writer = v
}
return w.writer
}
func (w *atomicWriter) Swap(v Writer) Writer {
w.lock.Lock()
defer w.lock.Unlock()

Loading…
Cancel
Save