-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathretryer.go
More file actions
71 lines (60 loc) · 1.8 KB
/
Copy pathretryer.go
File metadata and controls
71 lines (60 loc) · 1.8 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
package retryhttp
import (
"errors"
"net"
"slices"
"strings"
"syscall"
)
//counterfeiter:generate . Retryer
type Retryer interface {
IsRetryable(err error) bool
}
type DefaultRetryer struct{}
func (r *DefaultRetryer) IsRetryable(err error) bool {
if err == nil {
return false
}
if netErr, ok := err.(net.Error); ok {
if netErr.Temporary() || netErr.Timeout() {
return true
}
}
// Check if the error is in our predefined list of retryable errors
var sysErr syscall.Errno
if errors.As(err, &sysErr) {
if slices.Contains(retryableSyscallErrors, sysErr) {
return true
}
}
// Fall back to string matching for other error types
errMsg := strings.ToLower(err.Error())
for _, msg := range retryableErrorMessages {
if strings.Contains(errMsg, msg) {
return true
}
}
return false
}
// Syscall error codes that should trigger a retry
var retryableSyscallErrors = []syscall.Errno{
syscall.ECONNREFUSED, // Connection refused - The server is not listening on the specified port or is unreachable
syscall.ECONNRESET, // Connection reset by peer - The server abruptly closed the connection without proper termination
syscall.ETIMEDOUT, // Operation timed out - The connection or request did not complete within the allotted time
syscall.EPIPE, // Broken pipe - Attempt to write to a socket that has been closed by the peer
}
// Error message substrings that should trigger a retry
var retryableErrorMessages = []string{
"i/o timeout",
"no such host",
"handshake failure",
"handshake timeout",
"timeout awaiting response headers",
"net/http: timeout awaiting response headers", // Specific to http package timeouts
"unexpected eof",
"connection reset by peer",
"read: connection reset by peer",
"read on closed response body",
"broken pipe",
"use of closed network connection",
}