增加分组并发请求的逻辑

This commit is contained in:
白茶清欢 2025-03-28 18:13:49 +08:00
parent d9da985da5
commit 94cbccc047
2 changed files with 25 additions and 12 deletions

View File

@ -85,16 +85,16 @@ type RequestConfigResultRule struct {
//
// Date : 14:40 2025/3/28
type Response struct {
FinalFailure bool `json:"final_failure"` // 是否最终失败
FailureApiAlias string `json:"failure_api_alias"` // 导致最终失败的接口别名
IsSuccess bool `json:"is_success"` // 请求是否成功
ErrorCode string `json:"error_code"` // 错误码
ErrorMessage string `json:"error_message"` // 错误信息
FailApiAlias []string `json:"fail_api_alias"` // 失败的接口别名
Raw []byte `json:"raw"` // 返回结果的raw数据(按照result_rule格式化后的结果)
DataMap map[string]any `json:"data_map"` // 返回结果的map格式(按照result_rule格式化后的结果)
AliasResultTable map[string]*Response `json:"alias_result_table"` // 每一个请求的返回结果 key: 请求别名 value: 请求返回数据
Lock *sync.RWMutex `json:"-"` // 数据锁, public, 外部拿到结果需要做一席并发读写操作, 可以直接复用这把锁
FinalFailure bool `json:"final_failure"` // 是否最终失败
FailureApiAlias string `json:"failure_api_alias"` // 导致最终失败的接口别名
IsSuccess bool `json:"is_success"` // 请求是否成功
ErrorCode string `json:"error_code"` // 错误码
ErrorMessage string `json:"error_message"` // 错误信息
FailApiAlias []string `json:"fail_api_alias"` // 失败的接口别名
Raw []byte `json:"raw"` // 返回结果的raw数据(按照result_rule格式化后的结果)
DataMap map[string]any `json:"data_map"` // 返回结果的map格式(按照result_rule格式化后的结果)
AliasResultTable map[string]*define.Response `json:"alias_result_table"` // 每一个请求的返回结果 key: 请求别名 value: 请求返回数据
Lock *sync.RWMutex `json:"-"` // 数据锁, public, 外部拿到结果需要做一席并发读写操作, 可以直接复用这把锁
}
const (

View File

@ -10,6 +10,7 @@ package mesh
import (
"context"
"git.zhangdeman.cn/zhangdeman/network/httpclient"
"git.zhangdeman.cn/zhangdeman/network/httpclient/define"
"sync"
)
@ -24,7 +25,7 @@ func Request(req *RequestConfig, receiver any) *Response {
ErrorMessage: "",
Raw: nil,
DataMap: nil,
AliasResultTable: make(map[string]*Response),
AliasResultTable: make(map[string]*define.Response),
Lock: &sync.RWMutex{},
},
reqCfg: req,
@ -67,6 +68,7 @@ func (c *client) doRequest(apiList []*RequestConfigGroupItem) bool {
)
// 初始化一下请求
c.initApiCfg(apiCfg)
// TODO: 构造生成请求参数
if httpClient, err = httpclient.NewHttpClient(apiCfg.RequestCfg, apiCfg.CacheInstance); nil != err {
// 此处获取客户端实例即发生异常, 忽略一切配置, 直接作为全局失败, 后续也不请求了
c.resp.ErrorCode = "-500"
@ -86,12 +88,23 @@ func (c *client) doRequest(apiList []*RequestConfigGroupItem) bool {
}
wg.Done()
}()
// TODO : 判断是否已经是最终失败
if c.resp.FinalFailure && !apiCfg.FinalFailureAllow {
// 已经最终失败, 并且最终失败后, 当前接口已经不允许请求, 不在进行请求
return
}
resp := httpClientList[clientIdx].Request()
c.resp.Lock.Lock()
defer c.resp.Lock.Unlock()
if !resp.IsSuccess {
// 判断是否已经是最终失败
if apiCfg.FailBehavior.FinalFailure || apiCfg.FailBehavior.Action == FailBehaviorError {
c.resp.FinalFailure = true
// 判断是否继续, 只能阻断后续分组请求,无法阻断当前租的请求
isContinue = FailBehaviorContinue == apiCfg.FailBehavior.Action
}
}
// 记录请求的信息
c.resp.AliasResultTable[apiCfg.Alias] = resp
}(idx, apiCfg)
}
wg.Wait()