-
Notifications
You must be signed in to change notification settings - Fork 12
/
dowithbackoff.options.go
88 lines (76 loc) · 3.27 KB
/
dowithbackoff.options.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
// Copyright 2022 The searKing Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package http
import (
"net/http"
time_ "github.com/searKing/golang/go/time"
)
// WithDoWithBackoffOptionChainUnaryInterceptor returns a DoWithBackoffOption that specifies the chained
// interceptor for http clients. The first interceptor will be the outer most,
// while the last interceptor will be the inner most wrapper around the real call.
// All interceptors added by this method will be chained, and the interceptor
// defined by WithClientInterceptor will always be prepended to the chain.
func WithDoWithBackoffOptionChainUnaryInterceptor(interceptors ...ClientInterceptor) DoWithBackoffOption {
return DoWithBackoffOptionFunc(func(o *doWithBackoff) {
o.ChainClientInterceptors = append(o.ChainClientInterceptors, interceptors...)
})
}
func WithDoWithBackoffOptionRetryAfter(f RetryAfterHandler) DoWithBackoffOption {
return DoWithBackoffOptionFunc(func(o *doWithBackoff) {
o.RetryAfter = f
})
}
func WithDoWithBackoffOptionDoRetryHandler(f DoRetryHandler) DoWithBackoffOption {
return DoWithBackoffOptionFunc(func(o *doWithBackoff) {
o.DoRetryHandler = f
})
}
func WithDoWithBackoffOptionExponentialBackOffOption(opts ...time_.ExponentialBackOffOption) DoWithBackoffOption {
return DoWithBackoffOptionFunc(func(o *doWithBackoff) {
o.ExponentialBackOffOption = append(o.ExponentialBackOffOption, opts...)
})
}
func WithDoWithBackoffOptionMaxRetries(retries int) DoWithBackoffOption {
return DoWithBackoffOptionFunc(func(o *doWithBackoff) {
o.ExponentialBackOffOption = append(o.ExponentialBackOffOption, time_.WithExponentialBackOffOptionMaxElapsedCount(retries))
})
}
func WithDoWithBackoffOptionGrpcBackOff(retries int) DoWithBackoffOption {
return DoWithBackoffOptionFunc(func(o *doWithBackoff) {
o.ExponentialBackOffOption = append(o.ExponentialBackOffOption, time_.WithExponentialBackOffOptionGRPC())
})
}
// WithDoWithBackoffOptionRoundTripper returns a DoWithBackoffOption.
func WithDoWithBackoffOptionRoundTripper(rt http.RoundTripper) DoWithBackoffOption {
return WithDoWithBackoffOptionDoRetryHandler(
func(req *http.Request, retry int) (*http.Response, error) {
return rt.RoundTrip(req)
})
}
// WithDoWithBackoffOptionTarget returns a DoWithBackoffOption.
// TargetUrl is proxy's url, like socks5://127.0.0.1:8080
// Host is proxy's addr, replace the HOST in TargetUrl if not empty
func WithDoWithBackoffOptionTarget(target string) DoWithBackoffOption {
if target == "" {
return EmptyDoWithBackoffOption{}
}
cli := NewClientWithTarget(target)
return WithDoWithBackoffOptionDoRetryHandler(
func(req *http.Request, retry int) (*http.Response, error) {
return cli.Do(req)
})
}
// WithDoWithBackoffOptionProxy returns a DoWithBackoffOption.
// TargetUrl is proxy's url, like socks5://127.0.0.1:8080
// Host is proxy's addr, replace the HOST in TargetUrl if not empty
func WithDoWithBackoffOptionProxy(proxyUrl string, proxyTarget string) DoWithBackoffOption {
if proxyUrl == "" {
return EmptyDoWithBackoffOption{}
}
cli := NewClientWithProxy(proxyUrl, proxyTarget)
return WithDoWithBackoffOptionDoRetryHandler(
func(req *http.Request, retry int) (*http.Response, error) {
return cli.Do(req)
})
}