-
Notifications
You must be signed in to change notification settings - Fork 62
/
interceptor_retry_hosts.go
136 lines (113 loc) · 3.39 KB
/
interceptor_retry_hosts.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
package clientv2
import (
"net/http"
"net/url"
"strings"
"time"
"github.com/qiniu/go-sdk/v7/internal/hostprovider"
internal_io "github.com/qiniu/go-sdk/v7/internal/io"
"github.com/qiniu/go-sdk/v7/storagev2/retrier"
)
type HostsRetryConfig struct {
RetryMax int // 最大重试次数
ShouldRetry func(req *http.Request, resp *http.Response, err error) bool
Retrier retrier.Retrier // 重试器
HostFreezeDuration time.Duration // 主备域名冻结时间(默认:600s),当一个域名请求失败被冻结的时间,最小 time.Millisecond
HostProvider hostprovider.HostProvider // 备用域名获取方法
ShouldFreezeHost func(req *http.Request, resp *http.Response, err error) bool
}
func (c *HostsRetryConfig) init() {
if c.RetryMax < 0 {
c.RetryMax = 1
}
if c.HostFreezeDuration < time.Millisecond {
c.HostFreezeDuration = 600 * time.Second
}
if c.ShouldFreezeHost == nil {
c.ShouldFreezeHost = func(req *http.Request, resp *http.Response, err error) bool {
return true
}
}
}
func (c *HostsRetryConfig) getRetryDecision(req *http.Request, resp *http.Response, err error, attempts int) retrier.RetryDecision {
if c.ShouldRetry != nil {
if c.ShouldRetry(req, resp, err) {
return retrier.RetryRequest
} else {
return retrier.DontRetry
}
} else if c.Retrier != nil {
return c.Retrier.Retry(resp, err, &retrier.RetrierOptions{Attempts: attempts})
} else {
return errorRetrier.Retry(resp, err, &retrier.RetrierOptions{Attempts: attempts})
}
}
type hostsRetryInterceptor struct {
options HostsRetryConfig
}
func NewHostsRetryInterceptor(options HostsRetryConfig) Interceptor {
return &hostsRetryInterceptor{
options: options,
}
}
func (interceptor *hostsRetryInterceptor) Priority() InterceptorPriority {
return InterceptorPriorityRetryHosts
}
func (interceptor *hostsRetryInterceptor) Intercept(req *http.Request, handler Handler) (resp *http.Response, err error) {
if interceptor == nil || req == nil {
return handler(req)
}
interceptor.options.init()
// 不重试
if interceptor.options.RetryMax <= 0 {
return handler(req)
}
for i := 0; ; i++ {
// Clone 防止后面 Handler 处理对 req 有污染
reqBefore := cloneReq(req)
resp, err = handler(req)
if interceptor.options.getRetryDecision(reqBefore, resp, err, i) == retrier.DontRetry {
return resp, err
}
// 尝试冻结域名
oldHost := req.URL.Host
if interceptor.options.ShouldFreezeHost(req, resp, err) {
if fErr := interceptor.options.HostProvider.Freeze(oldHost, err, interceptor.options.HostFreezeDuration); fErr != nil {
break
}
}
if i >= interceptor.options.RetryMax {
break
}
// 尝试更换域名
newHost, pErr := interceptor.options.HostProvider.Provider()
if pErr != nil {
break
}
if index := strings.Index(newHost, "://"); index >= 0 {
newHost = newHost[(index + len("://")):]
}
if index := strings.Index(newHost, "/"); index >= 0 {
newHost = newHost[:index]
}
if len(newHost) == 0 {
break
}
if newHost != oldHost {
urlString := req.URL.String()
urlString = strings.Replace(urlString, oldHost, newHost, 1)
u, ppErr := url.Parse(urlString)
if ppErr != nil {
break
}
reqBefore.Host = u.Host
reqBefore.URL = u
}
req = reqBefore
if resp != nil && resp.Body != nil {
_ = internal_io.SinkAll(resp.Body)
resp.Body.Close()
}
}
return resp, err
}