mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-09-24 14:59:04 +02:00
fix: harden non-blocking console logger shutdown
Co-authored-by: jkroepke <1560587+jkroepke@users.noreply.github.com>
This commit is contained in:
co-authored by
jkroepke
parent
d9b8ff9dc0
commit
ee0423e79a
@@ -28,7 +28,8 @@ type nonBlockingWriter struct {
|
|||||||
queue chan []byte
|
queue chan []byte
|
||||||
stop chan struct{}
|
stop chan struct{}
|
||||||
workerDone chan struct{}
|
workerDone chan struct{}
|
||||||
once sync.Once
|
mu sync.RWMutex
|
||||||
|
closed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newNonBlockingWriter(writer io.Writer, queueSize int) *nonBlockingWriter {
|
func newNonBlockingWriter(writer io.Writer, queueSize int) *nonBlockingWriter {
|
||||||
@@ -69,9 +70,14 @@ func newNonBlockingWriter(writer io.Writer, queueSize int) *nonBlockingWriter {
|
|||||||
func (w *nonBlockingWriter) Write(p []byte) (int, error) {
|
func (w *nonBlockingWriter) Write(p []byte) (int, error) {
|
||||||
msg := bytes.Clone(p)
|
msg := bytes.Clone(p)
|
||||||
|
|
||||||
select {
|
w.mu.RLock()
|
||||||
case <-w.stop:
|
defer w.mu.RUnlock()
|
||||||
|
|
||||||
|
if w.closed {
|
||||||
return 0, io.ErrClosedPipe
|
return 0, io.ErrClosedPipe
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
case w.queue <- msg:
|
case w.queue <- msg:
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
@@ -80,9 +86,12 @@ func (w *nonBlockingWriter) Write(p []byte) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *nonBlockingWriter) Close() error {
|
func (w *nonBlockingWriter) Close() error {
|
||||||
w.once.Do(func() {
|
w.mu.Lock()
|
||||||
|
if !w.closed {
|
||||||
|
w.closed = true
|
||||||
close(w.stop)
|
close(w.stop)
|
||||||
})
|
}
|
||||||
|
w.mu.Unlock()
|
||||||
|
|
||||||
<-w.workerDone
|
<-w.workerDone
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user