添加client自定义重试次数 (#151)
This commit is contained in:
parent
a7ca046e03
commit
144b147606
14
client.go
14
client.go
@ -40,10 +40,11 @@ func (u UserAgentHook) AfterRequest(response *http.Response, err error) {}
|
|||||||
type Client struct {
|
type Client struct {
|
||||||
HttpHooks HttpHooks
|
HttpHooks HttpHooks
|
||||||
*http.Client
|
*http.Client
|
||||||
Domain WechatDomain
|
Domain WechatDomain
|
||||||
mode Mode
|
mode Mode
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
cookies map[string][]*http.Cookie
|
MaxRetryTimes int
|
||||||
|
cookies map[string][]*http.Cookie
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient() *Client {
|
func NewClient() *Client {
|
||||||
@ -64,6 +65,7 @@ func NewClient() *Client {
|
|||||||
func DefaultClient() *Client {
|
func DefaultClient() *Client {
|
||||||
client := NewClient()
|
client := NewClient()
|
||||||
client.AddHttpHook(UserAgentHook{})
|
client.AddHttpHook(UserAgentHook{})
|
||||||
|
client.MaxRetryTimes = 5
|
||||||
return client
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,8 +73,6 @@ func (c *Client) AddHttpHook(hooks ...HttpHook) {
|
|||||||
c.HttpHooks = append(c.HttpHooks, hooks...)
|
c.HttpHooks = append(c.HttpHooks, hooks...)
|
||||||
}
|
}
|
||||||
|
|
||||||
const maxRetry = 2
|
|
||||||
|
|
||||||
func (c *Client) do(req *http.Request) (*http.Response, error) {
|
func (c *Client) do(req *http.Request) (*http.Response, error) {
|
||||||
for _, hook := range c.HttpHooks {
|
for _, hook := range c.HttpHooks {
|
||||||
hook.BeforeRequest(req)
|
hook.BeforeRequest(req)
|
||||||
@ -81,7 +81,7 @@ func (c *Client) do(req *http.Request) (*http.Response, error) {
|
|||||||
resp *http.Response
|
resp *http.Response
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
for i := 0; i < maxRetry; i++ {
|
for i := 0; i < c.MaxRetryTimes; i++ {
|
||||||
resp, err = c.Client.Do(req)
|
resp, err = c.Client.Do(req)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
break
|
break
|
||||||
|
Loading…
x
Reference in New Issue
Block a user