github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/uniter/timer.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package uniter 5 6 import ( 7 "math/rand" 8 "time" 9 10 "github.com/juju/juju/worker/uniter/remotestate" 11 ) 12 13 type waitDuration time.Duration 14 15 func (w waitDuration) After() <-chan time.Time { 16 // TODO(fwereade): 2016-03-17 lp:1558657 17 return time.After(time.Duration(w)) 18 } 19 20 // NewUpdateStatusTimer returns a func returning timed signal suitable for update-status hook. 21 func NewUpdateStatusTimer() remotestate.UpdateStatusTimerFunc { 22 r := rand.New(rand.NewSource(time.Now().Unix())) 23 return func(wait time.Duration) remotestate.Waiter { 24 // Actual time to wait is randomised to be +/-20% 25 // of the nominal value. 26 lower := 0.8 * float64(wait) 27 window := 0.4 * float64(wait) 28 offset := float64(r.Int63n(int64(window))) 29 wait = time.Duration(lower + offset) 30 31 return waitDuration(wait) 32 } 33 }