forked from viant/afsc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
116 lines (103 loc) · 2.35 KB
/
helper.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
package gs
import (
"github.com/viant/afs/base"
"google.golang.org/api/googleapi"
"net/http"
"net/url"
"strings"
"sync"
"time"
)
const notFound = "Not Found"
const storageClassFragment = "storageclass"
const encryptionFragment = "encryption"
const backendError = "backendError"
const backendErrorCode = 10
const connectionResetCode = 11
var retryErrors = make(map[int]int)
var mux = &sync.Mutex{}
//isRetryError returns true if backend error
func isRetryError(err error) bool {
if err == nil {
return false
}
if apiError, ok := err.(*googleapi.Error); ok {
if apiError.Code == http.StatusServiceUnavailable || apiError.Code == http.StatusGatewayTimeout {
mux.Lock()
retryErrors[apiError.Code]++
mux.Unlock()
return true
}
}
message := err.Error()
if strings.Contains(message, "connection reset") {
mux.Lock()
retryErrors[connectionResetCode]++
mux.Unlock()
return true
}
if strings.Contains(message, backendError) {
mux.Lock()
retryErrors[backendErrorCode]++
mux.Unlock()
return true
}
return false
}
func isBucketNotFound(err error) bool {
if err == nil {
return false
}
if apiError, ok := err.(*googleapi.Error); ok {
if apiError.Code == http.StatusNotFound && strings.Contains(apiError.Message, notFound) {
return true
}
}
return strings.Contains(strings.ToLower(err.Error()), notFound)
}
func isFallbackError(err error) bool {
if err == nil {
return false
}
if apiError, ok := err.(*googleapi.Error); ok {
if apiError.Code == http.StatusBadRequest {
return true
}
}
errorMessage := strings.ToLower(err.Error())
return strings.Contains(errorMessage, storageClassFragment) || strings.Contains(errorMessage, encryptionFragment)
}
func sleepBeforeRetry(retry *base.Retry) {
time.Sleep(retry.Pause())
}
//isRetryError returns true if not found
func isNotFound(err error) bool {
if err == nil {
return false
}
if apiError, ok := err.(*googleapi.Error); ok {
if apiError.Code == http.StatusNotFound {
return true
}
}
return strings.Contains(err.Error(), notFound)
}
func isProxyError(err error) bool {
if err == nil {
return false
}
_, ok := err.(*url.Error)
if ok {
return true
}
return strings.Contains(err.Error(), "proxy")
}
func GetRetryCodes(reset bool) map[int]int {
result := retryErrors
if reset {
mux.Lock()
retryErrors = make(map[int]int)
mux.Unlock()
}
return result
}