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 11, 2024
1 parent ab53041 commit ccda4e7
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions klog.go
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,7 @@ func (l *loggingT) exit(err error) {
return
}
l.flushAll()
l.syncAll()
OsExit(2)
}

Expand Down Expand Up @@ -1225,24 +1226,36 @@ func (l *loggingT) lockAndFlushAll() {
l.mu.Lock()
l.flushAll()
l.mu.Unlock()
// Some environments are slow when syncing and holding the lock might cause contention.
l.syncAll()
}

// flushAll flushes all the logs and attempts to "sync" their data to disk.
// flushAll flushes all the logs
// l.mu is held.
func (l *loggingT) flushAll() {
// 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
}
}
if logging.loggerOptions.flush != nil {
logging.loggerOptions.flush()
}
}

// syncAll attempts to "sync" their data to disk.
func (l *loggingT) syncAll() {
// 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.Sync() // ignore error
}
}
}

// CopyStandardLogTo arranges for messages written to the Go "log" package's
// default logs to also appear in the Google logs for the named and lower
// severities. Subsequent changes to the standard log's default output location
Expand Down

0 comments on commit ccda4e7

Please sign in to comment.