-
Notifications
You must be signed in to change notification settings - Fork 3
/
retryable_error.go
103 lines (90 loc) · 1.9 KB
/
retryable_error.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
package retry
import (
"errors"
"io"
"net"
"net/url"
"strings"
)
type HTTPStatusCodeRetryable struct {
}
var retryErrorCodes = []int{
401, // Unauthorized
408, // Request Timeout
429, // Rate exceeded.
}
func (*HTTPStatusCodeRetryable) IsErrorRetryable(err error) bool {
var v interface{ HttpStatusCode() int }
if errors.As(err, &v) {
code := v.HttpStatusCode()
if code >= 500 {
return true
}
for _, e := range retryErrorCodes {
if code == e {
return true
}
}
}
return false
}
type ServiceErrorCodeRetryable struct {
}
var retryServiceErrorCodes = map[string]struct{}{
"RequestTimeTooSkewed": {},
"BadRequest": {},
}
func (*ServiceErrorCodeRetryable) IsErrorRetryable(err error) bool {
var v interface{ ErrorCode() string }
if errors.As(err, &v) {
if _, ok := retryServiceErrorCodes[v.ErrorCode()]; ok {
return true
}
}
return false
}
type ConnectionErrorRetryable struct{}
var retriableErrorStrings = []string{
"connection reset",
"connection refused",
"use of closed network connection",
"unexpected EOF reading trailer",
"transport connection broken",
"server closed idle connection",
"bad record MAC",
"stream error:",
"tls: use of closed connection",
"connection was forcibly closed",
"broken pipe",
"crc is inconsistent", // oss crc check error pattern
}
var retriableErrors = []error{
io.EOF,
io.ErrUnexpectedEOF,
}
func (c *ConnectionErrorRetryable) IsErrorRetryable(err error) bool {
if err != nil {
switch t := err.(type) {
case *url.Error:
if t.Err != nil {
return c.IsErrorRetryable(t.Err)
}
case net.Error:
if t.Temporary() || t.Timeout() {
return true
}
}
for _, retriableErr := range retriableErrors {
if err == retriableErr {
return true
}
}
errString := err.Error()
for _, phrase := range retriableErrorStrings {
if strings.Contains(errString, phrase) {
return true
}
}
}
return false
}