feat: 卡网关按运营商账户限流并记录停复机调用
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m33s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m33s
- 新增网关限流:按本系统运营商账户(卡 carrier_id)→ 限流组 → 操作限制每秒请求数,每次 HTTP 尝试计量,默认限流、仅后台人工点击/通道阈值/自动续费/手动刷新豁免;支持只统计与拦截两种模式,默认只统计 - Asynq 遇限流不计失败、固定短延迟重试;轮询任务与停复机业务重试遇限流短延迟重排 - 停机、复机不再使用网关内部重试,只发一次请求 - 网关返回结构化错误;新增上游错误分类规则表与测试接口 - 新增按月分区的停复机记录表:系统自动停复机只写记录,不再写统一审计与集成日志;普通轮询查询不再写集成日志 - 新增限流组、错误分类、停复机记录、按账户调用统计与枚举后台接口 - 迁移 000243~000246;同步 OpenSpec 提案(含后续 6 个 change)与技术方案、工程约束
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/bytedance/sonic"
|
||||
"go.uber.org/zap"
|
||||
@@ -36,6 +37,7 @@ type Client struct {
|
||||
timeout time.Duration
|
||||
logger *zap.Logger
|
||||
maxRetries int
|
||||
guard CallGuard // 卡网关调用守卫(限流与统计),为 nil 时不生效
|
||||
}
|
||||
|
||||
type attemptObserverKey struct{}
|
||||
@@ -138,6 +140,7 @@ func (c *Client) executeWithRetry(ctx context.Context, path, encryptedData strin
|
||||
// 带重试的 HTTP 请求
|
||||
var lastErr error
|
||||
observer, _ := ctx.Value(attemptObserverKey{}).(AttemptObserver)
|
||||
call, isCardCall := cardCallInfo(ctx)
|
||||
for attempt := 0; attempt <= c.maxRetries; attempt++ {
|
||||
if attempt > 0 {
|
||||
// 检查用户 Context 是否已取消
|
||||
@@ -158,12 +161,30 @@ func (c *Client) executeWithRetry(ctx context.Context, path, encryptedData strin
|
||||
}
|
||||
|
||||
attemptNumber := attempt + 1
|
||||
// 卡网关操作每次 HTTP 尝试前各取一次限流名额,内部重试同样计量;拿不到名额时本次调用立即结束。
|
||||
if isCardCall && c.guard != nil {
|
||||
call.Attempt = attemptNumber
|
||||
if err := c.guard.BeforeAttempt(ctx, call); err != nil {
|
||||
if attempt > 0 {
|
||||
c.logger.Warn("Gateway 重试被限流中止", zap.String("path", path), zap.Int("attempt", attemptNumber), zap.NamedError("last_error", lastErr))
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if observer != nil {
|
||||
if err := observer.BeforeAttempt(ctx, attemptNumber); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
attemptStartedAt := time.Now()
|
||||
result, retryable, err := c.executeHTTPRequest(ctx, path, encryptedData)
|
||||
attemptDuration := time.Since(attemptStartedAt)
|
||||
if gwErr, ok := AsGatewayError(err); ok {
|
||||
gwErr.Duration = attemptDuration
|
||||
}
|
||||
if isCardCall && c.guard != nil {
|
||||
c.guard.AfterAttempt(ctx, call, err, attemptDuration)
|
||||
}
|
||||
if observer != nil {
|
||||
if observeErr := observer.AfterAttempt(ctx, attemptNumber, err); observeErr != nil {
|
||||
return nil, observeErr
|
||||
@@ -238,30 +259,32 @@ func (c *Client) executeHTTPRequest(ctx context.Context, path string, encryptedD
|
||||
if err != nil {
|
||||
// 用户 Context 已取消 — 不可重试
|
||||
if ctx.Err() != nil {
|
||||
return nil, false, errors.Wrap(errors.CodeGatewayError, ctx.Err(), "请求被取消")
|
||||
return nil, false, newGatewayError(constants.GatewayErrorNetwork, errors.Wrap(errors.CodeGatewayError, ctx.Err(), "请求被取消"))
|
||||
}
|
||||
// Client 超时 — 可重试
|
||||
if reqCtx.Err() == context.DeadlineExceeded {
|
||||
return nil, true, errors.Wrap(errors.CodeGatewayTimeout, err, "Gateway 请求超时")
|
||||
return nil, true, newGatewayError(constants.GatewayErrorTimeout, errors.Wrap(errors.CodeGatewayTimeout, err, "Gateway 请求超时"))
|
||||
}
|
||||
// 其他网络错误(连接失败、DNS 解析等)— 可重试
|
||||
return nil, true, errors.Wrap(errors.CodeGatewayError, err, "发送 HTTP 请求失败")
|
||||
return nil, true, newGatewayError(constants.GatewayErrorNetwork, errors.Wrap(errors.CodeGatewayError, err, "发送 HTTP 请求失败"))
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// HTTP 状态码错误 — 不可重试
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, false, errors.New(errors.CodeGatewayError, fmt.Sprintf("HTTP 状态码异常: %d", resp.StatusCode))
|
||||
gwErr := newGatewayError(constants.GatewayErrorHTTP, errors.New(errors.CodeGatewayError, fmt.Sprintf("HTTP 状态码异常: %d", resp.StatusCode)))
|
||||
gwErr.HTTPStatus = resp.StatusCode
|
||||
return nil, false, gwErr
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, false, errors.Wrap(errors.CodeGatewayInvalidResp, err, "读取响应体失败")
|
||||
return nil, false, newGatewayError(constants.GatewayErrorInvalidResponse, errors.Wrap(errors.CodeGatewayInvalidResp, err, "读取响应体失败"))
|
||||
}
|
||||
|
||||
var gatewayResp GatewayResponse
|
||||
if err := sonic.Unmarshal(body, &gatewayResp); err != nil {
|
||||
return nil, false, errors.Wrap(errors.CodeGatewayInvalidResp, err, "解析 Gateway 响应失败")
|
||||
return nil, false, newGatewayError(constants.GatewayErrorInvalidResponse, errors.Wrap(errors.CodeGatewayInvalidResp, err, "解析 Gateway 响应失败"))
|
||||
}
|
||||
|
||||
// Gateway 业务错误 — 不可重试
|
||||
@@ -271,7 +294,10 @@ func (c *Client) executeHTTPRequest(ctx context.Context, path string, encryptedD
|
||||
zap.Int("gateway_code", gatewayResp.Code),
|
||||
zap.String("gateway_msg", gatewayResp.Msg),
|
||||
)
|
||||
return nil, false, errors.New(errors.CodeGatewayError, fmt.Sprintf("Gateway 业务错误: code=%d, msg=%s", gatewayResp.Code, gatewayResp.Msg))
|
||||
gwErr := newGatewayError(constants.GatewayErrorUpstream, errors.New(errors.CodeGatewayError, fmt.Sprintf("Gateway 业务错误: code=%d, msg=%s", gatewayResp.Code, gatewayResp.Msg)))
|
||||
gwErr.ProviderCode = gatewayResp.Code
|
||||
gwErr.ProviderMessage = truncateProviderMessage(gatewayResp.Msg)
|
||||
return nil, false, gwErr
|
||||
}
|
||||
|
||||
return gatewayResp.Data, false, nil
|
||||
|
||||
Reference in New Issue
Block a user