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,366 @@
package polling
import (
"context"
"sort"
"strconv"
"strings"
"time"
"github.com/redis/go-redis/v9"
"github.com/break/junhong_cmp_fiber/internal/model"
"github.com/break/junhong_cmp_fiber/internal/model/dto"
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
"github.com/break/junhong_cmp_fiber/pkg/constants"
"github.com/break/junhong_cmp_fiber/pkg/errors"
)
// 分钟计数 Hash 字段(与 gatewaylimit.Guard 写入口径一致)。
const (
statAttempts = "attempts"
statDurationSum = "duration_ms_sum"
statWouldLimit = "would_limit"
statPrefixDur = "duration_le:"
statPrefixExem = "exempt:"
statPrefixUp = "upstream:"
)
// statDurationBuckets 与写入端分桶上界一致(毫秒)。
var statDurationBuckets = []string{"100", "300", "500", "1000", "3000", "10000", "60000", "inf"}
// ListStopResumeRecords 按条件查询停复机记录;时间范围必填且跨度不超过 31 天。
func (s *GatewayRateLimitService) ListStopResumeRecords(ctx context.Context, req *dto.StopResumeRecordListRequest) (*dto.StopResumeRecordPageResult, error) {
if err := requirePlatformReader(ctx); err != nil {
return nil, err
}
if req.StartTime.IsZero() || req.EndTime.IsZero() {
return nil, errors.New(errors.CodeInvalidParam, "停复机记录查询参数不合法:开始时间与结束时间必填")
}
if !req.EndTime.After(req.StartTime) || req.EndTime.Sub(req.StartTime) > time.Duration(constants.StopResumeRecordQueryMaxDays)*24*time.Hour {
return nil, errors.New(errors.CodeInvalidParam, "停复机记录查询参数不合法:时间跨度须大于 0 且不超过 31 天")
}
page, size := normalizePage(req.Page, req.PageSize)
return s.listRecords(ctx, postgres.StopResumeRecordFilter{
ICCID: strings.TrimSpace(req.ICCID), CarrierID: req.CarrierID, Action: req.Action, Source: req.Source,
Result: req.Result, ErrorCategory: req.ErrorCategory, StartTime: req.StartTime, EndTime: req.EndTime,
Page: page, PageSize: size,
})
}
// ListCardStopResumeRecords 查询单卡停复机历史;未传时间默认最近 31 天。
func (s *GatewayRateLimitService) ListCardStopResumeRecords(ctx context.Context, req *dto.CardStopResumeRecordListRequest) (*dto.StopResumeRecordPageResult, error) {
if err := requirePlatformReader(ctx); err != nil {
return nil, err
}
var count int64
if err := s.db.WithContext(ctx).Model(&model.IotCard{}).Where("id = ?", req.ID).Count(&count).Error; err != nil {
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询 IoT 卡失败")
}
if count == 0 {
return nil, errors.New(errors.CodeNotFound, "IoT 卡不存在")
}
end := req.EndTime
if end.IsZero() {
end = time.Now()
}
start := req.StartTime
if start.IsZero() {
start = end.AddDate(0, 0, -constants.StopResumeRecordQueryMaxDays)
}
if !end.After(start) || end.Sub(start) > time.Duration(constants.StopResumeRecordQueryMaxDays)*24*time.Hour {
return nil, errors.New(errors.CodeInvalidParam, "停复机记录查询参数不合法:时间跨度须大于 0 且不超过 31 天")
}
page, size := normalizePage(req.Page, req.PageSize)
cardID := req.ID
return s.listRecords(ctx, postgres.StopResumeRecordFilter{CardID: &cardID, StartTime: start, EndTime: end, Page: page, PageSize: size})
}
func (s *GatewayRateLimitService) listRecords(ctx context.Context, filter postgres.StopResumeRecordFilter) (*dto.StopResumeRecordPageResult, error) {
rows, total, err := postgres.NewCardStopResumeRecordStore(s.db).List(ctx, filter)
if err != nil {
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询停复机记录失败")
}
items := make([]dto.StopResumeRecordResponse, 0, len(rows))
for _, row := range rows {
items = append(items, dto.StopResumeRecordResponse{
ID: row.ID, CardID: row.CardID, ICCID: row.ICCID, CarrierID: row.CarrierID, Action: row.Action,
ActionName: stopResumeActionName(row.Action), Source: row.Source, Scene: row.Scene, Reason: row.Reason,
CommandID: row.CommandID, Attempt: row.Attempt, Result: row.Result, ErrorCategory: row.ErrorCategory,
ErrorCategoryName: errorCategoryName(row.ErrorCategory), ProviderCode: row.ProviderCode,
ProviderMessage: row.ProviderMessage, DurationMS: row.DurationMS, CreatedAt: row.CreatedAt,
})
}
return &dto.StopResumeRecordPageResult{List: items, Total: total, Page: filter.Page, PageSize: filter.PageSize}, nil
}
// CarrierCallStats 汇总最近若干分钟各运营商账户的网关调用(只返回有调用的账户)。
func (s *GatewayRateLimitService) CarrierCallStats(ctx context.Context, redisClient *redis.Client, req *dto.GatewayCallStatsRequest) (*dto.GatewayCallStatsResponse, error) {
if err := requirePlatformReader(ctx); err != nil {
return nil, err
}
minutes := req.Minutes
if minutes <= 0 {
minutes = 15
}
names, err := s.carrierNames(ctx)
if err != nil {
return nil, err
}
carrierIDs := []uint{0}
for id := range names {
carrierIDs = append(carrierIDs, id)
}
sort.Slice(carrierIDs, func(i, j int) bool { return carrierIDs[i] < carrierIDs[j] })
stamps := minuteStamps(time.Now(), minutes)
data, err := loadStatHashes(ctx, redisClient, carrierIDs, stamps)
if err != nil {
return nil, err
}
resp := &dto.GatewayCallStatsResponse{Mode: s.currentMode(ctx), Minutes: minutes, Carriers: []dto.GatewayCarrierStat{}}
for _, carrierID := range carrierIDs {
var ops []dto.GatewayOperationStat
for _, op := range constants.GatewayCardOperations {
stat := aggregateOperation(op, data[statKey{carrierID, op}], len(stamps))
if stat.Attempts > 0 || stat.RateLimited > 0 || stat.WouldLimit > 0 {
ops = append(ops, stat)
}
}
if len(ops) == 0 {
continue
}
name := names[carrierID]
if carrierID == 0 {
name = "未知账户"
}
resp.Carriers = append(resp.Carriers, dto.GatewayCarrierStat{CarrierID: carrierID, CarrierName: name, Operations: ops})
}
return resp, nil
}
// CarrierCallTrend 返回单个运营商账户按时间桶聚合的网关调用趋势。
func (s *GatewayRateLimitService) CarrierCallTrend(ctx context.Context, redisClient *redis.Client, req *dto.GatewayCallTrendRequest) (*dto.GatewayCallTrendResponse, error) {
if err := requirePlatformReader(ctx); err != nil {
return nil, err
}
minutes, bucket := req.Minutes, req.BucketMinutes
if minutes <= 0 {
minutes = 60
}
if bucket <= 0 {
bucket = 1
}
stamps := minuteStamps(time.Now(), minutes)
data, err := loadStatHashes(ctx, redisClient, []uint{req.CarrierID}, stamps)
if err != nil {
return nil, err
}
resp := &dto.GatewayCallTrendResponse{CarrierID: req.CarrierID, Points: []dto.GatewayCallTrendPoint{}}
for start := 0; start < len(stamps); start += bucket {
end := start + bucket
if end > len(stamps) {
end = len(stamps)
}
point := dto.GatewayCallTrendPoint{Time: formatStamp(stamps[start]), Operations: []dto.GatewayOperationStat{}}
for _, op := range constants.GatewayCardOperations {
series := data[statKey{req.CarrierID, op}]
if series == nil {
continue
}
stat := aggregateOperation(op, series[start:end], end-start)
if stat.Attempts > 0 || stat.RateLimited > 0 || stat.WouldLimit > 0 {
point.Operations = append(point.Operations, stat)
}
}
resp.Points = append(resp.Points, point)
}
return resp, nil
}
func (s *GatewayRateLimitService) currentMode(ctx context.Context) string {
if s.runtime == nil {
return constants.RateLimitModeObserve
}
return s.runtime.Settings.Current(ctx).Mode
}
type statKey struct {
carrierID uint
op string
}
// minuteStamps 返回从最早到最近的分钟标识(上海时区 yyyyMMddHHmm)。
func minuteStamps(now time.Time, minutes int) []string {
location := shanghaiLocation()
current := now.In(location).Truncate(time.Minute)
stamps := make([]string, minutes)
for i := 0; i < minutes; i++ {
stamps[minutes-1-i] = current.Add(-time.Duration(i) * time.Minute).Format("200601021504")
}
return stamps
}
func formatStamp(stamp string) string {
t, err := time.ParseInLocation("200601021504", stamp, shanghaiLocation())
if err != nil {
return stamp
}
return t.Format("2006-01-02 15:04")
}
// loadStatHashes 用管道批量读取分钟计数;返回每个账户 × 操作按分钟顺序的 Hash 列表。
func loadStatHashes(ctx context.Context, redisClient *redis.Client, carrierIDs []uint, stamps []string) (map[statKey][]map[string]string, error) {
result := map[statKey][]map[string]string{}
if redisClient == nil {
return result, nil
}
type pending struct {
key statKey
idx int
cmd *redis.MapStringStringCmd
}
pipe := redisClient.Pipeline()
cmds := make([]pending, 0, len(carrierIDs)*len(constants.GatewayCardOperations)*len(stamps))
for _, carrierID := range carrierIDs {
for _, op := range constants.GatewayCardOperations {
key := statKey{carrierID, op}
result[key] = make([]map[string]string, len(stamps))
for idx, stamp := range stamps {
cmds = append(cmds, pending{key: key, idx: idx, cmd: pipe.HGetAll(ctx, constants.RedisGatewayCallStatKey(carrierID, op, stamp))})
}
}
}
if _, err := pipe.Exec(ctx); err != nil && err != redis.Nil {
return nil, errors.Wrap(errors.CodeRedisError, err, "读取网关调用统计失败")
}
for _, item := range cmds {
values, err := item.cmd.Result()
if err == nil && len(values) > 0 {
result[item.key][item.idx] = values
}
}
return result, nil
}
// aggregateOperation 汇总一个操作在若干分钟内的计数。
func aggregateOperation(op string, minutes []map[string]string, minuteCount int) dto.GatewayOperationStat {
stat := dto.GatewayOperationStat{Operation: op, OperationName: constants.GatewayOperationName(op), Failures: map[string]int64{}, Exempt: map[string]int64{}}
buckets := map[string]int64{}
var durationSum int64
for _, values := range minutes {
var minuteAttempts int64
for field, raw := range values {
value, err := strconv.ParseInt(raw, 10, 64)
if err != nil {
continue
}
switch {
case field == statAttempts:
stat.Attempts += value
minuteAttempts = value
case field == statDurationSum:
durationSum += value
case field == statWouldLimit:
stat.WouldLimit += value
case field == constants.GatewayErrorRateLimited:
stat.RateLimited += value
case field == constants.GatewayResultSuccess:
stat.Success += value
case strings.HasPrefix(field, statPrefixDur):
buckets[strings.TrimPrefix(field, statPrefixDur)] += value
case strings.HasPrefix(field, statPrefixExem):
stat.Exempt[strings.TrimPrefix(field, statPrefixExem)] += value
default:
stat.Failures[field] += value
}
}
if qps := float64(minuteAttempts) / 60; qps > stat.PeakMinuteQPS {
stat.PeakMinuteQPS = qps
}
}
if minuteCount > 0 {
stat.AvgQPS = float64(stat.Attempts) / float64(minuteCount*60)
}
if stat.Attempts > 0 {
stat.SuccessRate = float64(stat.Success) / float64(stat.Attempts)
stat.AvgDurationMS = float64(durationSum) / float64(stat.Attempts)
stat.P95DurationMS = percentileBucket(buckets, stat.Attempts, 0.95)
}
return stat
}
// percentileBucket 返回累计占比达到 p 的分桶上界;落在最后一个分桶时返回 -1。
func percentileBucket(buckets map[string]int64, total int64, p float64) int64 {
threshold := int64(float64(total)*p + 0.5)
var cumulative int64
for _, upper := range statDurationBuckets {
cumulative += buckets[upper]
if cumulative >= threshold {
if upper == "inf" {
return -1
}
value, _ := strconv.ParseInt(upper, 10, 64)
return value
}
}
return -1
}
func shanghaiLocation() *time.Location {
location, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
return time.FixedZone("CST", 8*3600)
}
return location
}
func stopResumeActionName(action string) string {
if action == constants.StopResumeActionStart {
return "复机"
}
return "停机"
}
func errorCategoryName(category string) string {
switch category {
case "":
return ""
case constants.GatewayErrorNetwork:
return "网络错误"
case constants.GatewayErrorTimeout:
return "请求超时"
case constants.GatewayErrorHTTP:
return "HTTP 异常"
case constants.GatewayErrorInvalidResponse:
return "响应无效"
default:
return constants.GatewayErrorClassName(category)
}
}
// PollingOptions 返回轮询与网关限流相关的前端枚举,前端不写死取值。
func (s *GatewayRateLimitService) PollingOptions(ctx context.Context) (*dto.PollingOptionsResponse, error) {
if err := requirePlatformReader(ctx); err != nil {
return nil, err
}
resp := &dto.PollingOptionsResponse{}
for _, op := range constants.GatewayCardOperations {
resp.GatewayOperations = append(resp.GatewayOperations, dto.PollingOptionItem{Value: op, Label: constants.GatewayOperationName(op)})
}
for _, class := range constants.GatewayErrorClasses {
resp.ErrorCategories = append(resp.ErrorCategories, dto.PollingOptionItem{Value: class, Label: constants.GatewayErrorClassName(class)})
}
resp.MatchTypes = []dto.PollingOptionItem{{Value: constants.GatewayMatchContains, Label: "包含"}, {Value: constants.GatewayMatchPrefix, Label: "前缀"}, {Value: constants.GatewayMatchRegex, Label: "正则"}}
resp.RateLimitModes = []dto.PollingOptionItem{{Value: constants.RateLimitModeObserve, Label: "只统计不拦截"}, {Value: constants.RateLimitModeEnforce, Label: "拦截"}}
resp.ExemptSources = []dto.PollingOptionItem{
{Value: constants.RateLimitExemptManualClick, Label: "后台人工点击"}, {Value: constants.RateLimitExemptChannelThreshold, Label: "通道阈值"},
{Value: constants.RateLimitExemptAutoRenewal, Label: "自动续费"}, {Value: constants.RateLimitExemptManualRefresh, Label: "手动刷新"},
}
resp.StopResumeResults = []dto.PollingOptionItem{
{Value: constants.StopResumeRecordSuccess, Label: "成功"}, {Value: constants.StopResumeRecordFailed, Label: "失败"},
{Value: constants.StopResumeRecordUnknown, Label: "结果未知"}, {Value: constants.StopResumeRecordAlreadyTarget, Label: "已是目标状态"},
{Value: constants.StopResumeRecordSkipped, Label: "跳过"},
}
resp.StopResumeActions = []dto.PollingOptionItem{{Value: constants.StopResumeActionStop, Label: "停机"}, {Value: constants.StopResumeActionStart, Label: "复机"}}
return resp, nil
}

