feat: 卡网关按运营商账户限流并记录停复机调用
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:
邹进
2026-10-24 15:06:41 +09:00
parent 9e45097aa2
commit 42e881576d
106 changed files with 7173 additions and 48 deletions

View File

@@ -8,7 +8,9 @@ import (
"github.com/google/uuid"
"gorm.io/gorm"
"github.com/break/junhong_cmp_fiber/internal/gateway"
"github.com/break/junhong_cmp_fiber/internal/infrastructure/audit"
"github.com/break/junhong_cmp_fiber/internal/infrastructure/gatewaylimit"
"github.com/break/junhong_cmp_fiber/internal/infrastructure/integrationlog"
"github.com/break/junhong_cmp_fiber/internal/model"
assetAuditSvc "github.com/break/junhong_cmp_fiber/internal/service/asset_audit"
@@ -25,6 +27,9 @@ func (s *StopResumeService) appendCardCommandAudit(
beforeData, afterData map[string]any,
businessErr error,
) error {
if !commandAuditRequired(ctx, actionCode, result) {
return nil
}
if s.auditWriter == nil || card == nil || card.ID == 0 {
return errors.New(errors.CodeInvalidStatus, "IoT 卡停复机统一审计接缝未配置或资源不完整")
}
@@ -62,6 +67,9 @@ func (s *StopResumeService) recordCardCommandAudit(
beforeData, afterData map[string]any,
businessErr error,
) {
if !commandAuditRequired(ctx, actionCode, result) {
return
}
if s.db == nil || s.auditWriter == nil || card == nil || card.ID == 0 {
recordCardAuditSecondaryFailure(ctx, actionCode, cardID(card), businessErr,
errors.New(errors.CodeInvalidStatus, "IoT 卡停复机统一审计接缝未配置"))
@@ -74,6 +82,49 @@ func (s *StopResumeService) recordCardCommandAudit(
}
}
// legacyCommandLogging 判断本次停复机是否沿用统一审计与集成日志:
// 带限流豁免标记的入口(后台人工点击、通道阈值、自动续费)与账户、个人客户、OpenAPI 发起的操作沿用;
// 系统自动停复机(轮询、Outbox、激活、重置、退款等系统任务)只写停复机记录(add-carrier-gateway-rate-limit)。
// 通道阈值与自动续费的恢复扫描依赖集成日志标识,因此必须沿用。
func legacyCommandLogging(ctx context.Context) bool {
if gateway.RateLimitExemptSource(ctx) != "" {
return true
}
switch auditcontext.From(ctx).ActorKind {
case constants.AuditActorAccount, constants.AuditActorPersonalCustomer, constants.AuditActorOpenAPI:
return true
default:
return false
}
}
// commandAuditRequired 判断停复机命令结果是否写统一审计:系统自动停复机的成功、失败与结果未知改由停复机记录承载;
// 被拒绝(未调用上游)与非停复机命令的审计保持不变。
func commandAuditRequired(ctx context.Context, actionCode, result string) bool {
if legacyCommandLogging(ctx) || result == constants.AuditResultDenied {
return true
}
switch actionCode {
case constants.AuditActionIotCardAutoStopped, constants.AuditActionIotCardAutoStarted,
constants.AuditActionIotCardManualStopped, constants.AuditActionIotCardManualStarted,
constants.AuditActionIotCardOpenAPIStarted:
return false
default:
return true
}
}
// commandSource 返回停复机记录的来源:豁免来源优先,其次审计来源。
func commandSource(ctx context.Context) string {
if source := gateway.RateLimitExemptSource(ctx); source != "" {
return source
}
if source := auditcontext.From(ctx).Source; source != "" {
return source
}
return constants.AuditSourceWorker
}
func cardID(card *model.IotCard) uint {
if card == nil {
return 0
@@ -191,6 +242,10 @@ type cardCommandAttemptObserver struct {
operation string
scene string
seriesKey string
action string // constants.StopResumeActionStop / StopResumeActionStart
reason string // 停机原因(复机为空)
legacy bool // 是否沿用集成日志(见 legacyCommandLogging)
startedAt time.Time
nextAttempt int
current *gatewayAttempt
successful *gatewayAttempt
@@ -203,6 +258,13 @@ type cardCommandAttemptObserver struct {
func (o *cardCommandAttemptObserver) BeforeAttempt(ctx context.Context, _ int) error {
o.nextAttempt++
o.lastCallErr = nil
o.startedAt = time.Now()
if !o.legacy {
// 系统自动停复机不再写集成日志,只在尝试结束后写停复机记录。
o.current = nil
o.lastIntegrationID = ""
return nil
}
attempt, err := o.service.startCardCommandAttempt(ctx, o.card, o.operation, o.scene, o.seriesKey, o.nextAttempt)
if err != nil {
o.recordingErr = err
@@ -218,6 +280,10 @@ func (o *cardCommandAttemptObserver) AfterAttempt(ctx context.Context, _ int, ca
if isGatewayTimeout(callErr) {
o.unknown = true
}
o.service.recordStopResumeAttempt(ctx, o, callErr)
if !o.legacy {
return nil
}
if callErr == nil {
o.successful = o.current
o.current = nil
@@ -237,3 +303,15 @@ func (o *cardCommandAttemptObserver) auditResult(lastErr error) string {
}
return cardCommandAuditResult(lastErr)
}
// recordStopResumeAttempt 每次实际调用上游停机或复机后写一条停复机记录,写入失败不影响业务回写。
func (s *StopResumeService) recordStopResumeAttempt(ctx context.Context, o *cardCommandAttemptObserver, callErr error) {
if s.stopResumeRecorder == nil || o == nil || o.card == nil {
return
}
s.stopResumeRecorder.Record(ctx, gatewaylimit.StopResumeRecordInput{
CardID: o.card.ID, ICCID: o.card.ICCID, CarrierID: o.card.CarrierID, Action: o.action,
Source: commandSource(ctx), Scene: o.scene, Reason: o.reason, CommandID: o.seriesKey,
Attempt: o.nextAttempt, CallErr: callErr, Duration: time.Since(o.startedAt),
})
}

View File

@@ -16,6 +16,7 @@ import (
"github.com/break/junhong_cmp_fiber/internal/gateway"
"github.com/break/junhong_cmp_fiber/internal/infrastructure/audit"
"github.com/break/junhong_cmp_fiber/internal/infrastructure/gatewaylimit"
"github.com/break/junhong_cmp_fiber/internal/infrastructure/integrationlog"
"github.com/break/junhong_cmp_fiber/internal/model"
packagepkg "github.com/break/junhong_cmp_fiber/internal/service/package"
@@ -61,6 +62,13 @@ type StopResumeService struct {
retryInterval time.Duration
// channelThresholdGuard 是可选的通道阈值持锁判定能力;未注入时不做持锁拒绝。
channelThresholdGuard ChannelThresholdLockGuard
// stopResumeRecorder 写每次上游停复机调用的停复机记录;未注入时不写。
stopResumeRecorder *gatewaylimit.StopResumeRecorder
}
// SetStopResumeRecorder 注入停复机记录写入器。
func (s *StopResumeService) SetStopResumeRecorder(recorder *gatewaylimit.StopResumeRecorder) {
s.stopResumeRecorder = recorder
}
// SetObservationSeriesEventWriter 注入停复机成功观测序列 Outbox Writer。
@@ -717,8 +725,9 @@ func (s *StopResumeService) stopCardWithRetry(ctx context.Context, card *model.I
attemptObserver := &cardCommandAttemptObserver{
service: s, card: card, operation: constants.IntegrationOperationGatewayStopCard,
scene: constants.CardObservationSceneBusinessStop, seriesKey: seriesKey,
action: constants.StopResumeActionStop, reason: stopReason, legacy: legacyCommandLogging(ctx),
}
gatewayCtx := gateway.WithAttemptObserver(ctx, attemptObserver)
gatewayCtx := gateway.WithCarrier(gateway.WithAttemptObserver(ctx, attemptObserver), card.CarrierID)
var lastErr error
lastIntegrationID := ""
for i := 0; i < s.maxRetries; i++ {
@@ -730,6 +739,11 @@ func (s *StopResumeService) stopCardWithRetry(ctx context.Context, card *model.I
}
callErr := s.gatewayClient.StopCard(gatewayCtx, &gateway.CardOperationReq{CardNo: card.ICCID})
if gateway.IsRateLimited(callErr) {
// 被运营商账户限流拒绝时请求并未发出:不重试、不写失败审计,由调用方的既有重试或兜底路径稍后处理。
s.logger.Warn("停机被运营商账户网关限流,本次不调用上游", zap.Uint("card_id", card.ID), zap.String("iccid", card.ICCID))
return carrierthresholddomain.CommandOutcome{Result: constants.AuditResultFailed}, callErr
}
lastIntegrationID = attemptObserver.lastIntegrationID
if attemptObserver.recordingErr != nil {
s.logger.Error("记录停机 Integration Log 失败", zap.String("integration_id", lastIntegrationID), zap.Error(attemptObserver.recordingErr))
@@ -818,8 +832,9 @@ func (s *StopResumeService) resumeCardWithRetry(ctx context.Context, card *model
attemptObserver := &cardCommandAttemptObserver{
service: s, card: card, operation: constants.IntegrationOperationGatewayStartCard,
scene: constants.CardObservationSceneBusinessResume, seriesKey: seriesKey,
action: constants.StopResumeActionStart, legacy: legacyCommandLogging(ctx),
}
gatewayCtx := gateway.WithAttemptObserver(ctx, attemptObserver)
gatewayCtx := gateway.WithCarrier(gateway.WithAttemptObserver(ctx, attemptObserver), card.CarrierID)
var lastErr error
lastIntegrationID := ""
for i := 0; i < s.maxRetries; i++ {
@@ -835,6 +850,11 @@ func (s *StopResumeService) resumeCardWithRetry(ctx context.Context, card *model
req.Extend = constants.GatewayCardStartExtendMachineSeparated
}
callErr := s.gatewayClient.StartCard(gatewayCtx, req)
if gateway.IsRateLimited(callErr) {
// 被运营商账户限流拒绝时请求并未发出:不重试、不写失败审计,由调用方的既有重试或兜底路径稍后处理。
s.logger.Warn("复机被运营商账户网关限流,本次不调用上游", zap.Uint("card_id", card.ID), zap.String("iccid", card.ICCID))
return nil, "", callErr
}
lastIntegrationID = attemptObserver.lastIntegrationID
if attemptObserver.recordingErr != nil {
s.logger.Error("记录复机 Integration Log 失败", zap.String("integration_id", lastIntegrationID), zap.Error(attemptObserver.recordingErr))