Skip to content

Commit 861d094

Browse files
authored
glog: don't hold mutex when sync'ing (#68)
Some environments are slow when syncing and holding the lock might cause contention. cl/621846576 (google-internal)
1 parent b256bbe commit 861d094

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

glog_file.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -369,23 +369,30 @@ func (s *fileSink) Flush() error {
369369

370370
// flush flushes all logs of severity threshold or greater.
371371
func (s *fileSink) flush(threshold logsink.Severity) error {
372-
s.mu.Lock()
373-
defer s.mu.Unlock()
374-
375372
var firstErr error
376373
updateErr := func(err error) {
377374
if err != nil && firstErr == nil {
378375
firstErr = err
379376
}
380377
}
381378

382-
// Flush from fatal down, in case there's trouble flushing.
383-
for sev := logsink.Fatal; sev >= threshold; sev-- {
384-
file := s.file[sev]
385-
if file != nil {
386-
updateErr(file.Flush())
387-
updateErr(file.Sync())
379+
// Remember where we flushed, so we can call sync without holding
380+
// the lock.
381+
var files []flushSyncWriter
382+
func() {
383+
s.mu.Lock()
384+
defer s.mu.Unlock()
385+
// Flush from fatal down, in case there's trouble flushing.
386+
for sev := logsink.Fatal; sev >= threshold; sev-- {
387+
if file := s.file[sev]; file != nil {
388+
updateErr(file.Flush())
389+
files = append(files, file)
390+
}
388391
}
392+
}()
393+
394+
for _, file := range files {
395+
updateErr(file.Sync())
389396
}
390397

391398
return firstErr

0 commit comments

Comments
 (0)