4 Star 17 Fork 27

少林码僧 / pkg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
retry.go 963 Bytes
一键复制 编辑 原始数据 按行查看 历史
phper95 提交于 2022-03-05 18:26 . httpclient
package httpclient
import (
"context"
"net/http"
"time"
)
const (
// DefaultRetryTimes 如果请求失败,最多重试3次
DefaultRetryTimes = 3
// DefaultRetryDelay 在重试前,延迟等待100毫秒
DefaultRetryDelay = time.Millisecond * 100
// _StatusReadRespErr read resp body err, should re-call doHTTP again.
_StatusReadRespErr = -204
// _StatusDoReqErr do req err, should re-call doHTTP again.
_StatusDoReqErr = -500
)
// RetryVerify Verify parse the body and verify that it is correct
type RetryVerify func(body []byte) (shouldRetry bool)
func shouldRetry(ctx context.Context, httpCode int) bool {
select {
case <-ctx.Done():
return false
default:
}
switch httpCode {
case
_StatusReadRespErr,
_StatusDoReqErr,
http.StatusRequestTimeout,
http.StatusLocked,
http.StatusTooEarly,
http.StatusTooManyRequests,
http.StatusServiceUnavailable,
http.StatusGatewayTimeout:
return true
default:
return false
}
}
Go
1
https://gitee.com/phper95/pkg.git
git@gitee.com:phper95/pkg.git
phper95
pkg
pkg
38d933c4cdfa

搜索帮助

53164aa7 5694891 3bd8fe86 5694891