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

@@ -0,0 +1,53 @@
package gatewaylimit
import (
"context"
"strconv"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
"github.com/break/junhong_cmp_fiber/pkg/constants"
)
// CarrierResolver 按 ICCID 解析卡所属的本系统运营商账户(tb_iot_card.carrier_id),结果缓存在 Redis。
type CarrierResolver struct {
db *gorm.DB
redis *redis.Client
}
// NewCarrierResolver 创建运营商账户解析器。
func NewCarrierResolver(db *gorm.DB, redisClient *redis.Client) *CarrierResolver {
return &CarrierResolver{db: db, redis: redisClient}
}
// Resolve 返回 ICCID 对应卡的运营商账户 ID;卡不存在时返回 0、nil,由调用方按未知账户处理。
func (r *CarrierResolver) Resolve(ctx context.Context, iccid string) (uint, error) {
if r == nil || iccid == "" {
return 0, nil
}
cacheKey := constants.RedisGatewayCardCarrierKey(iccid)
if r.redis != nil {
if raw, err := r.redis.Get(ctx, cacheKey).Result(); err == nil {
if value, parseErr := strconv.ParseUint(raw, 10, 64); parseErr == nil {
return uint(value), nil
}
}
}
if r.db == nil {
return 0, nil
}
var carrierIDs []uint
err := r.db.WithContext(ctx).Table("tb_iot_card").
Where("iccid = ? AND deleted_at IS NULL", iccid).Limit(1).Pluck("carrier_id", &carrierIDs).Error
if err != nil {
return 0, err
}
if len(carrierIDs) == 0 {
return 0, nil
}
if r.redis != nil {
_ = r.redis.Set(ctx, cacheKey, strconv.FormatUint(uint64(carrierIDs[0]), 10), constants.GatewayCardCarrierCacheTTL).Err()
}
return carrierIDs[0], nil
}