2023-06-16 20:29:50 +00:00
|
|
|
package retry
|
|
|
|
|
|
|
|
import "time"
|
|
|
|
|
2023-08-17 13:41:29 +00:00
|
|
|
// ThirtySeconds repeats an operation for thirty seconds and waits 500ms in between.
|
|
|
|
// Best for known slower operations like waiting on eventually consistent state.
|
|
|
|
func ThirtySeconds() *Timer {
|
|
|
|
return &Timer{Timeout: 30 * time.Second, Wait: 500 * time.Millisecond}
|
|
|
|
}
|
|
|
|
|
2023-06-16 20:29:50 +00:00
|
|
|
// TwoSeconds repeats an operation for two seconds and waits 25ms in between.
|
|
|
|
func TwoSeconds() *Timer {
|
|
|
|
return &Timer{Timeout: 2 * time.Second, Wait: 25 * time.Millisecond}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ThreeTimes repeats an operation three times and waits 25ms in between.
|
|
|
|
func ThreeTimes() *Counter {
|
|
|
|
return &Counter{Count: 3, Wait: 25 * time.Millisecond}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Timer repeats an operation for a given amount
|
|
|
|
// of time and waits between subsequent operations.
|
|
|
|
type Timer struct {
|
|
|
|
Timeout time.Duration
|
|
|
|
Wait time.Duration
|
|
|
|
|
|
|
|
// stop is the timeout deadline.
|
|
|
|
// TODO: Next()?
|
|
|
|
// Set on the first invocation of Next().
|
|
|
|
stop time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Timer) Continue() bool {
|
|
|
|
if r.stop.IsZero() {
|
|
|
|
r.stop = time.Now().Add(r.Timeout)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if time.Now().After(r.stop) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
time.Sleep(r.Wait)
|
|
|
|
return true
|
|
|
|
}
|