Merge pull request #2262 from WIZARD-CXY/master
Fixes go race bug in startup log buffering.
This commit is contained in:
commit
41b0115ee6
|
@ -29,15 +29,28 @@ func (w *GatedWriter) Flush() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *GatedWriter) Write(p []byte) (n int, err error) {
|
func (w *GatedWriter) Write(p []byte) (n int, err error) {
|
||||||
w.lock.RLock()
|
// Once we flush we no longer synchronize writers since there's
|
||||||
defer w.lock.RUnlock()
|
// no use of the internal buffer. This is the happy path.
|
||||||
|
w.lock.RLock()
|
||||||
|
if w.flush {
|
||||||
|
w.lock.RUnlock()
|
||||||
|
return w.Writer.Write(p)
|
||||||
|
}
|
||||||
|
w.lock.RUnlock()
|
||||||
|
|
||||||
if w.flush {
|
// Now take the write lock.
|
||||||
return w.Writer.Write(p)
|
w.lock.Lock()
|
||||||
}
|
defer w.lock.Unlock()
|
||||||
|
|
||||||
p2 := make([]byte, len(p))
|
// Things could have changed between the locking operations, so we
|
||||||
copy(p2, p)
|
// have to check one more time.
|
||||||
w.buf = append(w.buf, p2)
|
if w.flush {
|
||||||
return len(p), nil
|
return w.Writer.Write(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Buffer up the written data.
|
||||||
|
p2 := make([]byte, len(p))
|
||||||
|
copy(p2, p)
|
||||||
|
w.buf = append(w.buf, p2)
|
||||||
|
return len(p), nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue