From 325664766811cdac02735f97895fef6c4aa7034f Mon Sep 17 00:00:00 2001 From: "Timothy St. Clair" Date: Fri, 15 Jan 2016 13:54:26 -0600 Subject: [PATCH] Add jitter to housekeeping interval --- manager/container.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/manager/container.go b/manager/container.go index 07667218..b6706186 100644 --- a/manager/container.go +++ b/manager/container.go @@ -19,6 +19,7 @@ import ( "fmt" "io/ioutil" "math" + "math/rand" "os/exec" "path" "regexp" @@ -78,6 +79,17 @@ type containerData struct { collectorManager collector.CollectorManager } +// jitter returns a time.Duration between duration and duration + maxFactor * duration, +// to allow clients to avoid converging on periodic behavior. If maxFactor is 0.0, a +// suggested default value will be chosen. +func jitter(duration time.Duration, maxFactor float64) time.Duration { + if maxFactor <= 0.0 { + maxFactor = 1.0 + } + wait := duration + time.Duration(rand.Float64()*maxFactor*float64(duration)) + return wait +} + func (c *containerData) Start() error { go c.housekeeping() return nil @@ -356,7 +368,7 @@ func (self *containerData) nextHousekeeping(lastHousekeeping time.Time) time.Tim } } - return lastHousekeeping.Add(self.housekeepingInterval) + return lastHousekeeping.Add(jitter(self.housekeepingInterval, 1.0)) } // TODO(vmarmol): Implement stats collecting as a custom collector.