Procházet zdrojové kódy

fix(service): 优化防御阈值计算逻辑

- 修复 ConfigMaxProtection 配置解析问题
- 增加防御阈值的空值和格式错误检查
- 优化防御阈值的计算方法,支持 2000G 以上的阈值
-增加获取套餐 ID 失败时的日志记录
fusu před 1 měsícem
rodič
revize
4229cfebc2
1 změnil soubory, kde provedl 19 přidání a 7 odebrání
  1. 19 7
      internal/service/globallimit.go

+ 19 - 7
internal/service/globallimit.go

@@ -9,6 +9,7 @@ import (
 	"github.com/go-nunu/nunu-layout-advanced/internal/repository"
 	"github.com/mozillazg/go-pinyin"
 	"github.com/spf13/viper"
+	"go.uber.org/zap"
 	"golang.org/x/sync/errgroup"
 	"gorm.io/gorm"
 	"strconv"
@@ -261,21 +262,32 @@ func (s *globalLimitService) AddGlobalLimit(ctx context.Context, req v1.GlobalLi
 	}
 
 	// 获取套餐ID
-	maxProtection := strings.Split(require.ConfigMaxProtection, "G")
-	maxProtectionInt, err := strconv.Atoi(maxProtection[0])
+	maxProtection := strings.TrimSuffix(require.ConfigMaxProtection, "G")
+	if maxProtection == "" {
+		return fmt.Errorf("无效的配置 ConfigMaxProtection: '%s',数字部分为空", require.ConfigMaxProtection)
+	}
+	maxProtectionInt, err := strconv.Atoi(maxProtection)
 	if err != nil {
-		return err
+		return fmt.Errorf("无效的配置 ConfigMaxProtection: '%s',无法转换为数字", require.ConfigMaxProtection)
 	}
 	var planId int64
-	if maxProtectionInt < 1000 {
-		NodeAreaName := fmt.Sprintf("%s-%dT",require.NodeArea, 1)
-		planId, err = s.globalLimitRepository.GetNodeArea(ctx, NodeAreaName)
-		if err != nil {
+	maxProtectionNum := 1
+	if maxProtectionInt >= 2000 {
+		maxProtectionNum = maxProtectionInt / 1000
+	}
+	NodeAreaName := fmt.Sprintf("%s-%dT",require.NodeArea, maxProtectionNum)
+	planId, err = s.globalLimitRepository.GetNodeArea(ctx, NodeAreaName)
+	if err != nil {
+		if errors.Is(err, gorm.ErrRecordNotFound) {
+			planId = 0
+		}else {
 			return err
 		}
 	}
 	if planId == 0 {
+		// 安全冗余套餐
 		planId = 6
+		s.logger.Warn("获取套餐Id失败",  zap.String("节点区域", NodeAreaName), zap.String("防御阈值", require.ConfigMaxProtection),zap.Int64("套餐Id", int64(req.Uid)),zap.Int64("魔方套餐Id", int64(req.HostId)))
 	}