req
req copied to clipboard
使用问题,重试次数达到上限后没有返回error
webHttpClient := req.NewClient()
webHttpClient.
ImpersonateChrome().
SetCookieJar(nil).
SetTimeout(10*time.Second).
DisableKeepAlives().
SetCommonRetryCount(3). // 最大重试3次
SetCommonRetryBackoffInterval(1*time.Second, 4*time.Second).
SetCommonRetryCondition(func(resp *req.Response, err error) bool {
if resp.GetStatusCode() == 202 || resp.IsErrorState() && resp.GetStatusCode() != 404 {
logger.Debug("遇到错误了,需要等待重试", zap.String("httpStatus", resp.GetStatus()), zap.String("link", resp.Request.RawURL))
return true
}
return false
}).
SetCommonRetryHook(func(resp *req.Response, err error) {
c := webHttpClient.Clone().SetProxyURL(setting.GetConfig().Proxy.URL) // Create a client with proxy
resp.Request.SetClient(c) // Change the client of request dynamically.
})
如上,设置了重试最多3次,以及什么时候需要重试。 发现在重试次数达到3次,且返回403的时候,最后还是能拿到结果。期望是重试达到最大次数但还是符合重试条件的时候要返回error。 有什么选项可以设置直接让客户端抛出错误?目前是还是要在最后兜底判断一次,比较麻烦且容易遗漏。
可以用 client 中间件,OnAfterResponse 里判断下,如果 status 不是 SuccessState,统一都抛成 error。参考 : https://req.cool/zh/docs/examples/handle-exceptions-with-middleware/
感谢 这样看似乎可以自定义一个isSuccessState的判断函数 然后在是否需要重试和OnAfterResponse里复用?