Skip to content

Commit 2b067ea

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 c81c014 commit 2b067ea

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
@@ -97,13 +97,12 @@ func TestCompressMaintainMode(t *testing.T) {
9797
isNil(err, t)
9898
f.Close()
9999

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

119-
waitForNotify(notify, t)
118+
waitForNotify(l.notifyCompressed, t)
120119

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

150-
notify := make(chan struct{})
151149
l := &Logger{
152150
Compress: true,
153151
Filename: filename,
154152
MaxBackups: 1,
155153
MaxSize: 100, // megabytes
156-
notifyCompressed: notify,
154+
notifyCompressed: make(chan struct{}),
157155
}
158156
defer l.Close()
159157
b := []byte("boo!")
@@ -166,7 +164,7 @@ func TestCompressMaintainOwner(t *testing.T) {
166164
err = l.Rotate()
167165
isNil(err, t)
168166

169-
waitForNotify(notify, t)
167+
waitForNotify(l.notifyCompressed, t)
170168

171169
// a compressed version of the log file should now exist with the correct
172170
// 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
@@ -817,11 +817,12 @@ func exists(path string, t testing.TB) {
817817
}
818818

819819
func waitForNotify(notify <-chan struct{}, t testing.TB) {
820+
t.Helper()
821+
820822
select {
821823
case <-notify:
822824
// All good.
823825
case <-time.After(2 * time.Second):
824-
fmt.Println("logger notify not signalled")
825-
t.FailNow()
826+
t.Fatal("logger notify not signalled")
826827
}
827828
}

0 commit comments

Comments
 (0)