-
Notifications
You must be signed in to change notification settings - Fork 600
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
fixed goroutine leak #57
base: v2.0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,6 +112,7 @@ type Logger struct { | |
mu sync.Mutex | ||
|
||
millCh chan bool | ||
wg sync.WaitGroup | ||
startMill sync.Once | ||
} | ||
|
||
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. after waiting, you need to set millCh to nil, so if someone calls close again, it won't panic, trying to close the same channel. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, you are righ |
||
} | ||
return err | ||
|
||
} | ||
|
||
// close closes the file if it is open. | ||
|
@@ -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() | ||
|
@@ -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() | ||
}) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -623,7 +623,7 @@ func TestCompressOnRotate(t *testing.T) { | |
|
||
// we need to wait a little bit since the files get compressed on a different | ||
// goroutine. | ||
<-time.After(10 * time.Millisecond) | ||
<-time.After(20 * time.Millisecond) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you need to up these to make the tests pass? Just curious There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, I would got random failed if only 5 seconds |
||
|
||
// a compressed version of the log file should now exist and the original | ||
// should have been removed. | ||
|
@@ -672,7 +672,7 @@ func TestCompressOnResume(t *testing.T) { | |
|
||
// we need to wait a little bit since the files get compressed on a different | ||
// goroutine. | ||
<-time.After(10 * time.Millisecond) | ||
<-time.After(20 * time.Millisecond) | ||
|
||
// The write should have started the compression - a compressed version of | ||
// the log file should now exist and the original should have been removed. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be a pointer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why? it would not be passed by method, just one property of this struct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@natefinch could you reply this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jaczhao
https://golang.org/pkg/sync/#WaitGroup
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so where to init the : wg sync.WaitGroup
temporary I put it in file : lumberjack.go