1
// Copyright 2011, 2012, 2013 Canonical Ltd.
2
// Licensed under the LGPLv3, see LICENCE file for details.
10
// The Attempt and AttemptStrategy types are copied from those in launchpad.net/goamz/aws.
12
// AttemptStrategy represents a strategy for waiting for an action
13
// to complete successfully.
14
type AttemptStrategy struct {
15
Total time.Duration // total duration of attempt.
16
Delay time.Duration // interval between each try in the burst.
17
Min int // minimum number of retries; overrides Total
21
strategy AttemptStrategy
28
// Start begins a new sequence of attempts for the given strategy.
29
func (s AttemptStrategy) Start() *Attempt {
34
end: now.Add(s.Total),
39
// Next waits until it is time to perform the next attempt or returns
40
// false if it is time to stop trying.
41
// It always returns true the first time it is called - we are guaranteed to
42
// make at least one attempt.
43
func (a *Attempt) Next() bool {
45
sleep := a.nextSleep(now)
46
if !a.force && !now.Add(sleep).Before(a.end) && a.strategy.Min <= a.count {
50
if sleep > 0 && a.count > 0 {
59
func (a *Attempt) nextSleep(now time.Time) time.Duration {
60
sleep := a.strategy.Delay - now.Sub(a.last)
67
// HasNext returns whether another attempt will be made if the current
68
// one fails. If it returns true, the following call to Next is
69
// guaranteed to return true.
70
func (a *Attempt) HasNext() bool {
71
if a.force || a.strategy.Min > a.count {
75
if now.Add(a.nextSleep(now)).Before(a.end) {