// scope.go Day 51(R3 性能债专项 1/4):请求级数据快照——同一次提交/预检内, // 账号级数据(频次策略/签名白名单)只查一次,号码级数据(白名单/黑名单)批量 IN 一次取全。 // 背景:Day50 压测定位拦截器逐号查库(黑名单:白名单 Count+黑名单 Count;频次:白名单 // Count+策略 Find),批量 100 号=每号 4 查共 400 次 MySQL 往返,是受理批量 P50 915ms 主因之一。 // 设计:惰性加载(sync.Once,未启用的能力零开销)+生命周期限定单次请求同步链(逐号循环 // 串行调用);Req.Scope=nil 时拦截器走原逐号路径(单号 precheck/旧测试兼容)。 package interceptor import ( "context" "sync" "go-sms/internal/model/entity" "github.com/redis/go-redis/v9" "gorm.io/gorm" ) // Scope 请求级快照(db 与链装配同库;mobiles=本次请求全部号码,批查全集)。 type Scope struct { ctx context.Context db *gorm.DB accountID int64 mobiles []string wlOnce sync.Once wl map[string]struct{} // 白名单命中集(账号级+全局合并) blOnce sync.Once bl map[string]struct{} // 黑名单命中集(DB 二级) rblOnce sync.Once rbl map[string]bool // 黑名单命中集(Redis 一级,批量 SMIsMember) plOnce sync.Once policies []entity.PolicyFrequency // 启用中的频次策略 sgOnce sync.Once signs []string // 签名白名单(账号级;由 Sign 拦截器注入加载函数) } // NewScope 构造(经 Chain.NewScope 生成,db 取链装配库)。 func NewScope(ctx context.Context, db *gorm.DB, accountID int64, mobiles []string) *Scope { return &Scope{ctx: ctx, db: db, accountID: accountID, mobiles: mobiles} } // Whitelisted 白名单豁免判定(mobile IN 一次批查;查询失败=不豁免,与逐号路径语义一致)。 func (sc *Scope) Whitelisted(mobile string) bool { if sc == nil { return false } sc.wlOnce.Do(func() { var rows []string if err := sc.db.WithContext(sc.ctx).Model(&entity.PolicyWhitelist{}). Where("mobile IN ? AND account_id IN ?", sc.mobiles, []int64{0, sc.accountID}). Pluck("mobile", &rows).Error; err != nil { return // 失败=空集(放行,可用性优先,与原 Count 失败路径一致) } sc.wl = make(map[string]struct{}, len(rows)) for _, m := range rows { sc.wl[m] = struct{}{} } }) _, ok := sc.wl[mobile] return ok } // Blacklisted 黑名单 DB 二级判定(批量 IN 一次;命中号由调用方按需回写 Redis 缓存)。 func (sc *Scope) Blacklisted(mobile string) bool { if sc == nil { return false } sc.blOnce.Do(func() { var rows []string if err := sc.db.WithContext(sc.ctx).Model(&entity.PolicyBlacklist{}). Where("mobile IN ? AND account_id IN ?", sc.mobiles, []int64{0, sc.accountID}). Pluck("mobile", &rows).Error; err != nil { return } sc.bl = make(map[string]struct{}, len(rows)) for _, m := range rows { sc.bl[m] = struct{}{} } }) _, ok := sc.bl[mobile] return ok } // RedisBlacklisted 黑名单 Redis 一级判定(SMIsMember 一次批查全量号码;故障=未命中降级 DB)。 func (sc *Scope) RedisBlacklisted(rdb redis.UniversalClient, mobile string) bool { if sc == nil || rdb == nil || len(sc.mobiles) == 0 { return false } sc.rblOnce.Do(func() { hits, err := rdb.SMIsMember(sc.ctx, BlacklistCacheKey, sc.mobiles).Result() if err != nil || len(hits) != len(sc.mobiles) { return // 故障=空集,由 Blacklisted(DB)兜底 } sc.rbl = make(map[string]bool, len(hits)) for i, m := range sc.mobiles { sc.rbl[m] = hits[i] } }) return sc.rbl[mobile] } // FreqPolicies 启用中的频次策略(账号级,一次 Find 跨号复用;查询失败=nil=放行)。 func (sc *Scope) FreqPolicies() []entity.PolicyFrequency { if sc == nil { return nil } sc.plOnce.Do(func() { _ = sc.db.WithContext(sc.ctx). Where("status = 1 AND account_id IN ?", []int64{0, sc.accountID}). Order("id").Find(&sc.policies).Error }) return sc.policies } // AllowedSigns 签名白名单(账号级一次;加载函数由 Sign 拦截器首次使用时注入)。 func (sc *Scope) AllowedSigns(load func() []string) []string { if sc == nil { return load() } sc.sgOnce.Do(func() { sc.signs = load() }) return sc.signs }