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:
@@ -31,6 +31,8 @@ func generateOpenAPIDocs(outputPath string, logger *zap.Logger) {
|
||||
handlers.BusinessUserGroup = admin.NewBusinessUserGroupHandler(nil, nil)
|
||||
handlers.ShopBusinessOwnerImport = admin.NewShopBusinessOwnerImportHandler(nil)
|
||||
handlers.PhoneAssetAssociation = admin.NewPhoneAssetAssociationHandler(nil, nil)
|
||||
// 网关限流 Handler 必须同时进入文档工厂,避免新增管理接口遗漏文档注册。
|
||||
handlers.GatewayRateLimit = admin.NewGatewayRateLimitHandler(nil, nil, nil)
|
||||
// 套餐真流量预警 Handler 必须同时进入文档工厂,避免新增管理接口遗漏文档注册。
|
||||
handlers.PackageTrafficAlert = admin.NewPackageTrafficAlertHandler(nil, nil, nil, nil)
|
||||
// 资产钱包自动续费配置 Handler 必须同时进入文档工厂,避免新增管理接口遗漏文档注册。
|
||||
|
||||
@@ -22,6 +22,7 @@ import (
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/bootstrap"
|
||||
"github.com/break/junhong_cmp_fiber/internal/gateway"
|
||||
"github.com/break/junhong_cmp_fiber/internal/infrastructure/gatewaylimit"
|
||||
internalMiddleware "github.com/break/junhong_cmp_fiber/internal/middleware"
|
||||
"github.com/break/junhong_cmp_fiber/internal/routes"
|
||||
"github.com/break/junhong_cmp_fiber/internal/service/verification"
|
||||
@@ -71,6 +72,11 @@ func main() {
|
||||
|
||||
// 9. 初始化 Gateway 客户端(可选)
|
||||
gatewayClient := initGateway(cfg, appLogger)
|
||||
// 卡网关调用按本系统运营商账户限流与统计(默认只统计不拦截)。
|
||||
gatewayLimit := gatewaylimit.NewRuntime(db, redisClient, appLogger)
|
||||
if gatewayClient != nil {
|
||||
gatewayClient.SetCallGuard(gatewayLimit.Guard)
|
||||
}
|
||||
|
||||
// 10. 初始化所有业务组件(通过 Bootstrap)
|
||||
result, err := bootstrap.Bootstrap(&bootstrap.Dependencies{
|
||||
@@ -83,6 +89,7 @@ func main() {
|
||||
QueueClient: queueClient,
|
||||
StorageService: storageSvc,
|
||||
GatewayClient: gatewayClient,
|
||||
GatewayLimit: gatewayLimit,
|
||||
})
|
||||
if err != nil {
|
||||
appLogger.Fatal("初始化业务组件失败", zap.Error(err))
|
||||
|
||||
@@ -40,6 +40,8 @@ func generateAdminDocs(outputPath string) error {
|
||||
handlers.BusinessUserGroup = admin.NewBusinessUserGroupHandler(nil, nil)
|
||||
handlers.ShopBusinessOwnerImport = admin.NewShopBusinessOwnerImportHandler(nil)
|
||||
handlers.PhoneAssetAssociation = admin.NewPhoneAssetAssociationHandler(nil, nil)
|
||||
// 网关限流 Handler 必须同时进入文档工厂,避免新增管理接口遗漏文档注册。
|
||||
handlers.GatewayRateLimit = admin.NewGatewayRateLimitHandler(nil, nil, nil)
|
||||
// 套餐真流量预警 Handler 必须同时进入文档工厂,避免新增管理接口遗漏文档注册。
|
||||
handlers.PackageTrafficAlert = admin.NewPackageTrafficAlertHandler(nil, nil, nil, nil)
|
||||
// 资产钱包自动续费配置 Handler 必须同时进入文档工厂,避免新增管理接口遗漏文档注册。
|
||||
|
||||
@@ -34,6 +34,7 @@ import (
|
||||
cardObservationInfra "github.com/break/junhong_cmp_fiber/internal/infrastructure/cardobservation"
|
||||
carrierThresholdInfra "github.com/break/junhong_cmp_fiber/internal/infrastructure/carrierthreshold"
|
||||
commissionDelivery "github.com/break/junhong_cmp_fiber/internal/infrastructure/commissiondelivery"
|
||||
"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/infrastructure/messaging/outbox"
|
||||
notificationInfra "github.com/break/junhong_cmp_fiber/internal/infrastructure/notification"
|
||||
@@ -99,6 +100,8 @@ type workerRuntime struct {
|
||||
carrierThresholdService *carrierThresholdApp.Service
|
||||
// assetAutoRenewalService 是资产钱包自动续费的每日扫描、复机消费者与恢复扫描的唯一用例实例。
|
||||
assetAutoRenewalService *assetAutoRenewalApp.Service
|
||||
// gatewayLimit 是卡网关运营商账户限流组件(限流、统计、停复机记录)。
|
||||
gatewayLimit *gatewaylimit.Runtime
|
||||
}
|
||||
|
||||
func main() {
|
||||
@@ -178,6 +181,8 @@ func runWorker(cfg *config.Config) {
|
||||
registerAuditArchiveTask(taskHandler.GetMux(), runtime, cfg.Worker.AuditRetentionCleanupEnabled, cfg.Worker.AuditArchiveTasksEnabled, appLogger, retentionLogger)
|
||||
outboxHandler := outbox.NewHandler(runtime.outboxConsumers)
|
||||
taskHandler.GetMux().HandleFunc(constants.TaskTypeOutboxDeliver, outboxHandler.Handle)
|
||||
partitionMaintainer := gatewaylimit.NewPartitionMaintainer(postgres.NewCardStopResumeRecordStore(runtime.db), runtime.gatewayLimit.Settings, appLogger)
|
||||
taskHandler.GetMux().HandleFunc(constants.TaskTypeStopResumeRecordPartition, partitionMaintainer.Handle)
|
||||
startOutboxRelay(ctx, runtime, cfg.Worker.InstanceName, appLogger)
|
||||
rescuePendingImportTasks(ctx, runtime, appLogger)
|
||||
|
||||
@@ -266,6 +271,11 @@ func initWorkerRuntime(ctx context.Context, cfg *config.Config, appLogger *zap.L
|
||||
|
||||
storageSvc := initStorage(cfg, appLogger)
|
||||
gatewayClient := initGateway(cfg, appLogger)
|
||||
// 卡网关调用按本系统运营商账户限流与统计(默认只统计不拦截)。
|
||||
gatewayLimit := gatewaylimit.NewRuntime(db, redisClient, appLogger)
|
||||
if gatewayClient != nil {
|
||||
gatewayClient.SetCallGuard(gatewayLimit.Guard)
|
||||
}
|
||||
|
||||
asynqClient := asynq.NewClient(asynq.RedisClientOpt{
|
||||
Addr: redisAddr,
|
||||
@@ -282,6 +292,7 @@ func initWorkerRuntime(ctx context.Context, cfg *config.Config, appLogger *zap.L
|
||||
QueueClient: workerQueueClient,
|
||||
StorageService: storageSvc,
|
||||
GatewayClient: gatewayClient,
|
||||
GatewayLimit: gatewayLimit,
|
||||
}
|
||||
|
||||
workerResult, err := bootstrap.BootstrapWorker(workerDeps)
|
||||
@@ -289,7 +300,13 @@ func initWorkerRuntime(ctx context.Context, cfg *config.Config, appLogger *zap.L
|
||||
appLogger.Fatal("Worker Bootstrap 失败", zap.Error(err))
|
||||
}
|
||||
|
||||
workerServer := queue.NewServer(redisClient, &cfg.Queue, appLogger)
|
||||
workerServer := queue.NewServer(redisClient, &cfg.Queue, appLogger, queue.WithRateLimitedRetry(queue.RateLimitedRetry{
|
||||
Match: gateway.IsRateLimited,
|
||||
Delay: func() time.Duration {
|
||||
return gatewayLimit.Settings.Current(context.Background()).RetryDelay
|
||||
},
|
||||
OnDeferred: gatewayLimit.TrackDeferral,
|
||||
}))
|
||||
outboxQueueClient := workerQueueClient
|
||||
|
||||
pollingConfigStore := postgres.NewPollingConfigStore(db)
|
||||
@@ -324,6 +341,9 @@ func initWorkerRuntime(ctx context.Context, cfg *config.Config, appLogger *zap.L
|
||||
cfg.Polling.VerboseLog,
|
||||
cfg.Worker.PollingTotalMaxConcurrency,
|
||||
)
|
||||
pollingBase.SetRateLimitedDelay(func() time.Duration {
|
||||
return gatewayLimit.Settings.Current(context.Background()).RetryDelay
|
||||
})
|
||||
if stopResumeSvc, ok := workerResult.Services.StopResumeService.(*iot_card_svc.StopResumeService); ok {
|
||||
stopResumeSvc.SetPollingCallback(lifecycleSvc)
|
||||
}
|
||||
@@ -337,6 +357,7 @@ func initWorkerRuntime(ctx context.Context, cfg *config.Config, appLogger *zap.L
|
||||
db: db,
|
||||
storageSvc: storageSvc,
|
||||
gatewayClient: gatewayClient,
|
||||
gatewayLimit: gatewayLimit,
|
||||
asynqClient: asynqClient,
|
||||
workerResult: workerResult,
|
||||
workerServer: workerServer,
|
||||
@@ -996,6 +1017,14 @@ func registerAsynqScheduleTasks(asynqScheduler *asynq.Scheduler, auditArchiveEna
|
||||
)); err != nil {
|
||||
return fmt.Errorf("注册数据清理定时任务失败: %w", err)
|
||||
}
|
||||
if _, err := asynqScheduler.Register("CRON_TZ=Asia/Shanghai 30 2 * * *", asynq.NewTask(
|
||||
constants.TaskTypeStopResumeRecordPartition,
|
||||
nil,
|
||||
asynq.MaxRetry(3),
|
||||
asynq.Queue(constants.QueueDataCleanup),
|
||||
)); err != nil {
|
||||
return fmt.Errorf("注册停复机记录分区维护定时任务失败: %w", err)
|
||||
}
|
||||
if _, err := asynqScheduler.Register("15 2 * * *", asynq.NewTask(
|
||||
constants.TaskTypeNotificationCleanup,
|
||||
nil,
|
||||
|
||||
Reference in New Issue
Block a user