Repository navigation
Expand file tree
/
Copy pathretry.go
More file actions
280 lines (268 loc) · 8.12 KB
/
Copy pathretry.go
File metadata and controls
280 lines (268 loc) · 8.12 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package clone
import (
"context"
"errors"
"os"
"strings"
"time"
)
const (
DefaultAttempts = 3
DefaultBaseDelay = 500 * time.Millisecond
DefaultMaxDelay = 4 * time.Second
)
// Notice describes a transient failure before the next attempt.
type Notice struct {
Label string
Attempt int
Attempts int
Delay time.Duration
}
// Command is one remote Git invocation plus operation-specific hooks.
type Command struct {
Label string
Dir string
Env []string
Args []string
// Reset runs after a failed attempt, before either another attempt or a
// terminal error return. It must only clean command-owned state.
Reset func() error
// Confirm may recognize that an operation succeeded despite an ambiguous
// transient error. A confirmation error does not replace the original Git
// error; the normal retry budget continues unless the context ended.
Confirm func(context.Context) (bool, error)
}
// Retry bounds how a remote Git invocation is retried. Its zero value uses
// the default policy. Fields are exposed so callers can use a tighter budget
// or deterministic runners and sleepers in tests.
type Retry struct {
Attempts int
BaseDelay time.Duration
MaxDelay time.Duration
Run Runner
Sleep func(context.Context, time.Duration) error
Notify func(Notice)
}
// Resolved fills zero-valued options with the defaults.
func (r Retry) Resolved() Retry {
if r.Attempts <= 0 {
r.Attempts = DefaultAttempts
}
if r.BaseDelay <= 0 {
r.BaseDelay = DefaultBaseDelay
}
if r.MaxDelay <= 0 {
r.MaxDelay = DefaultMaxDelay
}
if r.Run == nil {
r.Run = Run
}
if r.Sleep == nil {
r.Sleep = sleep
}
return r
}
// Do runs cmd, retrying only transient failures while the budget, context,
// cleanup hook, and optional success confirmation allow another attempt.
func (r Retry) Do(ctx context.Context, cmd Command) (string, error) {
policy := r.Resolved()
finishFailure := func(out string, err error) (string, error) {
if cmd.Reset == nil {
return out, err
}
if resetErr := cmd.Reset(); resetErr != nil {
return out, errors.Join(err, resetErr)
}
return out, err
}
for attempt := 1; ; attempt++ {
out, err := policy.Run(ctx, cmd.Dir, cmd.Env, cmd.Args...)
if err == nil {
return out, nil
}
if ctxErr := ctx.Err(); ctxErr != nil {
return finishFailure(out, ctxErr)
}
if !TransientFailure(out) {
return finishFailure(out, err)
}
if cmd.Confirm != nil {
confirmed, _ := cmd.Confirm(ctx)
if confirmed {
return out, nil
}
if ctxErr := ctx.Err(); ctxErr != nil {
return finishFailure(out, ctxErr)
}
}
if attempt >= policy.Attempts {
return finishFailure(out, err)
}
if cmd.Reset != nil {
if resetErr := cmd.Reset(); resetErr != nil {
return out, errors.Join(err, resetErr)
}
}
delay := backoffDelay(attempt, policy.BaseDelay, policy.MaxDelay)
if policy.Notify != nil {
policy.Notify(Notice{
Label: cmd.Label,
Attempt: attempt,
Attempts: policy.Attempts,
Delay: delay,
})
}
if err := policy.Sleep(ctx, delay); err != nil {
return out, err
}
}
}
// DestReset returns the cleanup to run after a failed clone attempt, or nil
// when there is nothing safe to clean. A clone that dies partway can leave
// the destination behind, and `git clone` refuses a non-empty target, so the
// cleanup is needed both before retries and before a terminal error return.
//
// Removal is offered only when dst is absent or empty at this point. Callers
// reach the clone path exactly when dst holds no .git, so an absent or empty
// dst can only ever gain content this call put there. A non-empty one belongs
// to the caller, and Git would reject it as a permanent error that is never
// retried anyway.
func DestReset(dst string) func() error {
entries, err := os.ReadDir(dst)
if err != nil && !os.IsNotExist(err) {
return nil
}
if len(entries) > 0 {
return nil
}
return func() error { return os.RemoveAll(dst) }
}
// permanentFailures are answers about the repository, the ref, the local
// destination, or the local machine. Repeating the command cannot change any
// of them, and retrying would only multiply pointless remote traffic.
//
// Several of these matter precisely because Git reports them alongside
// transport noise. A clone that runs the disk out of space ends with "fatal:
// write error: No space left on device" followed by "fatal: the remote end
// hung up unexpectedly", and a rejected credential can surface as "error: RPC
// failed; result=22, HTTP code = 401", both of which would otherwise be read
// as transient. Permanent markers are therefore checked first and win.
var permanentFailures = []string{
// The repository, the ref, or the credentials.
"repository not found",
"authentication failed",
"could not read username",
"could not read password",
"terminal prompts disabled",
"permission denied (publickey)",
"remote: permission denied",
"access denied",
"couldn't find remote ref",
"does not appear to be a git repository",
"unable to update url base from redirection",
"returned error: 401",
"returned error: 403",
"returned error: 404",
"returned error: 410",
"returned error: 413",
"http code = 401",
"http code = 403",
"http code = 404",
"http code = 413",
// A server-side hard limit, not a hiccup.
"pack exceeds maximum allowed size",
// The local destination and the local machine. These arrive wrapped in
// transport noise but no amount of retrying frees a disk or a thread.
"already exists and is not an empty directory",
"no space left on device",
"disk quota exceeded",
"input/output error",
"read-only file system",
"cannot allocate memory",
"unable to create thread",
"cannot fork",
"unable to fork",
}
// transientFailures are name-resolution, connection, TLS, and
// remote-availability failures. They say nothing about the repository or the
// ref, so the same command may well succeed a moment later.
var transientFailures = []string{
"could not resolve host",
"couldn't resolve host",
"could not resolve proxy",
"temporary failure in name resolution",
"failed to connect",
"connection refused",
"connection reset",
"connection timed out",
"operation timed out",
"timeout was reached",
"network is unreachable",
"no route to host",
"the remote end hung up unexpectedly",
"expected flush after ref listing",
"early eof",
"rpc failed",
"unexpected disconnect while reading sideband packet",
"transfer closed with",
"recv failure",
"send failure",
"empty reply from server",
"gnutls_handshake() failed",
"ssl connect error",
"ssl_read",
"ssl_write",
"tls connection was non-properly terminated",
"returned error: 408",
"returned error: 429",
"returned error: 500",
"returned error: 502",
"returned error: 503",
"returned error: 504",
// Cloudflare's origin-side range: 520 unknown, 521 down, 522/524 timeout,
// 523 unreachable. All say the edge could not reach the origin right now.
"returned error: 520",
"returned error: 521",
"returned error: 522",
"returned error: 523",
"returned error: 524",
"http code = 500",
"http code = 502",
"http code = 503",
"http code = 504",
"http code = 520",
"http code = 521",
"http code = 522",
"http code = 523",
"http code = 524",
"internal server error",
"bad gateway",
"service unavailable",
// Narrowed from "temporarily unavailable": nginx reports 503 as "Service
// Temporarily Unavailable", but a bare "Resource temporarily unavailable"
// is a local EAGAIN (see "unable to create thread" above) and must stay
// permanent.
"service temporarily unavailable",
"too many requests",
}
// TransientFailure reports whether Git's combined output describes a failure
// worth another attempt.
//
// The classification fails closed. A permanent marker wins over a transient
// one, and output matching nothing at all is treated as permanent, so an
// unfamiliar message keeps today's single-attempt behavior rather than
// turning into repeated remote traffic.
func TransientFailure(out string) bool {
lower := strings.ToLower(out)
for _, marker := range permanentFailures {
if strings.Contains(lower, marker) {
return false
}
}
for _, marker := range transientFailures {
if strings.Contains(lower, marker) {
return true
}
}
return false
}