Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

glog: don't hold mutex when sync'ing #68

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 16 additions & 9 deletions glog_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,23 +369,30 @@ func (s *fileSink) Flush() error {

// flush flushes all logs of severity threshold or greater.
func (s *fileSink) flush(threshold logsink.Severity) error {
s.mu.Lock()
defer s.mu.Unlock()

var firstErr error
updateErr := func(err error) {
if err != nil && firstErr == nil {
firstErr = err
}
}

// Flush from fatal down, in case there's trouble flushing.
for sev := logsink.Fatal; sev >= threshold; sev-- {
file := s.file[sev]
if file != nil {
updateErr(file.Flush())
updateErr(file.Sync())
// Remember where we flushed, so we can call sync without holding
// the lock.
var files []flushSyncWriter
func() {
s.mu.Lock()
defer s.mu.Unlock()
// Flush from fatal down, in case there's trouble flushing.
for sev := logsink.Fatal; sev >= threshold; sev-- {
if file := s.file[sev]; file != nil {
updateErr(file.Flush())
files = append(files, file)
}
}
}()

for _, file := range files {
updateErr(file.Sync())
}

return firstErr
Expand Down