Skip to content

Commit

Permalink
Do not acquire lock for file.Sync() fsync call
Browse files Browse the repository at this point in the history
  • Loading branch information
1978629634 committed Apr 8, 2024
1 parent ab53041 commit a3c1718
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion klog.go
Original file line number Diff line number Diff line change
Expand Up @@ -1230,17 +1230,28 @@ func (l *loggingT) lockAndFlushAll() {
// flushAll flushes all the logs and attempts to "sync" their data to disk.
// l.mu is held.
func (l *loggingT) flushAll() {
// Remember where we flushed, so we can call sync without holding
// the lock.
var files []flushSyncWriter

// Flush from fatal down, in case there's trouble flushing.
for s := severity.FatalLog; s >= severity.InfoLog; s-- {
file := l.file[s]
if file != nil {
_ = file.Flush() // ignore error
_ = file.Sync() // ignore error
files = append(files, file)
}
}
if logging.loggerOptions.flush != nil {
logging.loggerOptions.flush()
}

l.mu.Unlock()
// Some environments are slow when syncing and holding the lock might cause contention.
for _, file := range files {
_ = file.Sync() // ignore error
}
l.mu.Lock()
}

// CopyStandardLogTo arranges for messages written to the Go "log" package's
Expand Down

0 comments on commit a3c1718

Please sign in to comment.