重试规则支持配置指定 http code / business code 重试
This commit is contained in:
parent
04927ae811
commit
ecf2bd6b55
@ -37,6 +37,14 @@ func NewHttpClient(reqConfig *define.Request, cacheInstance cache.ICache) (*Http
|
||||
if err := validate.RequestConfig(reqConfig); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
if reqConfig.RetryRule == nil {
|
||||
reqConfig.RetryRule = &define.RequestRetryRule{
|
||||
RetryCount: 0,
|
||||
RetryTimeInterval: 0,
|
||||
RetryHttpCodeList: []int64{},
|
||||
RetryBusinessCodeList: []string{},
|
||||
} // 未指定重试规则, 则使用默认重试规则
|
||||
}
|
||||
if len(reqConfig.Static) > 0 {
|
||||
for loc, valMap := range reqConfig.Static {
|
||||
if len(valMap) == 0 {
|
||||
@ -276,6 +284,16 @@ func (hc *HttpClient) requestBackendApi() *define.Response {
|
||||
response.RequestFinishTime = 0 // 清空完成时间
|
||||
response.Seq++
|
||||
response.RequestCount++
|
||||
retryHttpCodeTable := make(map[int64]bool)
|
||||
retryBusinessCodeTable := make(map[string]bool)
|
||||
if nil != hc.reqConfig.RetryRule {
|
||||
for _, httpCode := range hc.reqConfig.RetryRule.RetryHttpCodeList {
|
||||
retryHttpCodeTable[httpCode] = true
|
||||
}
|
||||
for _, businessCode := range hc.reqConfig.RetryRule.RetryBusinessCodeList {
|
||||
retryBusinessCodeTable[businessCode] = true
|
||||
}
|
||||
}
|
||||
if response.RestyResponse, err = hc.request.Send(); nil != err {
|
||||
errType := define.RequestFailTypeSend
|
||||
if err.Error() == resty.ErrRateLimitExceeded.Error() {
|
||||
@ -293,6 +311,10 @@ func (hc *HttpClient) requestBackendApi() *define.Response {
|
||||
// 命中限流就不重试了
|
||||
break
|
||||
}
|
||||
if len(retryHttpCodeTable) > 0 && !retryHttpCodeTable[499] {
|
||||
// 未配置超时重试
|
||||
break
|
||||
}
|
||||
time.Sleep(time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond)
|
||||
continue
|
||||
}
|
||||
@ -309,7 +331,8 @@ func (hc *HttpClient) requestBackendApi() *define.Response {
|
||||
hc.fillResponseHeader(response)
|
||||
hc.fillResponseCookie(response)
|
||||
hc.fillResponseBody(response)
|
||||
if response.HttpCode != http.StatusOK {
|
||||
if response.HttpCode < http.StatusOK || response.HttpCode >= http.StatusMultipleChoices {
|
||||
// 非 2xx
|
||||
errType := define.RequestFailTypeServerError
|
||||
if response.HttpCode/100 == 4 {
|
||||
// 客户端错误
|
||||
@ -319,6 +342,10 @@ func (hc *HttpClient) requestBackendApi() *define.Response {
|
||||
Type: errType,
|
||||
Message: "http code is " + response.HttpCodeStatus + ", not success",
|
||||
}
|
||||
if len(retryHttpCodeTable) > 0 && !retryHttpCodeTable[int64(response.HttpCode)] {
|
||||
// 未配置http code重试
|
||||
break
|
||||
}
|
||||
time.Sleep(time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond)
|
||||
continue
|
||||
}
|
||||
@ -327,6 +354,10 @@ func (hc *HttpClient) requestBackendApi() *define.Response {
|
||||
Type: define.RequestFailTypeBusinessError,
|
||||
Message: "business code is " + response.Code + ", not success",
|
||||
}
|
||||
if len(retryBusinessCodeTable) > 0 && !retryBusinessCodeTable[response.Code] {
|
||||
// 未配置业务code重试
|
||||
break
|
||||
}
|
||||
time.Sleep(time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond)
|
||||
continue
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user