This repository has been archived by the owner on Jun 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
retrier.go
184 lines (149 loc) · 3.56 KB
/
retrier.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package internal
import (
"context"
"io"
"net"
"time"
"github.com/cenkalti/backoff/v4"
"go.ytsaurus.tech/library/go/core/log"
"go.ytsaurus.tech/library/go/core/log/ctxlog"
"github.com/go-faster/errors"
"github.com/go-faster/yt/yt"
"github.com/go-faster/yt/yterrors"
)
type Retrier struct {
ProxySet *ProxySet
Config *yt.Config
Log log.Structured
}
type ReadRetryParams interface {
ReadRetryOptions() **yt.ReadRetryOptions
}
const (
CodeBalancerServiceUnavailable = 1000000
)
func isNetError(err error) bool {
var netErr net.Error
return errors.As(err, &netErr)
}
func (r *Retrier) shouldRetry(isRead bool, err error) bool {
var opErr *net.OpError
if errors.As(err, &opErr) && opErr.Op == "dial" {
var lookupErr *net.DNSError
if errors.As(err, &lookupErr) && lookupErr.IsNotFound {
return false
}
if tcp, ok := opErr.Addr.(*net.TCPAddr); ok && tcp.IP.IsLoopback() {
return false
}
return true
}
if isRead && isNetError(err) {
return true
}
if yterrors.ContainsErrorCode(err, CodeBalancerServiceUnavailable) {
return true
}
if yterrors.ContainsErrorCode(err, yterrors.CodeRetriableArchiveError) {
return true
}
if isProxyBannedError(err) {
return true
}
return false
}
func isProxyBannedError(err error) bool {
// COMPAT(babenko): drop ProxyBanned in favor of PeerBanned
if yterrors.ContainsErrorCode(err, yterrors.CodeProxyBanned) {
return true
}
if yterrors.ContainsErrorCode(err, yterrors.CodePeerBanned) {
return true
}
return false
}
func (r *Retrier) Intercept(ctx context.Context, call *Call, invoke CallInvoker) (res *CallResult, err error) {
var cancel func()
if timeout := r.Config.GetLightRequestTimeout(); timeout != 0 {
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}
for {
res, err = invoke(ctx, call)
if err == nil || call.DisableRetries {
return
}
_, isRead := call.Params.(ReadRetryParams)
if !r.shouldRetry(isRead, err) {
return
}
b := call.Backoff.NextBackOff()
if b == backoff.Stop {
return
}
if r.Log != nil {
ctxlog.Warn(ctx, r.Log.Logger(), "retrying light request",
log.String("call_id", call.CallID.String()),
log.Duration("backoff", b),
log.Error(err))
}
select {
case <-time.After(b):
case <-ctx.Done():
return nil, ctx.Err()
}
}
}
func (r *Retrier) Read(ctx context.Context, call *Call, invoke ReadInvoker) (rc io.ReadCloser, err error) {
for {
rc, err = invoke(ctx, call)
if err == nil || call.DisableRetries {
return
}
if !r.shouldRetry(true, err) {
return
}
b := call.Backoff.NextBackOff()
if b == backoff.Stop {
return
}
if r.Log != nil {
ctxlog.Warn(ctx, r.Log.Logger(), "retrying heavy read request",
log.String("call_id", call.CallID.String()),
log.Duration("backoff", b),
log.Error(err))
}
select {
case <-time.After(b):
case <-ctx.Done():
return nil, ctx.Err()
}
}
}
func (r *Retrier) Write(ctx context.Context, call *Call, invoke WriteInvoker) (w io.WriteCloser, err error) {
for {
w, err = invoke(ctx, call)
if err == nil || call.DisableRetries {
return
}
// We never actually sent any data to server. In is safe to retry this request like any other read.
if !r.shouldRetry(true, err) {
return
}
b := call.Backoff.NextBackOff()
if b == backoff.Stop {
return
}
if r.Log != nil {
ctxlog.Warn(ctx, r.Log.Logger(), "retrying heavy write request",
log.String("call_id", call.CallID.String()),
log.Duration("backoff", b),
log.Error(err))
}
select {
case <-time.After(b):
case <-ctx.Done():
return nil, ctx.Err()
}
}
}