添加client自定义重试次数 (#151)

This commit is contained in:
多吃点苹果 2022-12-11 12:26:29 +08:00 committed by GitHub
parent a7ca046e03
commit 144b147606
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -43,6 +43,7 @@ type Client struct {
Domain WechatDomain
mode Mode
mu sync.Mutex
MaxRetryTimes int
cookies map[string][]*http.Cookie
}
@ -64,6 +65,7 @@ func NewClient() *Client {
func DefaultClient() *Client {
client := NewClient()
client.AddHttpHook(UserAgentHook{})
client.MaxRetryTimes = 5
return client
}
@ -71,8 +73,6 @@ func (c *Client) AddHttpHook(hooks ...HttpHook) {
c.HttpHooks = append(c.HttpHooks, hooks...)
}
const maxRetry = 2
func (c *Client) do(req *http.Request) (*http.Response, error) {
for _, hook := range c.HttpHooks {
hook.BeforeRequest(req)
@ -81,7 +81,7 @@ func (c *Client) do(req *http.Request) (*http.Response, error) {
resp *http.Response
err error
)
for i := 0; i < maxRetry; i++ {
for i := 0; i < c.MaxRetryTimes; i++ {
resp, err = c.Client.Do(req)
if err == nil {
break