From ce2bf8987c1a5acffc047c386942cd04a1a86459 Mon Sep 17 00:00:00 2001 From: K3A Date: Wed, 19 Jun 2019 23:48:54 +0000 Subject: [PATCH 1/2] prevent unnecessary CPU hog in infinite loop Using second return value of receive operator satisfies the select's case when `errors` channel is closed, making another unnecessary for loop iteration. --- manager.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/manager.go b/manager.go index ca2a729..b3b281f 100644 --- a/manager.go +++ b/manager.go @@ -123,11 +123,9 @@ func (a *AbstractManager) startMainLoop(preLoop func() error, receive func(r Rep select { case <-a.exit: return - case e, ok := <-errors: - if ok { - a.err = e - return - } + case e := <-errors: + a.err = e + return case r := <-a.rc: if a.consume(r, receive) { return From 91dd00c0f66606269382bef36e7d1c07481cd6a4 Mon Sep 17 00:00:00 2001 From: K3A Date: Thu, 20 Jun 2019 12:51:44 +0000 Subject: [PATCH 2/2] fix data race by not closing and niling error channel --- manager.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/manager.go b/manager.go index b3b281f..7d4c221 100644 --- a/manager.go +++ b/manager.go @@ -114,8 +114,6 @@ func (a *AbstractManager) startMainLoop(preLoop func() error, receive func(r Rep if err != nil { errors <- err } - close(errors) - errors = nil preLoopFinished <- true }()