From a72bd5cf65c4816e2749836bcf2a0491c0f90596 Mon Sep 17 00:00:00 2001 From: Drew Bailey <2614075+drewbailey@users.noreply.github.com> Date: Wed, 30 Oct 2019 09:02:29 -0400 Subject: [PATCH] use channel instead of empty string to determine close --- api/agent.go | 11 +++++------ command/agent_monitor.go | 4 ++-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/api/agent.go b/api/agent.go index 6e381e4ba..b7e5e5a10 100644 --- a/api/agent.go +++ b/api/agent.go @@ -255,26 +255,25 @@ func (a *Agent) Monitor(stopCh <-chan struct{}, q *QueryOptions) (chan string, e logCh := make(chan string, 64) go func() { defer resp.Body.Close() - + defer close(logCh) scanner := bufio.NewScanner(resp.Body) + + LOOP: for { select { case <-stopCh: return default: } + if scanner.Scan() { - // An empty string signals to the caller that - // the scan is done, so make sure we only emit - // that when the scanner says it's done, not if - // we happen to ingest an empty line. if text := scanner.Text(); text != "" { logCh <- text } else { logCh <- " " } } else { - logCh <- "" + break LOOP } } }() diff --git a/command/agent_monitor.go b/command/agent_monitor.go index 19e13abcb..9b42562cd 100644 --- a/command/agent_monitor.go +++ b/command/agent_monitor.go @@ -101,8 +101,8 @@ func (c *MonitorCommand) Run(args []string) int { OUTER: for { select { - case log := <-logCh: - if log == "" { + case log, ok := <-logCh: + if !ok { break OUTER } c.Ui.Output(log)