收口审计治理与套餐任务进展
Constraint: 在线热修前必须保存当前迭代分支全部有效代码进展 Confidence: medium Scope-risk: broad Directive: 后续修改需保持审计事件与业务事务边界一致 Tested: git diff --cached --check Not-tested: 未运行全量测试,提交用于切换分支前保存既有工作
This commit is contained in:
@@ -5,17 +5,28 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
|
||||
auditinfra "github.com/break/junhong_cmp_fiber/internal/infrastructure/audit"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"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"
|
||||
)
|
||||
|
||||
// ConcurrencyService 并发控制服务
|
||||
type ConcurrencyService struct {
|
||||
store *postgres.PollingConcurrencyConfigStore
|
||||
redis *redis.Client
|
||||
store *postgres.PollingConcurrencyConfigStore
|
||||
db *gorm.DB
|
||||
auditWriter *auditinfra.Writer
|
||||
redis *redis.Client
|
||||
}
|
||||
|
||||
// SetAudit 注入轮询并发配置事务与统一审计 Writer。
|
||||
func (s *ConcurrencyService) SetAudit(db *gorm.DB, writer *auditinfra.Writer) {
|
||||
s.db = db
|
||||
s.auditWriter = writer
|
||||
}
|
||||
|
||||
// NewConcurrencyService 创建并发控制服务实例
|
||||
@@ -113,14 +124,34 @@ func (s *ConcurrencyService) UpdateMaxConcurrency(ctx context.Context, taskType
|
||||
}
|
||||
|
||||
// 验证任务类型存在
|
||||
_, err := s.store.GetByTaskType(ctx, taskType)
|
||||
config, err := s.store.GetByTaskType(ctx, taskType)
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.CodeNotFound, err, "任务类型不存在")
|
||||
}
|
||||
|
||||
// 更新数据库
|
||||
if err := s.store.UpdateMaxConcurrency(ctx, taskType, maxConcurrency, updatedBy); err != nil {
|
||||
return errors.Wrap(errors.CodeInternalError, err, "更新并发配置失败")
|
||||
before := config.MaxConcurrency
|
||||
err = runPollingTransaction(ctx, s.db, s.auditWriter, func(tx *gorm.DB) error {
|
||||
if err := s.store.WithTx(tx).UpdateMaxConcurrency(ctx, taskType, maxConcurrency, updatedBy); err != nil {
|
||||
return err
|
||||
}
|
||||
config.MaxConcurrency = maxConcurrency
|
||||
return writePollingAudit(ctx, tx, s.auditWriter, auditinfra.PollingInput{
|
||||
ActionCode: constants.AuditActionPollingConcurrencyUpdated, Summary: "更新轮询并发配置",
|
||||
ResourceType: constants.AuditResourcePollingConcurrencyConfig, ResourceID: config.ID,
|
||||
ResourceKey: config.TaskType, DisplayName: s.getTaskTypeName(config.TaskType), OperatorID: updatedBy,
|
||||
IdentitySnapshot: pollingConcurrencyIdentity(config),
|
||||
BeforeData: map[string]any{"max_concurrency": before}, AfterData: map[string]any{"max_concurrency": maxConcurrency},
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
appErr := errors.Wrap(errors.CodeInternalError, err, "更新并发配置失败")
|
||||
recordPollingFailure(ctx, s.db, s.auditWriter, auditinfra.PollingInput{
|
||||
ActionCode: constants.AuditActionPollingConcurrencyUpdated, Summary: "更新轮询并发配置失败",
|
||||
ResourceType: constants.AuditResourcePollingConcurrencyConfig, ResourceID: config.ID,
|
||||
ResourceKey: config.TaskType, DisplayName: s.getTaskTypeName(config.TaskType), OperatorID: updatedBy,
|
||||
IdentitySnapshot: pollingConcurrencyIdentity(config), BeforeData: map[string]any{"max_concurrency": before},
|
||||
}, appErr)
|
||||
return appErr
|
||||
}
|
||||
|
||||
// 同步更新 Redis 配置缓存
|
||||
@@ -134,19 +165,72 @@ func (s *ConcurrencyService) UpdateMaxConcurrency(ctx context.Context, taskType
|
||||
|
||||
// ResetConcurrency 重置并发计数(用于信号量修复)
|
||||
func (s *ConcurrencyService) ResetConcurrency(ctx context.Context, taskType string) error {
|
||||
operatorID := middleware.GetUserIDFromContext(ctx)
|
||||
if operatorID == 0 {
|
||||
return errors.New(errors.CodeUnauthorized, "未授权访问")
|
||||
}
|
||||
// 验证任务类型存在
|
||||
_, err := s.store.GetByTaskType(ctx, taskType)
|
||||
config, err := s.store.GetByTaskType(ctx, taskType)
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.CodeNotFound, err, "任务类型不存在")
|
||||
}
|
||||
|
||||
// 重置 Redis 当前计数为 0
|
||||
currentKey := constants.RedisPollingConcurrencyCurrentKey(taskType)
|
||||
if err := s.redis.Set(ctx, currentKey, 0, 24*time.Hour).Err(); err != nil {
|
||||
return errors.Wrap(errors.CodeInternalError, err, "重置并发计数失败")
|
||||
before, getErr := s.redis.Get(ctx, currentKey).Int64()
|
||||
beforeExists := getErr == nil
|
||||
if getErr != nil && getErr != redis.Nil {
|
||||
appErr := errors.Wrap(errors.CodeInternalError, getErr, "读取并发计数失败")
|
||||
recordPollingFailure(ctx, s.db, s.auditWriter, auditinfra.PollingInput{
|
||||
ActionCode: constants.AuditActionPollingConcurrencyReset, Summary: "重置轮询并发计数失败",
|
||||
ResourceType: constants.AuditResourcePollingConcurrencyConfig, ResourceID: config.ID,
|
||||
ResourceKey: config.TaskType, DisplayName: s.getTaskTypeName(config.TaskType), OperatorID: operatorID,
|
||||
IdentitySnapshot: pollingConcurrencyIdentity(config),
|
||||
}, appErr)
|
||||
return appErr
|
||||
}
|
||||
|
||||
return nil
|
||||
beforeTTL := time.Duration(0)
|
||||
if beforeExists {
|
||||
beforeTTL, _ = s.redis.PTTL(ctx, currentKey).Result()
|
||||
if beforeTTL < 0 {
|
||||
beforeTTL = 0
|
||||
}
|
||||
}
|
||||
if err := s.redis.Set(ctx, currentKey, 0, 24*time.Hour).Err(); err != nil {
|
||||
appErr := errors.Wrap(errors.CodeInternalError, err, "重置并发计数失败")
|
||||
recordPollingFailure(ctx, s.db, s.auditWriter, auditinfra.PollingInput{
|
||||
ActionCode: constants.AuditActionPollingConcurrencyReset, Summary: "重置轮询并发计数失败",
|
||||
ResourceType: constants.AuditResourcePollingConcurrencyConfig, ResourceID: config.ID,
|
||||
ResourceKey: config.TaskType, DisplayName: s.getTaskTypeName(config.TaskType), OperatorID: operatorID,
|
||||
IdentitySnapshot: pollingConcurrencyIdentity(config), BeforeData: map[string]any{"current": before},
|
||||
}, appErr)
|
||||
return appErr
|
||||
}
|
||||
err = runPollingTransaction(ctx, s.db, s.auditWriter, func(tx *gorm.DB) error {
|
||||
return writePollingAudit(ctx, tx, s.auditWriter, auditinfra.PollingInput{
|
||||
ActionCode: constants.AuditActionPollingConcurrencyReset, Summary: "重置轮询并发计数",
|
||||
ResourceType: constants.AuditResourcePollingConcurrencyConfig, ResourceID: config.ID,
|
||||
ResourceKey: config.TaskType, DisplayName: s.getTaskTypeName(config.TaskType), OperatorID: operatorID,
|
||||
IdentitySnapshot: pollingConcurrencyIdentity(config),
|
||||
BeforeData: map[string]any{"current": before}, AfterData: map[string]any{"current": int64(0)},
|
||||
})
|
||||
})
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if beforeExists {
|
||||
_ = s.redis.Set(ctx, currentKey, before, beforeTTL).Err()
|
||||
} else {
|
||||
_ = s.redis.Del(ctx, currentKey).Err()
|
||||
}
|
||||
appErr := errors.Wrap(errors.CodeInternalError, err, "记录重置并发计数审计失败")
|
||||
recordPollingFailure(ctx, s.db, s.auditWriter, auditinfra.PollingInput{
|
||||
ActionCode: constants.AuditActionPollingConcurrencyReset, Summary: "重置轮询并发计数失败",
|
||||
ResourceType: constants.AuditResourcePollingConcurrencyConfig, ResourceID: config.ID,
|
||||
ResourceKey: config.TaskType, DisplayName: s.getTaskTypeName(config.TaskType), OperatorID: operatorID,
|
||||
IdentitySnapshot: pollingConcurrencyIdentity(config), BeforeData: map[string]any{"current": before},
|
||||
}, appErr)
|
||||
return appErr
|
||||
}
|
||||
|
||||
// InitFromDB 从数据库初始化 Redis 并发配置
|
||||
|
||||
Reference in New Issue
Block a user