mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-09-01 03:31:25 +02:00
fix: simplify non-blocking console logger flushing
Co-authored-by: jkroepke <1560587+jkroepke@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
ee0423e79a
commit
7ec2eee6d3
@@ -26,7 +26,6 @@ const defaultNonBlockingWriterBufferSize = 1024
|
||||
type nonBlockingWriter struct {
|
||||
writer io.Writer
|
||||
queue chan []byte
|
||||
stop chan struct{}
|
||||
workerDone chan struct{}
|
||||
mu sync.RWMutex
|
||||
closed bool
|
||||
@@ -40,27 +39,14 @@ func newNonBlockingWriter(writer io.Writer, queueSize int) *nonBlockingWriter {
|
||||
w := &nonBlockingWriter{
|
||||
writer: writer,
|
||||
queue: make(chan []byte, queueSize),
|
||||
stop: make(chan struct{}),
|
||||
workerDone: make(chan struct{}),
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer close(w.workerDone)
|
||||
|
||||
for {
|
||||
select {
|
||||
case p := <-w.queue:
|
||||
_, _ = w.writer.Write(p)
|
||||
case <-w.stop:
|
||||
for {
|
||||
select {
|
||||
case p := <-w.queue:
|
||||
_, _ = w.writer.Write(p)
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
for p := range w.queue {
|
||||
_, _ = w.writer.Write(p)
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -89,7 +75,7 @@ func (w *nonBlockingWriter) Close() error {
|
||||
w.mu.Lock()
|
||||
if !w.closed {
|
||||
w.closed = true
|
||||
close(w.stop)
|
||||
close(w.queue)
|
||||
}
|
||||
w.mu.Unlock()
|
||||
|
||||
|
||||
@@ -111,4 +111,6 @@ func TestNonBlockingWriterDropsInsteadOfBlocking(t *testing.T) {
|
||||
require.Len(t, writes, 2)
|
||||
require.Equal(t, []byte("first"), writes[0])
|
||||
require.Equal(t, []byte("second"), writes[1])
|
||||
require.NotEqual(t, []byte("third"), writes[0])
|
||||
require.NotEqual(t, []byte("third"), writes[1])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user