Skip to content

Commit 012c0c2

Browse files
Use internal channels
As feedback from a code review, use the struct channels as a way of self documenting the code. This makes the code more readable.
1 parent 0dc8bb3 commit 012c0c2

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

linux_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,12 @@ func TestCompressMaintainMode(t *testing.T) {
9898
isNil(err, t)
9999
f.Close()
100100

101-
notify := make(chan struct{})
102101
l := &Logger{
103102
Compress: true,
104103
Filename: filename,
105104
MaxBackups: 1,
106105
MaxSize: 100, // megabytes
107-
notifyCompressed: notify,
106+
notifyCompressed: make(chan struct{}),
108107
}
109108
defer l.Close()
110109
b := []byte("boo!")
@@ -117,7 +116,7 @@ func TestCompressMaintainMode(t *testing.T) {
117116
err = l.Rotate()
118117
isNil(err, t)
119118

120-
waitForNotify(notify, t)
119+
waitForNotify(l.notifyCompressed, t)
121120

122121
// a compressed version of the log file should now exist with the correct
123122
// mode.
@@ -148,13 +147,12 @@ func TestCompressMaintainOwner(t *testing.T) {
148147
isNil(err, t)
149148
f.Close()
150149

151-
notify := make(chan struct{})
152150
l := &Logger{
153151
Compress: true,
154152
Filename: filename,
155153
MaxBackups: 1,
156154
MaxSize: 100, // megabytes
157-
notifyCompressed: notify,
155+
notifyCompressed: make(chan struct{}),
158156
}
159157
defer l.Close()
160158
b := []byte("boo!")
@@ -167,7 +165,7 @@ func TestCompressMaintainOwner(t *testing.T) {
167165
err = l.Rotate()
168166
isNil(err, t)
169167

170-
waitForNotify(notify, t)
168+
waitForNotify(l.notifyCompressed, t)
171169

172170
// a compressed version of the log file should now exist with the correct
173171
// owner.

lumberjack.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,20 @@ func (l *Logger) Write(p []byte) (n int, err error) {
175175
func (l *Logger) Close() error {
176176
l.mu.Lock()
177177
defer l.mu.Unlock()
178-
if err := l.close(); err != nil {
179-
return err
180-
}
178+
179+
// Always close the mill channel, even if the close fails. This way we
180+
// guarantee that the mill goroutine will exit.
181+
err := l.close()
182+
181183
if l.millCh != nil {
182184
close(l.millCh)
183185
l.wg.Wait()
184186
l.millCh = nil
185187
l.wg = nil
186188
}
187-
return nil
189+
190+
// Return the result of the file close.
191+
return err
188192
}
189193

190194
// close closes the file if it is open.
@@ -404,8 +408,8 @@ func (l *Logger) millRunOnce() error {
404408

405409
// millRun runs in a goroutine to manage post-rotation compression and removal
406410
// of old log files.
407-
func (l *Logger) millRun(ch <-chan struct{}) {
408-
for range ch {
411+
func (l *Logger) millRun() {
412+
for range l.millCh {
409413
// what am I going to do, log this?
410414
_ = l.millRunOnce()
411415
}
@@ -420,7 +424,7 @@ func (l *Logger) mill() {
420424
l.wg = &sync.WaitGroup{}
421425
l.wg.Add(1)
422426
go func() {
423-
l.millRun(l.millCh)
427+
l.millRun()
424428
l.wg.Done()
425429
}()
426430
}

lumberjack_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,11 +773,12 @@ func exists(path string, t testing.TB) {
773773
}
774774

775775
func waitForNotify(notify <-chan struct{}, t testing.TB) {
776+
t.Helper()
777+
776778
select {
777779
case <-notify:
778780
// All good.
779781
case <-time.After(2 * time.Second):
780-
fmt.Println("logger notify not signalled")
781-
t.FailNow()
782+
t.Fatal("logger notify not signalled")
782783
}
783784
}

0 commit comments

Comments
 (0)