-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathretry.go
87 lines (73 loc) · 1.6 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package controller
import (
"context"
"errors"
"io"
"net/http"
"time"
"github.com/moonrhythm/parapet-ingress-controller/proxy"
)
func retryMiddleware(h http.Handler) http.Handler {
const maxRetry = 5
canRequestRetry := func(r *http.Request) bool {
if r.Body == nil || r.Body == http.NoBody {
return true
}
if t, ok := r.Body.(*trackBodyRead); ok {
return !t.read
}
return false
}
tryServe := func(w http.ResponseWriter, r *http.Request) (ok bool) {
defer func() {
if e := recover(); e != nil {
err, _ := e.(error)
if errors.Is(err, context.Canceled) {
ok = true
return
}
if canRequestRetry(r) && proxy.IsRetryable(err) {
// retry
return
}
http.Error(w, "Bad Gateway", http.StatusBadGateway)
}
ok = true
}()
h.ServeHTTP(w, r)
return
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if r.Body != nil && r.Body != http.NoBody {
r.Body = &trackBodyRead{ReadCloser: r.Body}
}
for i := 0; i < maxRetry; i++ {
if tryServe(w, r) {
return
}
select {
case <-time.After(backoffDuration(i)):
case <-ctx.Done():
break
}
}
http.Error(w, "Bad Gateway", http.StatusBadGateway)
})
}
const maxBackoffDuration = 3 * time.Second
func backoffDuration(round int) (t time.Duration) {
t = time.Duration(1<<uint(round)) * 10 * time.Millisecond
if t > maxBackoffDuration {
t = maxBackoffDuration
}
return
}
type trackBodyRead struct {
io.ReadCloser
read bool
}
func (t *trackBodyRead) Read(p []byte) (n int, err error) {
t.read = true
return t.ReadCloser.Read(p)
}