From 8ccb770b00d3865bb4202f2f1ef7019d889e4261 Mon Sep 17 00:00:00 2001 From: Drew Bailey <2614075+drewbailey@users.noreply.github.com> Date: Tue, 5 Nov 2019 11:01:50 -0500 Subject: [PATCH] simplify logch goroutine --- command/agent/monitor/monitor.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/command/agent/monitor/monitor.go b/command/agent/monitor/monitor.go index d85719712..2c28a09f6 100644 --- a/command/agent/monitor/monitor.go +++ b/command/agent/monitor/monitor.go @@ -81,29 +81,32 @@ func (d *monitor) Start() <-chan []byte { d.logger.RegisterSink(d.sink) streamCh := make(chan []byte, d.bufSize) + + // run a go routine that listens for streamed + // log messages and sends them to streamCh go func() { - defer close(streamCh) + defer func() { + d.logger.DeregisterSink(d.sink) + close(streamCh) + }() + for { select { case log := <-d.logCh: select { case <-d.doneCh: - d.logger.DeregisterSink(d.sink) - close(d.logCh) return case streamCh <- log: } case <-d.doneCh: - d.Lock() - defer d.Unlock() - - d.logger.DeregisterSink(d.sink) - close(d.logCh) return } } }() + // run a go routine that periodically checks for + // dropped messages and makes room on the logCh + // to add a dropped message count warning go func() { // loop and check for dropped messages LOOP: