-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathretry.go
68 lines (53 loc) · 1.59 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package httpclient
import (
"math"
"net/http"
"time"
)
type noRetry struct {
}
var _ Retryable = (*noRetry)(nil)
func (r *noRetry) RetryError(err error) bool {
return false
}
func (r *noRetry) RetryResponse(resp *http.Response) bool {
return false
}
func (r *noRetry) RetryDelay(retry int) time.Duration {
return time.Duration(0)
}
func (r *noRetry) RetryMaxDuration() time.Duration {
return time.Second
}
// NewNoRetry will return a struct that implements Retryable but doesn't retry at all
func NewNoRetry() Retryable {
return &noRetry{}
}
type backoffRetry struct {
exponentFactor float64
initialTimeout float64
incrementingTimeout float64
maxTimeout float64
}
var _ Retryable = (*backoffRetry)(nil)
func (r *backoffRetry) RetryError(err error) bool {
return true
}
func (r *backoffRetry) RetryResponse(resp *http.Response) bool {
return true
}
func (r *backoffRetry) RetryDelay(retry int) time.Duration {
return time.Duration((r.initialTimeout + math.Pow(r.exponentFactor, float64(retry))) * r.incrementingTimeout)
}
func (r *backoffRetry) RetryMaxDuration() time.Duration {
return time.Duration(r.maxTimeout)
}
// NewBackoffRetry will return a Retryable that will support expotential backoff
func NewBackoffRetry(initialTimeout time.Duration, incrementingTimeout time.Duration, maxTimeout time.Duration, exponentFactor float64) Retryable {
return &backoffRetry{
exponentFactor: exponentFactor,
initialTimeout: float64(initialTimeout / time.Millisecond),
incrementingTimeout: float64(incrementingTimeout),
maxTimeout: float64(maxTimeout),
}
}