// frequency.go Day 42:频次拦截——滑动窗口,支持自然日/滚动 24h,同号码/同内容两维。 // 计数走 FreqStore 抽象:Redis ZSet(生产) / 内存(单测/降级);DryRun(预检)不计数。 package interceptor import ( "context" "fmt" "hash/fnv" "sync" "time" "go-sms/internal/model/entity" errs "go-sms/internal/pkg/errors" "github.com/redis/go-redis/v9" "gorm.io/gorm" ) // FreqStore 窗口计数:Incr 记录并返回窗口内第 N 次提交(1-based);Count 只读(预检)。 type FreqStore interface { Incr(ctx context.Context, key string, window time.Duration) (int64, error) Count(ctx context.Context, key string, window time.Duration) (int64, error) } // Frequency 频次拦截器(policy_frequency:dim/window_unit/max_count)。 type Frequency struct { DB *gorm.DB // 策略库 Store FreqStore // 窗口计数 Now func() time.Time // 可注入时钟(测试) } // NewFrequency 构造(store nil 时用内存实现,仅建议测试/单机降级)。 func NewFrequency(db *gorm.DB, store FreqStore) *Frequency { if store == nil { store = NewMemoryFreqStore() } return &Frequency{DB: db, Store: store, Now: time.Now} } // Name 拦截器名。 func (f *Frequency) Name() string { return "frequency" } // Check 逐条启用策略校验;白名单豁免;DryRun 只查不增。 func (f *Frequency) Check(ctx context.Context, r *Req) *Block { if f == nil || f.DB == nil { return nil } var policies []entity.PolicyFrequency if r.Scope != nil { // Day51 R3:白名单豁免复用快照集(与黑名单拦截器同一批查结果), // 策略账号级一次 Find 跨号复用(查询失败=nil=放行,与逐号路径一致)。 if r.Scope.Whitelisted(r.Mobile) { return nil } policies = r.Scope.FreqPolicies() } else { var wl int64 if err := f.DB.WithContext(ctx).Model(&entity.PolicyWhitelist{}). Where("mobile = ? AND account_id IN ?", r.Mobile, []int64{0, r.AccountID}). Count(&wl).Error; err == nil && wl > 0 { return nil } if err := f.DB.WithContext(ctx). Where("status = 1 AND account_id IN ?", []int64{0, r.AccountID}). Order("id").Find(&policies).Error; err != nil { return nil // 查询失败放行(可用性优先) } } for _, p := range policies { identity := r.Mobile if p.Dim == 2 { // 同内容:内容哈希 h := fnv.New32a() _, _ = h.Write([]byte(r.Content)) identity = fmt.Sprintf("c%x", h.Sum32()) } var window time.Duration var winTag string if p.WindowUnit == 1 { // 自然日:窗口=至次日零点 now := f.Now() next := time.Date(now.Year(), now.Month(), now.Day(), 24, 0, 0, 0, now.Location()) window = next.Sub(now) winTag = "d" + now.Format("20060102") } else { // 滚动 24h window = 24 * time.Hour winTag = "r24" } if window <= 0 { window = time.Second } key := fmt.Sprintf("gosms:freq:%d:%d:%s:%s", p.AccountID, p.Dim, identity, winTag) var n int64 var err error if r.DryRun { // 预检:只读不占配额 n, err = f.Store.Count(ctx, key, window) if err == nil && n >= int64(p.MaxCount) { // 已达上限即拦(第 max+1 次会被拒) return f.block(p, r) } continue } n, err = f.Store.Incr(ctx, key, window) if err != nil { continue // 存储故障放行 } if n > int64(p.MaxCount) { return f.block(p, r) } } return nil } func (f *Frequency) block(p entity.PolicyFrequency, _ *Req) *Block { e := errs.SendLimited.WithMsg(fmt.Sprintf("频次超限:策略#%d %s最多 %d 条", p.ID, map[int8]string{1: "同号码", 2: "同内容"}[p.Dim], p.MaxCount)) return &Block{Interceptor: f.Name(), Code: e.Code, Msg: e.Msg, HTTP: e} } // MemoryFreqStore 内存滑动窗口(测试/单机降级;非分布式安全)。 type MemoryFreqStore struct { mu sync.Mutex data map[string][]time.Time now func() time.Time } // NewMemoryFreqStore 构造内存滑窗。 func NewMemoryFreqStore() *MemoryFreqStore { return &MemoryFreqStore{data: make(map[string][]time.Time), now: time.Now} } // Incr 记录并返回窗口内次数(过期清除)。 func (m *MemoryFreqStore) Incr(_ context.Context, key string, window time.Duration) (int64, error) { m.mu.Lock() defer m.mu.Unlock() now := m.now() kept := prune(m.data[key], now, window) kept = append(kept, now) m.data[key] = kept return int64(len(kept)), nil } // Count 只读窗口内次数。 func (m *MemoryFreqStore) Count(_ context.Context, key string, window time.Duration) (int64, error) { m.mu.Lock() defer m.mu.Unlock() kept := prune(m.data[key], m.now(), window) m.data[key] = kept return int64(len(kept)), nil } // prune 清除窗口外时间戳(返回原地切片)。 func prune(list []time.Time, now time.Time, window time.Duration) []time.Time { kept := list[:0] for _, t := range list { if now.Sub(t) < window { kept = append(kept, t) } } return kept } // RedisFreqStore Redis ZSet 滑动窗口(ZADD/ZREMRANGEBYSCORE/ZCARD+EXPIRE)。 type RedisFreqStore struct { RDB redis.UniversalClient Now func() time.Time } // NewRedisFreqStore 构造。 func NewRedisFreqStore(rdb redis.UniversalClient) *RedisFreqStore { return &RedisFreqStore{RDB: rdb, Now: time.Now} } // Incr ZSet 滑窗计数(score/member 均纳秒精度,同毫秒多次不合并)。 func (s *RedisFreqStore) Incr(ctx context.Context, key string, window time.Duration) (int64, error) { now := s.Now() ns := now.UnixNano() exp := ns - window.Nanoseconds() pipe := s.RDB.TxPipeline() pipe.ZRemRangeByScore(ctx, key, "-inf", fmt.Sprintf("(%d", exp)) pipe.ZAdd(ctx, key, redis.Z{Score: float64(ns), Member: ns}) pipe.Expire(ctx, key, window+time.Minute) if _, err := pipe.Exec(ctx); err != nil { return 0, err } return s.RDB.ZCard(ctx, key).Result() } // Count 只读窗口内次数(预检;仅清过期不写入)。 func (s *RedisFreqStore) Count(ctx context.Context, key string, window time.Duration) (int64, error) { exp := s.Now().UnixNano() - window.Nanoseconds() if err := s.RDB.ZRemRangeByScore(ctx, key, "-inf", fmt.Sprintf("(%d", exp)).Err(); err != nil { return 0, err } return s.RDB.ZCard(ctx, key).Result() }