diff --git a/queue/logic/traffic/trafficStatisticsLogic.go b/queue/logic/traffic/trafficStatisticsLogic.go index a89df57..9c5e9b1 100644 --- a/queue/logic/traffic/trafficStatisticsLogic.go +++ b/queue/logic/traffic/trafficStatisticsLogic.go @@ -3,11 +3,13 @@ package traffic import ( "context" "encoding/json" + "fmt" "strings" "time" "github.com/perfect-panel/server/internal/model/node" "github.com/perfect-panel/server/pkg/logger" + "github.com/perfect-panel/server/pkg/speedlimit" "github.com/hibiken/asynq" "github.com/perfect-panel/server/internal/model/traffic" @@ -126,6 +128,21 @@ func (l *TrafficStatisticsLogic) ProcessTask(ctx context.Context, task *asynq.Ta logger.Field("error", err.Error()), ) } + + // 写完流量后检查是否触发按量限速,若触发则清除节点缓存使限速立即生效 + if planSub, planErr := l.svc.SubscribeModel.FindOne(ctx, sub.SubscribeId); planErr == nil && + (planSub.SpeedLimit > 0 || planSub.TrafficLimit != "") { + throttle := speedlimit.Calculate(ctx, l.svc.DB, sub.UserId, sub.Id, planSub.SpeedLimit, planSub.TrafficLimit) + if throttle.IsThrottled { + cacheKey := fmt.Sprintf("%s%d", node.ServerUserListCacheKey, payload.ServerId) + if delErr := l.svc.Redis.Del(ctx, cacheKey).Err(); delErr != nil { + logger.WithContext(ctx).Error("[TrafficStatistics] Clear server user cache failed", + logger.Field("serverId", payload.ServerId), + logger.Field("error", delErr.Error()), + ) + } + } + } } return nil }