-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresty.go
More file actions
57 lines (45 loc) · 1.09 KB
/
resty.go
File metadata and controls
57 lines (45 loc) · 1.09 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
package restyclient
import (
"time"
syncOnce "github.com/admpub/once"
"github.com/admpub/resty/v2"
)
var (
// DefaultTimeout 默认超时时间
DefaultTimeout = 10 * time.Second
DefaultMaxRetryCount = 3
DefaultRedirectPolicy = resty.FlexibleRedirectPolicy(5)
classicClient *resty.Client
retryableClient *resty.Client
classicOnce syncOnce.Once
retryableOnce syncOnce.Once
)
func initClassic() {
classicClient = resty.New().
SetTimeout(DefaultTimeout).
SetRedirectPolicy(DefaultRedirectPolicy)
InitRestyHook(classicClient)
}
func ResetClassic() {
classicOnce.Reset()
}
func Classic() *resty.Request {
classicOnce.Do(initClassic)
return classicClient.R()
}
// - retryable -
func initRetryable() {
retryableClient = resty.New().
SetRetryCount(DefaultMaxRetryCount).
SetTimeout(DefaultTimeout).
SetRedirectPolicy(DefaultRedirectPolicy).
SetRetryAfter(RetryAfter)
InitRestyHook(retryableClient)
}
func ResetRetryable() {
retryableOnce.Reset()
}
func Retryable() *resty.Request {
retryableOnce.Do(initRetryable)
return retryableClient.R()
}