Skip to content

Commit

Permalink
fixed goroutine leak
Browse files Browse the repository at this point in the history
  • Loading branch information
santihong committed Nov 3, 2017
1 parent aee4629 commit e72f7fe
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lumberjack.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ type Logger struct {
mu sync.Mutex

millCh chan bool
wg sync.WaitGroup
startMill sync.Once
}

Expand Down Expand Up @@ -165,7 +166,15 @@ func (l *Logger) Write(p []byte) (n int, err error) {
func (l *Logger) Close() error {
l.mu.Lock()
defer l.mu.Unlock()
return l.close()

err := l.close()

if l.millCh != nil {
close(l.millCh)
l.wg.Wait()
}
return err

}

// close closes the file if it is open.
Expand Down Expand Up @@ -376,6 +385,7 @@ func (l *Logger) millRunOnce() error {
// millRun runs in a goroutine to manage post-rotation compression and removal
// of old log files.
func (l *Logger) millRun() {
defer l.wg.Done()
for _ = range l.millCh {
// what am I going to do, log this?
_ = l.millRunOnce()
Expand All @@ -386,6 +396,7 @@ func (l *Logger) millRun() {
// starting the mill goroutine if necessary.
func (l *Logger) mill() {
l.startMill.Do(func() {
l.wg.Add(1)
l.millCh = make(chan bool, 1)
go l.millRun()
})
Expand Down

0 comments on commit e72f7fe

Please sign in to comment.