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,258 @@
package postgres
import (
"context"
stderrors "errors"
"fmt"
"time"
"gorm.io/gorm"
"github.com/break/junhong_cmp_fiber/internal/model"
)
// CarrierRateLimitGroupStore 运营商账户网关限流组存储。
type CarrierRateLimitGroupStore struct {
db *gorm.DB
}
// NewCarrierRateLimitGroupStore 创建限流组存储。
func NewCarrierRateLimitGroupStore(db *gorm.DB) *CarrierRateLimitGroupStore {
return &CarrierRateLimitGroupStore{db: db}
}
// RateLimitGroupFilter 是限流组列表筛选条件;CarrierID 为 nil 不限,OnlyGlobal 只返回全局组。
type RateLimitGroupFilter struct {
CarrierID *uint
OnlyGlobal bool
Status *int
Page int
PageSize int
}
// List 分页查询限流组。
func (s *CarrierRateLimitGroupStore) List(ctx context.Context, filter RateLimitGroupFilter) ([]model.CarrierRateLimitGroup, int64, error) {
query := s.db.WithContext(ctx).Model(&model.CarrierRateLimitGroup{})
if filter.OnlyGlobal {
query = query.Where("carrier_id IS NULL")
} else if filter.CarrierID != nil {
query = query.Where("carrier_id = ?", *filter.CarrierID)
}
if filter.Status != nil {
query = query.Where("status = ?", *filter.Status)
}
var total int64
if err := query.Count(&total).Error; err != nil {
return nil, 0, err
}
var rows []model.CarrierRateLimitGroup
err := query.Order("carrier_id NULLS FIRST, id").Offset((filter.Page - 1) * filter.PageSize).Limit(filter.PageSize).Find(&rows).Error
return rows, total, err
}
// ListEnabled 返回全部启用的限流组,供限流器加载。
func (s *CarrierRateLimitGroupStore) ListEnabled(ctx context.Context) ([]model.CarrierRateLimitGroup, error) {
var rows []model.CarrierRateLimitGroup
err := s.db.WithContext(ctx).Where("status = ?", 1).Order("id").Find(&rows).Error
return rows, err
}
// ListByCarrier 返回同一账户(nil 为全局组)下的全部未删除组,供保存时校验操作归属。
func (s *CarrierRateLimitGroupStore) ListByCarrier(ctx context.Context, carrierID *uint) ([]model.CarrierRateLimitGroup, error) {
query := s.db.WithContext(ctx)
if carrierID == nil {
query = query.Where("carrier_id IS NULL")
} else {
query = query.Where("carrier_id = ?", *carrierID)
}
var rows []model.CarrierRateLimitGroup
err := query.Find(&rows).Error
return rows, err
}
// Get 按 ID 查询限流组;不存在返回 nil、nil。
func (s *CarrierRateLimitGroupStore) Get(ctx context.Context, id uint) (*model.CarrierRateLimitGroup, error) {
var row model.CarrierRateLimitGroup
err := s.db.WithContext(ctx).First(&row, id).Error
if stderrors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return &row, nil
}
// Create 新建限流组。
func (s *CarrierRateLimitGroupStore) Create(ctx context.Context, row *model.CarrierRateLimitGroup) error {
return s.db.WithContext(ctx).Create(row).Error
}
// Save 保存限流组全部字段。
func (s *CarrierRateLimitGroupStore) Save(ctx context.Context, row *model.CarrierRateLimitGroup) error {
return s.db.WithContext(ctx).Save(row).Error
}
// Delete 软删除限流组。
func (s *CarrierRateLimitGroupStore) Delete(ctx context.Context, id uint) error {
return s.db.WithContext(ctx).Delete(&model.CarrierRateLimitGroup{}, id).Error
}
// GatewayErrorClassificationStore 上游错误分类规则存储。
type GatewayErrorClassificationStore struct {
db *gorm.DB
}
// NewGatewayErrorClassificationStore 创建错误分类规则存储。
func NewGatewayErrorClassificationStore(db *gorm.DB) *GatewayErrorClassificationStore {
return &GatewayErrorClassificationStore{db: db}
}
// ErrorClassificationFilter 是错误分类规则列表筛选条件。
type ErrorClassificationFilter struct {
Category string
Status *int
Keyword string
Page int
PageSize int
}
// List 分页查询错误分类规则。
func (s *GatewayErrorClassificationStore) List(ctx context.Context, filter ErrorClassificationFilter) ([]model.GatewayErrorClassification, int64, error) {
query := s.db.WithContext(ctx).Model(&model.GatewayErrorClassification{})
if filter.Category != "" {
query = query.Where("category = ?", filter.Category)
}
if filter.Status != nil {
query = query.Where("status = ?", *filter.Status)
}
if filter.Keyword != "" {
query = query.Where("keyword LIKE ?", "%"+filter.Keyword+"%")
}
var total int64
if err := query.Count(&total).Error; err != nil {
return nil, 0, err
}
var rows []model.GatewayErrorClassification
err := query.Order("priority, id").Offset((filter.Page - 1) * filter.PageSize).Limit(filter.PageSize).Find(&rows).Error
return rows, total, err
}
// ListEnabled 返回全部启用规则(按优先级),供分类器加载。
func (s *GatewayErrorClassificationStore) ListEnabled(ctx context.Context) ([]model.GatewayErrorClassification, error) {
var rows []model.GatewayErrorClassification
err := s.db.WithContext(ctx).Where("status = ?", 1).Order("priority, id").Find(&rows).Error
return rows, err
}
// Get 按 ID 查询规则;不存在返回 nil、nil。
func (s *GatewayErrorClassificationStore) Get(ctx context.Context, id uint) (*model.GatewayErrorClassification, error) {
var row model.GatewayErrorClassification
err := s.db.WithContext(ctx).First(&row, id).Error
if stderrors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return &row, nil
}
// Create 新建规则。
func (s *GatewayErrorClassificationStore) Create(ctx context.Context, row *model.GatewayErrorClassification) error {
return s.db.WithContext(ctx).Create(row).Error
}
// Save 保存规则全部字段。
func (s *GatewayErrorClassificationStore) Save(ctx context.Context, row *model.GatewayErrorClassification) error {
return s.db.WithContext(ctx).Save(row).Error
}
// Delete 软删除规则。
func (s *GatewayErrorClassificationStore) Delete(ctx context.Context, id uint) error {
return s.db.WithContext(ctx).Delete(&model.GatewayErrorClassification{}, id).Error
}
// CardStopResumeRecordStore 停复机调用记录存储(分区表)。
type CardStopResumeRecordStore struct {
db *gorm.DB
}
// NewCardStopResumeRecordStore 创建停复机记录存储。
func NewCardStopResumeRecordStore(db *gorm.DB) *CardStopResumeRecordStore {
return &CardStopResumeRecordStore{db: db}
}
// StopResumeRecordFilter 是停复机记录列表筛选条件;时间范围由调用方保证必填且有上限。
type StopResumeRecordFilter struct {
CardID *uint
ICCID string
CarrierID *uint
Action string
Source string
Result string
ErrorCategory string
StartTime time.Time
EndTime time.Time
Page int
PageSize int
}
// Create 写入一条停复机记录。
func (s *CardStopResumeRecordStore) Create(ctx context.Context, row *model.CardStopResumeRecord) error {
return s.db.WithContext(ctx).Create(row).Error
}
// List 按条件分页查询停复机记录,按时间倒序。
func (s *CardStopResumeRecordStore) List(ctx context.Context, filter StopResumeRecordFilter) ([]model.CardStopResumeRecord, int64, error) {
query := s.db.WithContext(ctx).Model(&model.CardStopResumeRecord{}).
Where("created_at >= ? AND created_at < ?", filter.StartTime, filter.EndTime)
if filter.CardID != nil {
query = query.Where("card_id = ?", *filter.CardID)
}
if filter.ICCID != "" {
query = query.Where("iccid = ?", filter.ICCID)
}
if filter.CarrierID != nil {
query = query.Where("carrier_id = ?", *filter.CarrierID)
}
if filter.Action != "" {
query = query.Where("action = ?", filter.Action)
}
if filter.Source != "" {
query = query.Where("source = ?", filter.Source)
}
if filter.Result != "" {
query = query.Where("result = ?", filter.Result)
}
if filter.ErrorCategory != "" {
query = query.Where("error_category = ?", filter.ErrorCategory)
}
var total int64
if err := query.Count(&total).Error; err != nil {
return nil, 0, err
}
var rows []model.CardStopResumeRecord
err := query.Order("created_at DESC, id DESC").Offset((filter.Page - 1) * filter.PageSize).Limit(filter.PageSize).Find(&rows).Error
return rows, total, err
}
// stopResumeRecordPartitionName 返回某月分区表名(上海时区月份)。
func stopResumeRecordPartitionName(month time.Time) string {
return "tb_card_stop_resume_record_" + month.Format("200601")
}
// EnsureMonthPartition 创建 month 所在月的分区(已存在则跳过);month 须为上海时区月初。
func (s *CardStopResumeRecordStore) EnsureMonthPartition(ctx context.Context, month time.Time) error {
next := month.AddDate(0, 1, 0)
sql := fmt.Sprintf(
"CREATE TABLE IF NOT EXISTS %s PARTITION OF tb_card_stop_resume_record FOR VALUES FROM ('%s') TO ('%s')",
stopResumeRecordPartitionName(month), month.Format(time.RFC3339), next.Format(time.RFC3339))
return s.db.WithContext(ctx).Exec(sql).Error
}
// DropMonthPartition 删除 month 所在月的分区(不存在则跳过),用于按保留期清理。
func (s *CardStopResumeRecordStore) DropMonthPartition(ctx context.Context, month time.Time) error {
sql := fmt.Sprintf("DROP TABLE IF EXISTS %s", stopResumeRecordPartitionName(month))
return s.db.WithContext(ctx).Exec(sql).Error
}