From 3daa4b4cdd7a1374f2373a56f3f1e4afc9449939 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 15 Mar 2018 17:56:15 +0100 Subject: [PATCH] Fix stats collector spinning CPU if no stats are collected Commit fd0e24b7189374e0fe7c55b6d26ee916d3ee1655 changed the stats collection loop to use a `sleep()` instead of `time.Tick()` in the for-loop. This change caused a regression in situations where no stats are being collected, or an error is hit in the loop (in which case the loop would `continue`, and the `sleep()` is not hit). This patch puts the sleep at the start of the loop to guarantee it's always hit. This will delay the sampling, which is similar to the behavior before fd0e24b7189374e0fe7c55b6d26ee916d3ee1655. Signed-off-by: Sebastiaan van Stijn Upstream-commit: 481b8e54b45955e40075f49a9af321afce439320 Component: engine --- components/engine/daemon/stats/collector.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/components/engine/daemon/stats/collector.go b/components/engine/daemon/stats/collector.go index 6b7479dfd0..88e20984bc 100644 --- a/components/engine/daemon/stats/collector.go +++ b/components/engine/daemon/stats/collector.go @@ -91,6 +91,10 @@ func (s *Collector) Run() { var pairs []publishersPair for { + // Put sleep at the start so that it will always be hit, + // preventing a tight loop if no stats are collected. + time.Sleep(s.interval) + // it does not make sense in the first iteration, // but saves allocations in further iterations pairs = pairs[:0] @@ -141,8 +145,6 @@ func (s *Collector) Run() { logrus.Errorf("collecting stats for %s: %v", pair.container.ID, err) } } - - time.Sleep(s.interval) } }