From d36a7f4e6ff7687f02b6ff0d0de4aae0685409b9 Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Mon, 24 Aug 2015 16:42:28 +0200 Subject: [PATCH] Fix busylooping in case of no target providers. merge() closes the channel that handleUpdates() reads from when there are zero configured target providers in the configuration. In that case, the for-select loop in handleUpdates() entered a busy loop. It should exit when the upstream channel is closed. --- retrieval/targetmanager.go | 7 +++++-- retrieval/targetmanager_test.go | 7 +++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/retrieval/targetmanager.go b/retrieval/targetmanager.go index 43763466a..c11efe98e 100644 --- a/retrieval/targetmanager.go +++ b/retrieval/targetmanager.go @@ -171,12 +171,15 @@ func (tm *TargetManager) Run() { tm.running = true } -// handleTargetUpdates receives target group updates and handles them in the +// handleUpdates receives target group updates and handles them in the // context of the given job config. func (tm *TargetManager) handleUpdates(ch <-chan targetGroupUpdate, done <-chan struct{}) { for { select { - case update := <-ch: + case update, ok := <-ch: + if !ok { + return + } if update.tg == nil { break } diff --git a/retrieval/targetmanager_test.go b/retrieval/targetmanager_test.go index b30bfdfef..54f9d9fc2 100644 --- a/retrieval/targetmanager_test.go +++ b/retrieval/targetmanager_test.go @@ -376,3 +376,10 @@ func TestTargetManagerConfigUpdate(t *testing.T) { } } } + +func TestHandleUpdatesReturnsWhenUpdateChanIsClosed(t *testing.T) { + tm := NewTargetManager(nopAppender{}) + ch := make(chan targetGroupUpdate) + close(ch) + tm.handleUpdates(ch, make(chan struct{})) +}