Merge pull request '优化mesh请求' (#4) from feature/upgrade_mesh_request into master

Reviewed-on: #4
This commit is contained in:
白茶清欢 2025-04-16 22:04:47 +08:00
commit 04927ae811
2 changed files with 6 additions and 9 deletions

View File

@ -48,8 +48,7 @@ type RequestConfigGroupItem struct {
//
// Date : 15:51 2025/3/28
type RequestConfigGroupItemFailBehavior struct {
Action string `json:"action"` // 请求失败的行为: continue: 继续执行下一个请求, break: 停止执行后续请求
FinalFailure bool `json:"final_failure"` // 是否作为最终失败: action = break时, 无论外部是什么值, 永远为true, 当 action = continue时, 该值由外部传入, 原因: 当前接口失败, 可能还需要调用一些后续接口做些逻辑.
Action string `json:"action"` // 请求失败的行为: continue: 继续执行下一个请求, error: 停止执行后续请求, 并且整体请求失败
}
// RequestConfigGroupItemParamRule 参数提取规则

View File

@ -173,9 +173,12 @@ func (c *client) doRequest(apiList []*RequestConfigGroupItem) bool {
c.resp.Lock.Lock()
defer c.resp.Lock.Unlock()
if !resp.IsSuccess {
// 哪些接口请求失败: 注意,接口请求失败不代表整体失败,接口可能针对失败配置规则 continue
c.resp.FailApiAlias = append(c.resp.FailApiAlias, apiCfg.Alias)
// 判断是否已经是最终失败
if apiCfg.FailBehavior.FinalFailure || apiCfg.FailBehavior.Action == FailBehaviorError {
if apiCfg.FailBehavior.Action == FailBehaviorError {
c.resp.FinalFailure = true
c.resp.FailureApiAlias = apiCfg.Alias // 导致最终请求失败的接口
// 判断是否继续, 只能阻断后续分组请求,无法阻断当前租的请求
isContinue = FailBehaviorContinue == apiCfg.FailBehavior.Action
}
@ -191,14 +194,9 @@ func (c *client) doRequest(apiList []*RequestConfigGroupItem) bool {
func (c *client) initApiCfg(apiCfg *RequestConfigGroupItem) {
if apiCfg.FailBehavior == nil {
apiCfg.FailBehavior = &RequestConfigGroupItemFailBehavior{
Action: FailBehaviorError, // 默认失败终止请求
FinalFailure: true, // 默认一旦失败,则争个整个失败
Action: FailBehaviorError, // 默认失败终止请求
}
}
if apiCfg.FailBehavior.Action == FailBehaviorError {
// 配置了请求失败则报错, 一定是导致结果最终失败
apiCfg.FailBehavior.FinalFailure = true
}
// 每一个请求有独立的context
apiCfg.RequestCfg.Ctx = context.WithValue(c.reqCfg.Ctx, "alias", apiCfg.Alias)
}