Skip to content

Commit

Permalink
Add API function to stop flushDaemon
Browse files Browse the repository at this point in the history
The new API function stops the daemons running goroutine. This way,
users can gracefully shut down klog and prevent goroutine leakage.
  • Loading branch information
katexochen committed Feb 10, 2022
1 parent b8f32b4 commit 397e3c9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
7 changes: 7 additions & 0 deletions klog.go
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,13 @@ func (f *flushDaemon) isRunning() bool {
return f.stopC != nil
}

// StopFlushDaemon stops the flush daemon, if running.
// This prevents klog from leaking goroutines on shutdown. After stopping
// the daemon, you can still manually flush buffers by calling Flush().
func StopFlushDaemon() {
logging.flushD.stop()
}

// lockAndFlushAll is like flushAll but locks l.mu first.
func (l *loggingT) lockAndFlushAll() {
l.mu.Lock()
Expand Down
13 changes: 13 additions & 0 deletions klog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2005,3 +2005,16 @@ func TestFlushDaemon(t *testing.T) {
}
}
}

func TestStopFlushDaemon(t *testing.T) {
logging.flushD.stop()
logging.flushD = newFlushDaemon(time.Second, func() {})
logging.flushD.run()
if !logging.flushD.isRunning() {
t.Error("expected flushD to be running")
}
StopFlushDaemon()
if logging.flushD.isRunning() {
t.Error("expected flushD to be stopped")
}
}

0 comments on commit 397e3c9

Please sign in to comment.