View File

@@ -0,0 +1,638 @@
package polling
import (
"context"
"regexp"
"sort"
"strconv"
"strings"
"github.com/bytedance/sonic"
"gorm.io/datatypes"
"gorm.io/gorm"
auditinfra "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/model"
"github.com/break/junhong_cmp_fiber/internal/model/dto"
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
"github.com/break/junhong_cmp_fiber/pkg/constants"
"github.com/break/junhong_cmp_fiber/pkg/errors"
"github.com/break/junhong_cmp_fiber/pkg/middleware"
)
// GatewayRateLimitService 维护运营商账户网关限流组与上游错误分类规则(后台配置,简单写事务脚本)。
// 读取仅超级管理员与平台用户可用;修改仅超级管理员可用,并在同一事务写统一审计。
type GatewayRateLimitService struct {
db *gorm.DB
auditWriter *auditinfra.Writer
groups *postgres.CarrierRateLimitGroupStore
rules *postgres.GatewayErrorClassificationStore
runtime *gatewaylimit.Runtime
}
// NewGatewayRateLimitService 创建网关限流配置服务。
func NewGatewayRateLimitService(db *gorm.DB, auditWriter *auditinfra.Writer, runtime *gatewaylimit.Runtime) *GatewayRateLimitService {
return &GatewayRateLimitService{
db: db, auditWriter: auditWriter, runtime: runtime,
groups: postgres.NewCarrierRateLimitGroupStore(db), rules: postgres.NewGatewayErrorClassificationStore(db),
}
}
// requirePlatformReader 校验读取权限:超级管理员或平台用户。
func requirePlatformReader(ctx context.Context) error {
switch middleware.GetUserTypeFromContext(ctx) {
case constants.UserTypeSuperAdmin, constants.UserTypePlatform:
return nil
default:
return errors.New(errors.CodeForbidden, "仅平台账号可查看网关限流配置")
}
}
// requireSuperAdminWriter 校验修改权限:仅超级管理员,返回操作者 ID。
func requireSuperAdminWriter(ctx context.Context) (uint, error) {
if middleware.GetUserTypeFromContext(ctx) != constants.UserTypeSuperAdmin {
return 0, errors.New(errors.CodeForbidden, "仅超级管理员可修改网关限流配置")
}
operatorID := middleware.GetUserIDFromContext(ctx)
if operatorID == 0 {
return 0, errors.New(errors.CodeUnauthorized, "未授权访问")
}
return operatorID, nil
}
func normalizePage(page, size int) (int, int) {
if page < 1 {
page = 1
}
if size < 1 {
size = 20
}
if size > 100 {
size = 100
}
return page, size
}
// ListGroups 分页查询限流组。
func (s *GatewayRateLimitService) ListGroups(ctx context.Context, req *dto.RateLimitGroupListRequest) (*dto.RateLimitGroupPageResult, error) {
if err := requirePlatformReader(ctx); err != nil {
return nil, err
}
page, size := normalizePage(req.Page, req.PageSize)
rows, total, err := s.groups.List(ctx, postgres.RateLimitGroupFilter{CarrierID: req.CarrierID, OnlyGlobal: req.OnlyGlobal, Status: req.Status, Page: page, PageSize: size})
if err != nil {
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询网关限流组失败")
}
names, err := s.carrierNames(ctx)
if err != nil {
return nil, err
}
items := make([]dto.RateLimitGroupResponse, 0, len(rows))
for i := range rows {
items = append(items, toRateLimitGroupResponse(&rows[i], names))
}
return &dto.RateLimitGroupPageResult{List: items, Total: total, Page: page, PageSize: size}, nil
}
// GetGroup 查询限流组详情。
func (s *GatewayRateLimitService) GetGroup(ctx context.Context, id uint) (*dto.RateLimitGroupResponse, error) {
if err := requirePlatformReader(ctx); err != nil {
return nil, err
}
row, err := s.loadGroup(ctx, id)
if err != nil {
return nil, err
}
names, err := s.carrierNames(ctx)
if err != nil {
return nil, err
}
resp := toRateLimitGroupResponse(row, names)
return &resp, nil
}
// CreateGroup 新建限流组。
func (s *GatewayRateLimitService) CreateGroup(ctx context.Context, req *dto.SaveRateLimitGroupRequest) (*dto.RateLimitGroupResponse, error) {
operatorID, err := requireSuperAdminWriter(ctx)
if err != nil {
return nil, err
}
if err := s.validateGroup(ctx, req, 0); err != nil {
return nil, err
}
row := &model.CarrierRateLimitGroup{CarrierID: req.CarrierID, Status: constants.RateLimitGroupStatusEnabled}
applyGroupRequest(row, req)
row.Creator, row.Updater = operatorID, operatorID
err = runPollingTransaction(ctx, s.db, s.auditWriter, func(tx *gorm.DB) error {
if err := tx.WithContext(ctx).Create(row).Error; err != nil {
return errors.Wrap(errors.CodeDatabaseError, err, "新建网关限流组失败")
}
return writePollingAudit(ctx, tx, s.auditWriter, groupAudit(constants.AuditActionRateLimitGroupCreated, "创建网关限流组", row, operatorID, nil))
})
if err != nil {
return nil, err
}
s.invalidateGroups()
return s.GetGroup(ctx, row.ID)
}
// UpdateGroup 修改限流组。
func (s *GatewayRateLimitService) UpdateGroup(ctx context.Context, req *dto.SaveRateLimitGroupRequest) (*dto.RateLimitGroupResponse, error) {
operatorID, err := requireSuperAdminWriter(ctx)
if err != nil {
return nil, err
}
row, err := s.loadGroup(ctx, req.ID)
if err != nil {
return nil, err
}
if !sameCarrier(row.CarrierID, req.CarrierID) {
return nil, errors.New(errors.CodeInvalidParam, "限流组所属运营商账户不可修改,请删除后重新创建")
}
if err := s.validateGroup(ctx, req, row.ID); err != nil {
return nil, err
}
before := groupState(row)
applyGroupRequest(row, req)
row.Updater = operatorID
err = runPollingTransaction(ctx, s.db, s.auditWriter, func(tx *gorm.DB) error {
if err := tx.WithContext(ctx).Save(row).Error; err != nil {
return errors.Wrap(errors.CodeDatabaseError, err, "修改网关限流组失败")
}
return writePollingAudit(ctx, tx, s.auditWriter, groupAudit(constants.AuditActionRateLimitGroupUpdated, "更新网关限流组", row, operatorID, before))
})
if err != nil {
return nil, err
}
s.invalidateGroups()
return s.GetGroup(ctx, row.ID)
}
// UpdateGroupStatus 启停限流组;停用后组内操作回落为默认每秒请求上限。
func (s *GatewayRateLimitService) UpdateGroupStatus(ctx context.Context, id uint, status int) error {
operatorID, err := requireSuperAdminWriter(ctx)
if err != nil {
return err
}
row, err := s.loadGroup(ctx, id)
if err != nil {
return err
}
if status == constants.RateLimitGroupStatusEnabled {
ops, _ := gatewaylimit.DecodeOperations(row.Operations)
if err := s.checkOperationsFree(ctx, row.CarrierID, ops, row.ID); err != nil {
return err
}
}
before := groupState(row)
row.Status, row.Updater = status, operatorID
err = runPollingTransaction(ctx, s.db, s.auditWriter, func(tx *gorm.DB) error {
if err := tx.WithContext(ctx).Model(&model.CarrierRateLimitGroup{}).Where("id = ?", id).
Updates(map[string]any{"status": status, "updater": operatorID}).Error; err != nil {
return errors.Wrap(errors.CodeDatabaseError, err, "更新网关限流组状态失败")
}
return writePollingAudit(ctx, tx, s.auditWriter, groupAudit(constants.AuditActionRateLimitGroupStatusUpdated, "更新网关限流组状态", row, operatorID, before))
})
if err != nil {
return err
}
s.invalidateGroups()
return nil
}
// DeleteGroup 删除限流组;组内操作回落为默认每秒请求上限。
func (s *GatewayRateLimitService) DeleteGroup(ctx context.Context, id uint) error {
operatorID, err := requireSuperAdminWriter(ctx)
if err != nil {
return err
}
row, err := s.loadGroup(ctx, id)
if err != nil {
return err
}
err = runPollingTransaction(ctx, s.db, s.auditWriter, func(tx *gorm.DB) error {
if err := tx.WithContext(ctx).Delete(&model.CarrierRateLimitGroup{}, id).Error; err != nil {
return errors.Wrap(errors.CodeDatabaseError, err, "删除网关限流组失败")
}
return writePollingAudit(ctx, tx, s.auditWriter, groupAudit(constants.AuditActionRateLimitGroupDeleted, "删除网关限流组", row, operatorID, groupState(row)))
})
if err != nil {
return err
}
s.invalidateGroups()
return nil
}
func (s *GatewayRateLimitService) loadGroup(ctx context.Context, id uint) (*model.CarrierRateLimitGroup, error) {
row, err := s.groups.Get(ctx, id)
if err != nil {
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询网关限流组失败")
}
if row == nil {
return nil, errors.New(errors.CodeNotFound, "网关限流组不存在")
}
return row, nil
}
// validateGroup 校验预留额度、高优先级操作归属、同账户同操作只属于一个启用组、组标识唯一。
func (s *GatewayRateLimitService) validateGroup(ctx context.Context, req *dto.SaveRateLimitGroupRequest, selfID uint) error {
if req.ReservedQPS >= req.MaxQPS {
return errors.New(errors.CodeInvalidParam, "预留额度必须小于每秒请求上限")
}
ops := uniqueStrings(req.Operations)
opSet := map[string]bool{}
for _, op := range ops {
opSet[op] = true
}
for _, op := range req.HighPriorityOperations {
if !opSet[op] {
return errors.New(errors.CodeInvalidParam, "高优先级操作必须属于组内操作:"+constants.GatewayOperationName(op))
}
}
if req.ReservedQPS > 0 && len(req.HighPriorityOperations) == 0 {
return errors.New(errors.CodeInvalidParam, "设置预留额度时必须指定高优先级操作")
}
if req.CarrierID != nil {
var count int64
if err := s.db.WithContext(ctx).Model(&model.Carrier{}).Where("id = ?", *req.CarrierID).Count(&count).Error; err != nil {
return errors.Wrap(errors.CodeDatabaseError, err, "查询运营商账户失败")
}
if count == 0 {
return errors.New(errors.CodeInvalidParam, "运营商账户不存在")
}
}
existing, err := s.groups.ListByCarrier(ctx, req.CarrierID)
if err != nil {
return errors.Wrap(errors.CodeDatabaseError, err, "查询网关限流组失败")
}
for _, row := range existing {
if row.ID != selfID && row.GroupCode == req.GroupCode {
return errors.New(errors.CodeInvalidParam, "同一运营商账户内组标识已存在")
}
}
if req.Status == nil || *req.Status == constants.RateLimitGroupStatusEnabled {
return s.checkOperationsFree(ctx, req.CarrierID, ops, selfID)
}
return nil
}
// checkOperationsFree 校验操作未被同账户其他启用组占用。
func (s *GatewayRateLimitService) checkOperationsFree(ctx context.Context, carrierID *uint, ops []string, selfID uint) error {
existing, err := s.groups.ListByCarrier(ctx, carrierID)
if err != nil {
return errors.Wrap(errors.CodeDatabaseError, err, "查询网关限流组失败")
}
for _, row := range existing {
if row.ID == selfID || row.Status != constants.RateLimitGroupStatusEnabled {
continue
}
used, _ := gatewaylimit.DecodeOperations(row.Operations)
for _, op := range used {
for _, want := range ops {
if op == want {
return errors.New(errors.CodeInvalidParam, "操作「"+constants.GatewayOperationName(op)+"」已属于限流组「"+row.GroupName+"」,同一账户下每个操作只能属于一个启用的组")
}
}
}
}
return nil
}
func applyGroupRequest(row *model.CarrierRateLimitGroup, req *dto.SaveRateLimitGroupRequest) {
row.GroupCode = strings.TrimSpace(req.GroupCode)
row.GroupName = strings.TrimSpace(req.GroupName)
row.MaxQPS = req.MaxQPS
row.ReservedQPS = req.ReservedQPS
row.Operations = encodeStrings(uniqueStrings(req.Operations))
row.HighPriorityOperations = encodeStrings(uniqueStrings(req.HighPriorityOperations))
if req.Status != nil {
row.Status = *req.Status
}
}
func groupState(row *model.CarrierRateLimitGroup) map[string]any {
ops, _ := gatewaylimit.DecodeOperations(row.Operations)
high, _ := gatewaylimit.DecodeOperations(row.HighPriorityOperations)
return map[string]any{"group_code": row.GroupCode, "group_name": row.GroupName, "max_qps": row.MaxQPS,
"reserved_qps": row.ReservedQPS, "operations": ops, "high_priority_operations": high, "status": row.Status}
}
func groupAudit(action, summary string, row *model.CarrierRateLimitGroup, operatorID uint, before map[string]any) auditinfra.PollingInput {
var carrierID any
if row.CarrierID != nil {
carrierID = *row.CarrierID
}
return auditinfra.PollingInput{
ActionCode: action, Summary: summary, ResourceType: constants.AuditResourceRateLimitGroup,
ResourceID: row.ID, ResourceKey: "rate_limit_group:" + uintString(row.ID), DisplayName: row.GroupName,
OperatorID: operatorID,
IdentitySnapshot: map[string]any{"id": row.ID, "carrier_id": carrierID, "group_code": row.GroupCode,
"group_name": row.GroupName, "max_qps": row.MaxQPS, "reserved_qps": row.ReservedQPS, "status": row.Status},
BeforeData: before, AfterData: groupState(row),
}
}
func toRateLimitGroupResponse(row *model.CarrierRateLimitGroup, names map[uint]string) dto.RateLimitGroupResponse {
ops, _ := gatewaylimit.DecodeOperations(row.Operations)
high, _ := gatewaylimit.DecodeOperations(row.HighPriorityOperations)
resp := dto.RateLimitGroupResponse{
ID: row.ID, CarrierID: row.CarrierID, GroupCode: row.GroupCode, GroupName: row.GroupName, MaxQPS: row.MaxQPS,
Operations: nonNil(ops), HighPriorityOperations: nonNil(high), ReservedQPS: row.ReservedQPS,
Status: row.Status, StatusName: enabledName(row.Status), CreatedAt: row.CreatedAt, UpdatedAt: row.UpdatedAt,
}
if row.CarrierID != nil {
resp.CarrierName = names[*row.CarrierID]
}
return resp
}
// ListRules 分页查询上游错误分类规则。
func (s *GatewayRateLimitService) ListRules(ctx context.Context, req *dto.GatewayErrorRuleListRequest) (*dto.GatewayErrorRulePageResult, error) {
if err := requirePlatformReader(ctx); err != nil {
return nil, err
}
page, size := normalizePage(req.Page, req.PageSize)
rows, total, err := s.rules.List(ctx, postgres.ErrorClassificationFilter{Category: req.Category, Status: req.Status, Keyword: strings.TrimSpace(req.Keyword), Page: page, PageSize: size})
if err != nil {
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询上游错误分类规则失败")
}
items := make([]dto.GatewayErrorRuleResponse, 0, len(rows))
for i := range rows {
items = append(items, toRuleResponse(&rows[i]))
}
return &dto.GatewayErrorRulePageResult{List: items, Total: total, Page: page, PageSize: size}, nil
}
// CreateRule 新建上游错误分类规则。
func (s *GatewayRateLimitService) CreateRule(ctx context.Context, req *dto.SaveGatewayErrorRuleRequest) (*dto.GatewayErrorRuleResponse, error) {
operatorID, err := requireSuperAdminWriter(ctx)
if err != nil {
return nil, err
}
if err := validateRule(req); err != nil {
return nil, err
}
row := &model.GatewayErrorClassification{Status: constants.RateLimitGroupStatusEnabled}
applyRuleRequest(row, req)
row.Creator, row.Updater = operatorID, operatorID
err = runPollingTransaction(ctx, s.db, s.auditWriter, func(tx *gorm.DB) error {
if err := tx.WithContext(ctx).Create(row).Error; err != nil {
return errors.Wrap(errors.CodeDatabaseError, err, "新建上游错误分类规则失败")
}
return writePollingAudit(ctx, tx, s.auditWriter, ruleAudit(constants.AuditActionGatewayErrorRuleCreated, "创建上游错误分类规则", row, operatorID, nil))
})
if err != nil {
return nil, err
}
s.invalidateRules()
resp := toRuleResponse(row)
return &resp, nil
}
// UpdateRule 修改上游错误分类规则。
func (s *GatewayRateLimitService) UpdateRule(ctx context.Context, req *dto.SaveGatewayErrorRuleRequest) (*dto.GatewayErrorRuleResponse, error) {
operatorID, err := requireSuperAdminWriter(ctx)
if err != nil {
return nil, err
}
if err := validateRule(req); err != nil {
return nil, err
}
row, err := s.loadRule(ctx, req.ID)
if err != nil {
return nil, err
}
before := ruleState(row)
applyRuleRequest(row, req)
row.Updater = operatorID
err = runPollingTransaction(ctx, s.db, s.auditWriter, func(tx *gorm.DB) error {
if err := tx.WithContext(ctx).Save(row).Error; err != nil {
return errors.Wrap(errors.CodeDatabaseError, err, "修改上游错误分类规则失败")
}
return writePollingAudit(ctx, tx, s.auditWriter, ruleAudit(constants.AuditActionGatewayErrorRuleUpdated, "更新上游错误分类规则", row, operatorID, before))
})
if err != nil {
return nil, err
}
s.invalidateRules()
resp := toRuleResponse(row)
return &resp, nil
}
// UpdateRuleStatus 启停上游错误分类规则。
func (s *GatewayRateLimitService) UpdateRuleStatus(ctx context.Context, id uint, status int) error {
operatorID, err := requireSuperAdminWriter(ctx)
if err != nil {
return err
}
row, err := s.loadRule(ctx, id)
if err != nil {
return err
}
before := ruleState(row)
row.Status, row.Updater = status, operatorID
err = runPollingTransaction(ctx, s.db, s.auditWriter, func(tx *gorm.DB) error {
if err := tx.WithContext(ctx).Model(&model.GatewayErrorClassification{}).Where("id = ?", id).
Updates(map[string]any{"status": status, "updater": operatorID}).Error; err != nil {
return errors.Wrap(errors.CodeDatabaseError, err, "更新上游错误分类规则状态失败")
}
return writePollingAudit(ctx, tx, s.auditWriter, ruleAudit(constants.AuditActionGatewayErrorRuleStatusUpdated, "更新上游错误分类规则状态", row, operatorID, before))
})
if err != nil {
return err
}
s.invalidateRules()
return nil
}
// DeleteRule 删除上游错误分类规则。
func (s *GatewayRateLimitService) DeleteRule(ctx context.Context, id uint) error {
operatorID, err := requireSuperAdminWriter(ctx)
if err != nil {
return err
}
row, err := s.loadRule(ctx, id)
if err != nil {
return err
}
err = runPollingTransaction(ctx, s.db, s.auditWriter, func(tx *gorm.DB) error {
if err := tx.WithContext(ctx).Delete(&model.GatewayErrorClassification{}, id).Error; err != nil {
return errors.Wrap(errors.CodeDatabaseError, err, "删除上游错误分类规则失败")
}
return writePollingAudit(ctx, tx, s.auditWriter, ruleAudit(constants.AuditActionGatewayErrorRuleDeleted, "删除上游错误分类规则", row, operatorID, ruleState(row)))
})
if err != nil {
return err
}
s.invalidateRules()
return nil
}
// TestRule 用已启用规则(可附带一条未保存的草稿)对上游返回信息分类,不写任何数据。
func (s *GatewayRateLimitService) TestRule(ctx context.Context, req *dto.TestGatewayErrorRuleRequest) (*dto.TestGatewayErrorRuleResponse, error) {
if err := requirePlatformReader(ctx); err != nil {
return nil, err
}
rows, err := s.rules.ListEnabled(ctx)
if err != nil {
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询上游错误分类规则失败")
}
if req.Draft != nil {
if err := validateRule(req.Draft); err != nil {
return nil, err
}
draft := model.GatewayErrorClassification{}
applyRuleRequest(&draft, req.Draft)
rows = append(rows, draft)
sort.SliceStable(rows, func(i, j int) bool { return rows[i].Priority < rows[j].Priority })
}
result := gatewaylimit.ClassifyWith(rows, req.Operation, req.Message)
matched := result.Keyword != ""
return &dto.TestGatewayErrorRuleResponse{
Category: result.Category, CategoryName: constants.GatewayErrorClassName(result.Category),
RuleID: result.RuleID, Keyword: result.Keyword, MaybeAccepted: result.MaybeAccepted, Matched: matched,
}, nil
}
func (s *GatewayRateLimitService) loadRule(ctx context.Context, id uint) (*model.GatewayErrorClassification, error) {
row, err := s.rules.Get(ctx, id)
if err != nil {
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询上游错误分类规则失败")
}
if row == nil {
return nil, errors.New(errors.CodeNotFound, "上游错误分类规则不存在")
}
return row, nil
}
func validateRule(req *dto.SaveGatewayErrorRuleRequest) error {
if strings.TrimSpace(req.Keyword) == "" {
return errors.New(errors.CodeInvalidParam, "匹配关键字不能为空")
}
if req.MatchType == constants.GatewayMatchRegex {
if _, err := regexp.Compile(req.Keyword); err != nil {
return errors.New(errors.CodeInvalidParam, "正则表达式不合法")
}
}
return nil
}
func applyRuleRequest(row *model.GatewayErrorClassification, req *dto.SaveGatewayErrorRuleRequest) {
row.Keyword = strings.TrimSpace(req.Keyword)
row.MatchType = req.MatchType
row.Category = req.Category
row.Operations = encodeStrings(uniqueStrings(req.Operations))
row.Priority = req.Priority
if row.Priority == 0 {
row.Priority = 100
}
row.MaybeAccepted = req.MaybeAccepted
row.Remark = strings.TrimSpace(req.Remark)
if req.Status != nil {
row.Status = *req.Status
}
}
func ruleState(row *model.GatewayErrorClassification) map[string]any {
ops, _ := gatewaylimit.DecodeOperations(row.Operations)
return map[string]any{"keyword": row.Keyword, "match_type": row.MatchType, "category": row.Category,
"operations": ops, "priority": row.Priority, "maybe_accepted": row.MaybeAccepted, "status": row.Status}
}
func ruleAudit(action, summary string, row *model.GatewayErrorClassification, operatorID uint, before map[string]any) auditinfra.PollingInput {
return auditinfra.PollingInput{
ActionCode: action, Summary: summary, ResourceType: constants.AuditResourceGatewayErrorRule,
ResourceID: row.ID, ResourceKey: "gateway_error_rule:" + uintString(row.ID), DisplayName: row.Keyword,
OperatorID: operatorID,
IdentitySnapshot: map[string]any{"id": row.ID, "keyword": row.Keyword, "match_type": row.MatchType,
"category": row.Category, "priority": row.Priority, "status": row.Status},
BeforeData: before, AfterData: ruleState(row),
}
}
func toRuleResponse(row *model.GatewayErrorClassification) dto.GatewayErrorRuleResponse {
ops, _ := gatewaylimit.DecodeOperations(row.Operations)
return dto.GatewayErrorRuleResponse{
ID: row.ID, Keyword: row.Keyword, MatchType: row.MatchType, Category: row.Category,
CategoryName: constants.GatewayErrorClassName(row.Category), Operations: nonNil(ops), Priority: row.Priority,
MaybeAccepted: row.MaybeAccepted, Status: row.Status, Remark: row.Remark, CreatedAt: row.CreatedAt, UpdatedAt: row.UpdatedAt,
}
}
// carrierNames 返回运营商账户 ID → 名称映射(含已删除账户,便于展示历史配置)。
func (s *GatewayRateLimitService) carrierNames(ctx context.Context) (map[uint]string, error) {
var carriers []model.Carrier
if err := s.db.WithContext(ctx).Unscoped().Select("id", "carrier_name").Find(&carriers).Error; err != nil {
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询运营商账户失败")
}
names := make(map[uint]string, len(carriers))
for _, carrier := range carriers {
names[carrier.ID] = carrier.CarrierName
}
return names, nil
}
func sameCarrier(a, b *uint) bool {
if a == nil || b == nil {
return a == nil && b == nil
}
return *a == *b
}
func uniqueStrings(values []string) []string {
seen := map[string]bool{}
out := make([]string, 0, len(values))
for _, value := range values {
value = strings.TrimSpace(value)
if value == "" || seen[value] {
continue
}
seen[value] = true
out = append(out, value)
}
return out
}
func encodeStrings(values []string) datatypes.JSON {
if values == nil {
values = []string{}
}
raw, err := sonic.Marshal(values)
if err != nil {
return datatypes.JSON("[]")
}
return datatypes.JSON(raw)
}
func nonNil(values []string) []string {
if values == nil {
return []string{}
}
return values
}
func enabledName(status int) string {
if status == constants.RateLimitGroupStatusEnabled {
return "启用"
}
return "停用"
}
func uintString(value uint) string {
return strconv.FormatUint(uint64(value), 10)
}
// invalidateGroups 让本进程限流组缓存立即失效;其他进程在缓存时长(数秒)内自动刷新。
func (s *GatewayRateLimitService) invalidateGroups() {
if s.runtime != nil {
s.invalidateGroups()
}
}
// invalidateRules 让本进程错误分类规则缓存立即失效。
func (s *GatewayRateLimitService) invalidateRules() {
if s.runtime != nil {
s.invalidateRules()
}
}