Skip to content

Commit

Permalink
Fix side effects of sink.Close() refs #4 and #5
Browse files Browse the repository at this point in the history
Fixed in a straight way by removing logic with sink ID. The logic was
corrupted any way. Aman Gupta explained the problem in #5 and figured
out the cause with sink ID. In this change I suppose that a number of
sinks will not very large.
  • Loading branch information
grafov committed Nov 4, 2019
1 parent 66b49ee commit 967e6e6
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,13 @@ const (
var collector struct {
sync.RWMutex
sinks []*Sink
count int
}

type (
// Sink used for filtering incoming log records from all logger instances
// and decides how to filter them. Each output wraps its own io.Writer.
// Sink methods are safe for concurrent usage.
Sink struct {
id uint
In chan chain
close chan struct{}
writer io.Writer
Expand Down Expand Up @@ -108,9 +106,7 @@ func SinkTo(w io.Writer, fn Formatter) *Sink {
}
)
collector.Lock()
sink.id = uint(collector.count)
collector.sinks = append(collector.sinks, sink)
collector.count++
collector.Unlock()
go processSink(sink)
return sink
Expand Down Expand Up @@ -310,8 +306,14 @@ func (s *Sink) Close() {
atomic.StoreInt32(s.state, sinkClosed)
s.close <- struct{}{}
collector.Lock()
collector.count--
collector.sinks = append(collector.sinks[0:s.id], collector.sinks[s.id+1:]...)
for i, v := range collector.sinks {
if s == v {
collector.sinks[i] = collector.sinks[len(collector.sinks)-1]
collector.sinks[len(collector.sinks)-1] = nil
collector.sinks = collector.sinks[:len(collector.sinks)-1]
break
}
}
collector.Unlock()
}
}
Expand Down

0 comments on commit 967e6e6

Please sign in to comment.