增加分组并发请求的逻辑
This commit is contained in:
parent
d9da985da5
commit
94cbccc047
@ -93,7 +93,7 @@ type Response struct {
|
|||||||
FailApiAlias []string `json:"fail_api_alias"` // 失败的接口别名
|
FailApiAlias []string `json:"fail_api_alias"` // 失败的接口别名
|
||||||
Raw []byte `json:"raw"` // 返回结果的raw数据(按照result_rule格式化后的结果)
|
Raw []byte `json:"raw"` // 返回结果的raw数据(按照result_rule格式化后的结果)
|
||||||
DataMap map[string]any `json:"data_map"` // 返回结果的map格式(按照result_rule格式化后的结果)
|
DataMap map[string]any `json:"data_map"` // 返回结果的map格式(按照result_rule格式化后的结果)
|
||||||
AliasResultTable map[string]*Response `json:"alias_result_table"` // 每一个请求的返回结果 key: 请求别名 value: 请求返回数据
|
AliasResultTable map[string]*define.Response `json:"alias_result_table"` // 每一个请求的返回结果 key: 请求别名 value: 请求返回数据
|
||||||
Lock *sync.RWMutex `json:"-"` // 数据锁, public, 外部拿到结果需要做一席并发读写操作, 可以直接复用这把锁
|
Lock *sync.RWMutex `json:"-"` // 数据锁, public, 外部拿到结果需要做一席并发读写操作, 可以直接复用这把锁
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@ package mesh
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"git.zhangdeman.cn/zhangdeman/network/httpclient"
|
"git.zhangdeman.cn/zhangdeman/network/httpclient"
|
||||||
|
"git.zhangdeman.cn/zhangdeman/network/httpclient/define"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -24,7 +25,7 @@ func Request(req *RequestConfig, receiver any) *Response {
|
|||||||
ErrorMessage: "",
|
ErrorMessage: "",
|
||||||
Raw: nil,
|
Raw: nil,
|
||||||
DataMap: nil,
|
DataMap: nil,
|
||||||
AliasResultTable: make(map[string]*Response),
|
AliasResultTable: make(map[string]*define.Response),
|
||||||
Lock: &sync.RWMutex{},
|
Lock: &sync.RWMutex{},
|
||||||
},
|
},
|
||||||
reqCfg: req,
|
reqCfg: req,
|
||||||
@ -67,6 +68,7 @@ func (c *client) doRequest(apiList []*RequestConfigGroupItem) bool {
|
|||||||
)
|
)
|
||||||
// 初始化一下请求
|
// 初始化一下请求
|
||||||
c.initApiCfg(apiCfg)
|
c.initApiCfg(apiCfg)
|
||||||
|
// TODO: 构造生成请求参数
|
||||||
if httpClient, err = httpclient.NewHttpClient(apiCfg.RequestCfg, apiCfg.CacheInstance); nil != err {
|
if httpClient, err = httpclient.NewHttpClient(apiCfg.RequestCfg, apiCfg.CacheInstance); nil != err {
|
||||||
// 此处获取客户端实例即发生异常, 忽略一切配置, 直接作为全局失败, 后续也不请求了
|
// 此处获取客户端实例即发生异常, 忽略一切配置, 直接作为全局失败, 后续也不请求了
|
||||||
c.resp.ErrorCode = "-500"
|
c.resp.ErrorCode = "-500"
|
||||||
@ -86,12 +88,23 @@ func (c *client) doRequest(apiList []*RequestConfigGroupItem) bool {
|
|||||||
}
|
}
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
// TODO : 判断是否已经是最终失败
|
if c.resp.FinalFailure && !apiCfg.FinalFailureAllow {
|
||||||
|
// 已经最终失败, 并且最终失败后, 当前接口已经不允许请求, 不在进行请求
|
||||||
|
return
|
||||||
|
}
|
||||||
resp := httpClientList[clientIdx].Request()
|
resp := httpClientList[clientIdx].Request()
|
||||||
c.resp.Lock.Lock()
|
c.resp.Lock.Lock()
|
||||||
defer c.resp.Lock.Unlock()
|
defer c.resp.Lock.Unlock()
|
||||||
if !resp.IsSuccess {
|
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)
|
}(idx, apiCfg)
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user