-
Notifications
You must be signed in to change notification settings - Fork 214
/
retry.go
51 lines (40 loc) · 1.11 KB
/
retry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package heimdall
import "time"
// Retriable defines contract for retriers to implement
type Retriable interface {
NextInterval(retry int) time.Duration
}
// RetriableFunc is an adapter to allow the use of ordinary functions
// as a Retriable
type RetriableFunc func(retry int) time.Duration
// NextInterval calls f(retry)
func (f RetriableFunc) NextInterval(retry int) time.Duration {
return f(retry)
}
type retrier struct {
backoff Backoff
}
// NewRetrier returns retrier with some backoff strategy
func NewRetrier(backoff Backoff) Retriable {
return &retrier{
backoff: backoff,
}
}
// NewRetrierFunc returns a retrier with a retry function defined
func NewRetrierFunc(f RetriableFunc) Retriable {
return f
}
// NextInterval returns next retriable time
func (r *retrier) NextInterval(retry int) time.Duration {
return r.backoff.Next(retry)
}
type noRetrier struct {
}
// NewNoRetrier returns a null object for retriable
func NewNoRetrier() Retriable {
return &noRetrier{}
}
// NextInterval returns next retriable time, always 0
func (r *noRetrier) NextInterval(retry int) time.Duration {
return 0 * time.Millisecond
}