mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 10:22:23 +00:00
log: fix data race in RotatingFileHandler
This commit is contained in:
parent
ff3a5d24d2
commit
20cb3e3549
1 changed files with 9 additions and 3 deletions
|
|
@ -77,6 +77,7 @@ func FileHandler(path string, fmtr Format) (Handler, error) {
|
|||
|
||||
// countingWriter wraps a WriteCloser object in order to count the written bytes.
|
||||
type countingWriter struct {
|
||||
sync.Mutex // lock
|
||||
w io.WriteCloser // the wrapped object
|
||||
count uint // number of bytes written
|
||||
}
|
||||
|
|
@ -157,9 +158,14 @@ func RotatingFileHandler(path string, limit uint, formatter Format) (Handler, er
|
|||
if counter == nil {
|
||||
counter = new(countingWriter)
|
||||
}
|
||||
h := StreamHandler(counter, formatter)
|
||||
h := LazyHandler(FuncHandler(func(r *Record) error {
|
||||
_, err := counter.Write(formatter.Format(r))
|
||||
return err
|
||||
}))
|
||||
|
||||
return FuncHandler(func(r *Record) error {
|
||||
counter.Lock()
|
||||
defer counter.Unlock()
|
||||
if counter.count > limit {
|
||||
counter.Close()
|
||||
counter.w = nil
|
||||
|
|
|
|||
Loading…
Reference in a new issue