增加重试配置初始化 + 重试逻辑处理

This commit is contained in:
白茶清欢 2024-05-31 18:51:31 +08:00
parent 0ba0f7dced
commit 196e81191a
4 changed files with 44 additions and 3 deletions

View File

@ -88,9 +88,36 @@ func (hc *HttpClient) GetRestyClient() *resty.Client {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:52 2024/5/31
func (hc *HttpClient) Request(reqConfig *define.Request) *define.Response {
func (hc *HttpClient) Request() *define.Response {
hc.Client.OnBeforeRequest(hc.getRequestValidateMiddleware()) // 请求参数验证中间件必注册
hc.Client.OnAfterResponse(hc.getResponseValidateMiddleware()) // 响应验证中间件必注册
hc.request.Send()
var (
err error
)
response := &define.Response{
Header: map[string]string{},
Cookie: map[string]string{},
Body: map[string]any{},
Code: "",
Message: "",
Data: "",
HttpCode: 0,
HttpCodeStatus: "",
ResponseDataRule: nil,
Seq: 0,
RequestStartTime: 0,
FinishRequestTime: 0,
UsedTime: 0,
RestyResponse: nil,
IsSuccess: false,
RequestCount: 0,
FailInfoList: make([]any, 0),
}
for i := 0; i < hc.reqConfig.RetryRule.RetryCount+1; i++ {
if response.RestyResponse, err = hc.request.Send(); nil != err {
}
}
return nil
}

View File

@ -36,7 +36,7 @@ type Request struct {
//
// Date : 12:26 2024/5/31
type RequestRetryRule struct {
RetryCount int64 `json:"retry_count"` // 重试次数
RetryCount int `json:"retry_count"` // 重试次数
RetryTimeInterval int64 `json:"retry_time_interval"` // 重试的时间间隔 1 - 10 之间, 单位毫秒
RetryHttpCodeList []int64 `json:"retry_http_code_list"` // 哪些http状态码需要重试
RetryBusinessCodeList []string `json:"retry_business_code_list"` // 哪些业务状态码需要重试

View File

@ -7,6 +7,8 @@
// Date : 2024-05-31 12:34
package define
import "github.com/go-resty/resty/v2"
// Response 响应的数据结构定义
//
// Author : go_developer@163.com<白茶清欢>
@ -26,4 +28,8 @@ type Response struct {
RequestStartTime int64 `json:"request_start_time"` // 请求开始时间 : ms
FinishRequestTime int64 `json:"finish_request_time"` // 请求完成时间 : ms
UsedTime int64 `json:"used_time"` // 请求耗时 : ms
RestyResponse *resty.Response `json:"-"` // 请求返回
IsSuccess bool `json:"is_success"` // 是否请求成功
RequestCount int64 `json:"request_count"` // 请求次数
FailInfoList []any `json:"fail_info_list"` // 请求失败信息记录
}

View File

@ -38,6 +38,14 @@ type requestConfig struct {
//
// Date : 16:25 2024/5/31
func (rc *requestConfig) initDefaultConfig(reqConfig *define.Request) {
if nil == reqConfig.RetryRule {
reqConfig.RetryRule = &define.RequestRetryRule{
RetryCount: 0,
RetryTimeInterval: 0,
RetryHttpCodeList: make([]int64, 0),
RetryBusinessCodeList: make([]string, 0),
}
}
if nil == reqConfig.Header {
reqConfig.Header = map[string]string{}
}