feat: 分层轮询、重置日前加密与激活前流量结算
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m37s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m37s
- 轮询配置新增套餐状态、资产状态、重置日前窗口与窗口内流量间隔(迁移 248),间隔三态:正数按间隔、0 不检查、空继承 - 配置按 priority, id 排序合并;在库与已销售无套餐未匹配时告警不入队 - 新增优先级整体重排、匹配预览、立即重算到期时间(异步只提前)、分层统计与重置日前加密统计接口 - 已切换日快照计量的卡流量按观测时间归属,激活前用量只记已结算 - 订单购买与实名激活补齐资产级激活锁,激活前对已切换卡结算一次流量,失败只告警不阻塞,结算不触发通道阈值判定
This commit is contained in:
@@ -25,12 +25,24 @@ type PollingConfigManager struct {
|
||||
mu sync.RWMutex
|
||||
configs []*model.PollingConfig
|
||||
refreshOnce sync.Once
|
||||
tier *TierResolver
|
||||
}
|
||||
|
||||
// SetTierResolver 注入分层事实解析器;未注入时要求套餐状态的配置不匹配。
|
||||
func (m *PollingConfigManager) SetTierResolver(tier *TierResolver) {
|
||||
m.tier = tier
|
||||
}
|
||||
|
||||
// TierResolver 返回已注入的分层事实解析器。
|
||||
func (m *PollingConfigManager) TierResolver() *TierResolver {
|
||||
return m.tier
|
||||
}
|
||||
|
||||
// TaskTypeInterval 某任务类型的轮询间隔信息
|
||||
type TaskTypeInterval struct {
|
||||
Interval int // 秒
|
||||
Priority int // 来源配置的 priority
|
||||
Interval int // 秒
|
||||
Priority int // 来源配置的 priority
|
||||
ConfigID uint // 来源配置 ID
|
||||
}
|
||||
|
||||
// NewPollingConfigManager 创建配置管理器
|
||||
@@ -149,74 +161,158 @@ func (m *PollingConfigManager) MatchConfig(card *model.IotCard) *model.PollingCo
|
||||
return configs[0]
|
||||
}
|
||||
|
||||
// MatchConfigs 按 priority ASC 遍历所有启用的配置,返回所有满足匹配条件且至少有一个非 NULL interval 的配置
|
||||
// 无匹配时返回空切片
|
||||
// MatchConfigs 按 priority、id 升序遍历所有启用的配置,返回满足匹配条件且至少配置了一种检查间隔(含 0 不检查)的配置。
|
||||
// 无匹配时返回空切片。
|
||||
func (m *PollingConfigManager) MatchConfigs(card *model.IotCard) []*model.PollingConfig {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
configs := m.configs
|
||||
m.mu.RUnlock()
|
||||
return m.matchAgainst(configs, card)
|
||||
}
|
||||
|
||||
func (m *PollingConfigManager) matchAgainst(configs []*model.PollingConfig, card *model.IotCard) []*model.PollingConfig {
|
||||
facts := m.lazyTierFacts(card)
|
||||
var result []*model.PollingConfig
|
||||
for _, cfg := range m.configs {
|
||||
if matchConfigConditions(cfg, card) && hasAnyEnabledInterval(cfg) {
|
||||
for _, cfg := range configs {
|
||||
if matchConfigConditions(cfg, card, facts) && hasAnyConfiguredInterval(cfg) {
|
||||
result = append(result, cfg)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// hasAnyEnabledInterval 检查配置是否有至少一个非 NULL 且大于 0 的轮询间隔
|
||||
func hasAnyEnabledInterval(cfg *model.PollingConfig) bool {
|
||||
return (cfg.RealnameCheckInterval != nil && *cfg.RealnameCheckInterval > 0) ||
|
||||
(cfg.CarddataCheckInterval != nil && *cfg.CarddataCheckInterval > 0) ||
|
||||
(cfg.PackageCheckInterval != nil && *cfg.PackageCheckInterval > 0) ||
|
||||
(cfg.ProtectCheckInterval != nil && *cfg.ProtectCheckInterval > 0) ||
|
||||
(cfg.CardStatusCheckInterval != nil && *cfg.CardStatusCheckInterval > 0)
|
||||
// lazyTierFacts 只在某个配置要求套餐状态时才解析卡的分层事实,避免无分层配置时额外查库。
|
||||
func (m *PollingConfigManager) lazyTierFacts(card *model.IotCard) func() TierFacts {
|
||||
var once sync.Once
|
||||
var facts TierFacts
|
||||
return func() TierFacts {
|
||||
once.Do(func() {
|
||||
if m.tier != nil {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
facts = m.tier.Resolve(ctx, card)
|
||||
}
|
||||
})
|
||||
return facts
|
||||
}
|
||||
}
|
||||
|
||||
// MergedTaskIntervals 对所有匹配配置按 priority 遍历,对每种 task type 选取最高优先级的非 nil interval
|
||||
// hasAnyConfiguredInterval 检查配置是否至少配置了一种检查间隔(正数或 0)。
|
||||
func hasAnyConfiguredInterval(cfg *model.PollingConfig) bool {
|
||||
return cfg.RealnameCheckInterval != nil || cfg.CarddataCheckInterval != nil || cfg.PackageCheckInterval != nil ||
|
||||
(cfg.ProtectCheckInterval != nil && *cfg.ProtectCheckInterval > 0) || cfg.CardStatusCheckInterval != nil
|
||||
}
|
||||
|
||||
// IntervalSource 是某检查类型最终采用的间隔及其来源,供合并结果与匹配预览共用。
|
||||
type IntervalSource struct {
|
||||
TaskType string
|
||||
Interval int // 秒;Disabled 为 true 时为 0
|
||||
Disabled bool // 命中取值 0(不检查)
|
||||
ConfigID uint // 来源配置;未匹配时为 0
|
||||
Priority int
|
||||
Config *model.PollingConfig
|
||||
}
|
||||
|
||||
// taskIntervalFields 返回配置中各检查类型的间隔字段(保护期按既有语义只认正数)。
|
||||
func taskIntervalFields(cfg *model.PollingConfig) []struct {
|
||||
taskType string
|
||||
value *int
|
||||
} {
|
||||
protect := cfg.ProtectCheckInterval
|
||||
if protect != nil && *protect <= 0 {
|
||||
protect = nil
|
||||
}
|
||||
return []struct {
|
||||
taskType string
|
||||
value *int
|
||||
}{
|
||||
{constants.TaskTypePollingRealname, cfg.RealnameCheckInterval},
|
||||
{constants.TaskTypePollingCarddata, cfg.CarddataCheckInterval},
|
||||
{constants.TaskTypePollingPackage, cfg.PackageCheckInterval},
|
||||
{constants.TaskTypePollingProtect, protect},
|
||||
{constants.TaskTypePollingCardStatus, cfg.CardStatusCheckInterval},
|
||||
}
|
||||
}
|
||||
|
||||
// ResolveIntervalSources 按三态规则合并匹配配置:每种检查类型取第一个非空取值,正数按间隔、0 不检查且停止向下查找、空继承。
|
||||
func ResolveIntervalSources(configs []*model.PollingConfig) map[string]IntervalSource {
|
||||
result := make(map[string]IntervalSource)
|
||||
for _, cfg := range configs {
|
||||
for _, field := range taskIntervalFields(cfg) {
|
||||
if field.value == nil {
|
||||
continue
|
||||
}
|
||||
if _, exists := result[field.taskType]; exists {
|
||||
continue
|
||||
}
|
||||
source := IntervalSource{TaskType: field.taskType, ConfigID: cfg.ID, Priority: cfg.Priority, Config: cfg}
|
||||
if *field.value <= 0 {
|
||||
source.Disabled = true
|
||||
} else {
|
||||
source.Interval = *field.value
|
||||
}
|
||||
result[field.taskType] = source
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// MergedTaskIntervals 返回卡各检查类型最终采用的正数间隔;取值 0(不检查)的类型不出现在结果中。
|
||||
// 返回 map: taskType → TaskTypeInterval{Interval, Priority}
|
||||
func (m *PollingConfigManager) MergedTaskIntervals(card *model.IotCard) map[string]TaskTypeInterval {
|
||||
configs := m.MatchConfigs(card)
|
||||
merged := make(map[string]TaskTypeInterval)
|
||||
|
||||
for _, cfg := range configs {
|
||||
priority := int(cfg.Priority)
|
||||
if cfg.RealnameCheckInterval != nil && *cfg.RealnameCheckInterval > 0 {
|
||||
taskType := constants.TaskTypePollingRealname
|
||||
if _, exists := merged[taskType]; !exists {
|
||||
merged[taskType] = TaskTypeInterval{Interval: *cfg.RealnameCheckInterval, Priority: priority}
|
||||
}
|
||||
}
|
||||
if cfg.CarddataCheckInterval != nil && *cfg.CarddataCheckInterval > 0 {
|
||||
taskType := constants.TaskTypePollingCarddata
|
||||
if _, exists := merged[taskType]; !exists {
|
||||
merged[taskType] = TaskTypeInterval{Interval: *cfg.CarddataCheckInterval, Priority: priority}
|
||||
}
|
||||
}
|
||||
if cfg.PackageCheckInterval != nil && *cfg.PackageCheckInterval > 0 {
|
||||
taskType := constants.TaskTypePollingPackage
|
||||
if _, exists := merged[taskType]; !exists {
|
||||
merged[taskType] = TaskTypeInterval{Interval: *cfg.PackageCheckInterval, Priority: priority}
|
||||
}
|
||||
}
|
||||
if cfg.ProtectCheckInterval != nil && *cfg.ProtectCheckInterval > 0 {
|
||||
taskType := constants.TaskTypePollingProtect
|
||||
if _, exists := merged[taskType]; !exists {
|
||||
merged[taskType] = TaskTypeInterval{Interval: *cfg.ProtectCheckInterval, Priority: priority}
|
||||
}
|
||||
}
|
||||
if cfg.CardStatusCheckInterval != nil && *cfg.CardStatusCheckInterval > 0 {
|
||||
taskType := constants.TaskTypePollingCardStatus
|
||||
if _, exists := merged[taskType]; !exists {
|
||||
merged[taskType] = TaskTypeInterval{Interval: *cfg.CardStatusCheckInterval, Priority: priority}
|
||||
}
|
||||
sources := ResolveIntervalSources(m.MatchConfigs(card))
|
||||
merged := make(map[string]TaskTypeInterval, len(sources))
|
||||
for taskType, source := range sources {
|
||||
if source.Disabled {
|
||||
continue
|
||||
}
|
||||
merged[taskType] = TaskTypeInterval{Interval: source.Interval, Priority: source.Priority, ConfigID: source.ConfigID}
|
||||
}
|
||||
if len(sources) == 0 {
|
||||
m.warnUnmatchedFallback(card)
|
||||
}
|
||||
return merged
|
||||
}
|
||||
|
||||
// matchConfigConditions 检查卡是否满足配置的匹配条件
|
||||
func matchConfigConditions(cfg *model.PollingConfig, card *model.IotCard) bool {
|
||||
// PreviewIntervalSources 用给定配置集合(可含未保存修改)计算卡的匹配配置与最终间隔,只读。
|
||||
func (m *PollingConfigManager) PreviewIntervalSources(configs []*model.PollingConfig, card *model.IotCard) ([]*model.PollingConfig, map[string]IntervalSource) {
|
||||
matched := m.matchAgainst(configs, card)
|
||||
return matched, ResolveIntervalSources(matched)
|
||||
}
|
||||
|
||||
// EnabledConfigs 返回当前内存中的启用配置快照(已按 priority、id 排序)。
|
||||
func (m *PollingConfigManager) EnabledConfigs() []*model.PollingConfig {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
out := make([]*model.PollingConfig, len(m.configs))
|
||||
copy(out, m.configs)
|
||||
return out
|
||||
}
|
||||
|
||||
// TierFactsOf 返回卡的分层事实(未配置解析器时 Known 为 false)。
|
||||
func (m *PollingConfigManager) TierFactsOf(card *model.IotCard) TierFacts {
|
||||
return m.lazyTierFacts(card)()
|
||||
}
|
||||
|
||||
// warnUnmatchedFallback 已销售无套餐、在库的卡未匹配任何配置时告警:这两类卡依赖卡状态检查兜底开机即停。
|
||||
func (m *PollingConfigManager) warnUnmatchedFallback(card *model.IotCard) {
|
||||
if card == nil || m.logger == nil {
|
||||
return
|
||||
}
|
||||
if card.AssetStatus == constants.AssetStatusInStock {
|
||||
m.logger.Warn("在库卡未匹配任何轮询配置,将不入队", zap.Uint("card_id", card.ID), zap.String("metric", "polling.tier.unmatched_in_stock"))
|
||||
return
|
||||
}
|
||||
if card.AssetStatus == constants.AssetStatusSold {
|
||||
facts := m.TierFactsOf(card)
|
||||
if facts.Known && facts.PackageState == constants.PollingPackageStateNone {
|
||||
m.logger.Warn("已销售无套餐卡未匹配任何轮询配置,将不入队", zap.Uint("card_id", card.ID), zap.String("metric", "polling.tier.unmatched_sold_no_package"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// matchConfigConditions 检查卡是否满足配置的匹配条件;套餐状态仅在配置要求时解析,解析失败视为不匹配。
|
||||
func matchConfigConditions(cfg *model.PollingConfig, card *model.IotCard, facts func() TierFacts) bool {
|
||||
if cfg.CardCondition != "" {
|
||||
if cfg.CardCondition != getCardCondition(card) {
|
||||
return false
|
||||
@@ -232,9 +328,23 @@ func matchConfigConditions(cfg *model.PollingConfig, card *model.IotCard) bool {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if cfg.AssetStatus != nil && *cfg.AssetStatus != card.AssetStatus {
|
||||
return false
|
||||
}
|
||||
if cfg.PackageState != nil && *cfg.PackageState != "" {
|
||||
f := facts()
|
||||
if !f.Known || f.PackageState != *cfg.PackageState {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// CardConditionOf 返回卡用于匹配轮询配置的状态条件(not_real_name / activated / suspended)。
|
||||
func CardConditionOf(card *model.IotCard) string {
|
||||
return getCardCondition(card)
|
||||
}
|
||||
|
||||
// getCardCondition 获取卡的状态条件(用于匹配轮询配置)
|
||||
// ⚠️ 注意判断顺序:停机优先,避免停机卡错误匹配 activated 或 not_real_name 配置
|
||||
// 停机卡需要继续轮询 carddata/package 以检测复机条件(套餐购买、流量重置、实名完成)
|
||||
|
||||
@@ -257,6 +257,10 @@ func (p *PollingInitializer) initBatch(ctx context.Context, cards []*model.IotCa
|
||||
cmdCount = 0
|
||||
}
|
||||
|
||||
// 分层轮询按批预取套餐状态,避免逐卡查库。
|
||||
if tier := p.configMgr.TierResolver(); tier != nil {
|
||||
tier.Prefill(ctx, cards)
|
||||
}
|
||||
for _, card := range cards {
|
||||
intervals := p.configMgr.MergedTaskIntervals(card)
|
||||
if len(intervals) == 0 {
|
||||
@@ -271,6 +275,10 @@ func (p *PollingInitializer) initBatch(ctx context.Context, cards []*model.IotCa
|
||||
for taskType, info := range intervals {
|
||||
lastCheckAt := lastCheckAtByTaskType(card, taskType)
|
||||
nextCheck := calculateNextCheckTime(lastCheckAt, info.Interval, now)
|
||||
// 重置日前加密:窗口规则算出的时间更早时(如需截断到窗口起点)以它为准。
|
||||
if windowDue := p.configMgr.NextCheckAt(ctx, card, taskType, info, now); windowDue.Before(nextCheck) && windowDue.After(now) {
|
||||
nextCheck = windowDue
|
||||
}
|
||||
pipe.ZAdd(ctx, constants.RedisPollingShardQueueKey(shardID, taskType), redis.Z{
|
||||
Score: float64(nextCheck.Unix()), Member: cardIDStr,
|
||||
})
|
||||
@@ -289,7 +297,8 @@ func (p *PollingInitializer) initBatch(ctx context.Context, cards []*model.IotCa
|
||||
"last_gateway_reading_mb": card.LastGatewayReadingMB,
|
||||
"data_usage_mb": card.DataUsageMB,
|
||||
"stop_reason": card.StopReason, "is_standalone": boolToStr(card.IsStandalone),
|
||||
"cached_at": now.Unix(),
|
||||
"asset_status": card.AssetStatus,
|
||||
"cached_at": now.Unix(),
|
||||
}
|
||||
if card.CurrentMonthStartDate != nil {
|
||||
cacheData["current_month_start_date"] = card.CurrentMonthStartDate.Unix()
|
||||
|
||||
130
internal/polling/pre_reset.go
Normal file
130
internal/polling/pre_reset.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package polling
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
// shanghaiLocation 返回业务时区(上海)。
|
||||
func shanghaiLocation() *time.Location {
|
||||
location, err := time.LoadLocation("Asia/Shanghai")
|
||||
if err != nil {
|
||||
return time.FixedZone("CST", 8*3600)
|
||||
}
|
||||
return location
|
||||
}
|
||||
|
||||
// nextResetAt 返回 now 之后最近一次流量重置时刻:重置日当天 0 点(上海时区)。
|
||||
func nextResetAt(now time.Time, resetDay int) time.Time {
|
||||
if resetDay < 1 || resetDay > 28 {
|
||||
resetDay = 1
|
||||
}
|
||||
local := now.In(shanghaiLocation())
|
||||
candidate := time.Date(local.Year(), local.Month(), resetDay, 0, 0, 0, 0, shanghaiLocation())
|
||||
if !candidate.After(local) {
|
||||
candidate = candidate.AddDate(0, 1, 0)
|
||||
}
|
||||
return candidate
|
||||
}
|
||||
|
||||
// PreResetNextDue 计算重置日前加密后的流量检查下次到期时间。
|
||||
// - 当前在窗口 [reset-window, reset) 内:下次 = now + 窗口间隔;越过重置时刻则改为 reset - 收尾提前量,已过则按正常间隔;
|
||||
// - 当前在窗口前、按正常间隔算出的下次越过窗口起点:截断到窗口起点,保证按时进入窗口;
|
||||
// - 其余按正常间隔。
|
||||
func PreResetNextDue(now time.Time, normalInterval, window, windowInterval time.Duration, reset time.Time, tail time.Duration) time.Time {
|
||||
normal := now.Add(normalInterval)
|
||||
windowStart := reset.Add(-window)
|
||||
if !now.Before(windowStart) && now.Before(reset) {
|
||||
next := now.Add(windowInterval)
|
||||
if next.Before(reset) {
|
||||
return next
|
||||
}
|
||||
last := reset.Add(-tail)
|
||||
if last.After(now) {
|
||||
return last
|
||||
}
|
||||
return normal
|
||||
}
|
||||
if now.Before(windowStart) && normal.After(windowStart) {
|
||||
return windowStart
|
||||
}
|
||||
return normal
|
||||
}
|
||||
|
||||
// NextCheckAt 返回卡某检查类型的下次到期时间:流量检查在来源配置开启重置日前加密时按窗口规则计算,其余为 now + 间隔。
|
||||
func (m *PollingConfigManager) NextCheckAt(ctx context.Context, card *model.IotCard, taskType string, info TaskTypeInterval, now time.Time) time.Time {
|
||||
normal := now.Add(time.Duration(info.Interval) * time.Second)
|
||||
if taskType != constants.TaskTypePollingCarddata || card == nil || info.ConfigID == 0 {
|
||||
return normal
|
||||
}
|
||||
cfg := m.configByID(info.ConfigID)
|
||||
if cfg == nil || cfg.PreResetWindow == nil || cfg.PreResetCarddataInterval == nil {
|
||||
return normal
|
||||
}
|
||||
resetDay := 1
|
||||
if m.tier != nil {
|
||||
resetDay = m.tier.ResetDay(ctx, card.CarrierID)
|
||||
}
|
||||
return PreResetNextDue(now, time.Duration(info.Interval)*time.Second,
|
||||
time.Duration(*cfg.PreResetWindow)*time.Second, time.Duration(*cfg.PreResetCarddataInterval)*time.Second,
|
||||
nextResetAt(now, resetDay), constants.PollingPreResetTailMinutes*time.Minute)
|
||||
}
|
||||
|
||||
func (m *PollingConfigManager) configByID(id uint) *model.PollingConfig {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
for _, cfg := range m.configs {
|
||||
if cfg.ID == id {
|
||||
return cfg
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RecordPreResetCheck 在流量查询成功后记录重置日前加密效果:处于该卡来源配置的加密窗口内时,
|
||||
// 把卡记入窗口已查集合,并记下本次查询距重置时刻的分钟数(同卡取最后一次)。统计失败不影响轮询。
|
||||
func (m *PollingConfigManager) RecordPreResetCheck(ctx context.Context, card *model.IotCard, now time.Time) {
|
||||
if m == nil || m.redis == nil || card == nil {
|
||||
return
|
||||
}
|
||||
info, ok := m.MergedTaskIntervals(card)[constants.TaskTypePollingCarddata]
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
cfg := m.configByID(info.ConfigID)
|
||||
if cfg == nil || cfg.PreResetWindow == nil || cfg.PreResetCarddataInterval == nil {
|
||||
return
|
||||
}
|
||||
resetDay := 1
|
||||
if m.tier != nil {
|
||||
resetDay = m.tier.ResetDay(ctx, card.CarrierID)
|
||||
}
|
||||
reset := nextResetAt(now, resetDay)
|
||||
if now.Before(reset.Add(-time.Duration(*cfg.PreResetWindow) * time.Second)) {
|
||||
return
|
||||
}
|
||||
date := reset.Format("20060102")
|
||||
member := strconv.FormatUint(uint64(card.ID), 10)
|
||||
minutes := int(reset.Sub(now).Minutes())
|
||||
ttl := constants.PollingPreResetStatTTLSeconds * time.Second
|
||||
pipe := m.redis.Pipeline()
|
||||
pipe.PFAdd(ctx, constants.RedisPollingPreResetCheckedKey(card.CarrierID, date), member)
|
||||
pipe.Expire(ctx, constants.RedisPollingPreResetCheckedKey(card.CarrierID, date), ttl)
|
||||
pipe.HSet(ctx, constants.RedisPollingPreResetOffsetKey(card.CarrierID, date), member, minutes)
|
||||
pipe.Expire(ctx, constants.RedisPollingPreResetOffsetKey(card.CarrierID, date), ttl)
|
||||
_, _ = pipe.Exec(ctx)
|
||||
}
|
||||
|
||||
// ResetMoments 返回某运营商账户最近一次已过去的重置时刻与下一次重置时刻。
|
||||
func (m *PollingConfigManager) ResetMoments(ctx context.Context, carrierID uint, now time.Time) (time.Time, time.Time) {
|
||||
resetDay := 1
|
||||
if m.tier != nil {
|
||||
resetDay = m.tier.ResetDay(ctx, carrierID)
|
||||
}
|
||||
next := nextResetAt(now, resetDay)
|
||||
return next.AddDate(0, -1, 0), next
|
||||
}
|
||||
129
internal/polling/recalculate.go
Normal file
129
internal/polling/recalculate.go
Normal file
@@ -0,0 +1,129 @@
|
||||
package polling
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
// recalculateBatchSize 是重算到期时间每批读取的卡数。
|
||||
const recalculateBatchSize = 500
|
||||
|
||||
// RecalculateProgress 是重算到期时间任务的进度快照。
|
||||
type RecalculateProgress struct {
|
||||
Total int
|
||||
Processed int
|
||||
Advanced int // 被提前的检查项数
|
||||
}
|
||||
|
||||
// DueRecalculator 按当前轮询配置重算已在队列中的卡的下次到期时间:新到期 = min(当前到期, 上次检查 + 新间隔),只提前不推后。
|
||||
// 只更新已在分片队列中的成员(ZADD XX LT),不新增成员,避免把执行中的卡重复放回队列;不调用网关。
|
||||
type DueRecalculator struct {
|
||||
db *gorm.DB
|
||||
redis *redis.Client
|
||||
configMgr *PollingConfigManager
|
||||
queueMgr *PollingQueueManager
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// NewDueRecalculator 创建重算器。
|
||||
func NewDueRecalculator(db *gorm.DB, redisClient *redis.Client, configMgr *PollingConfigManager, queueMgr *PollingQueueManager, logger *zap.Logger) *DueRecalculator {
|
||||
if logger == nil {
|
||||
logger = zap.NewNop()
|
||||
}
|
||||
return &DueRecalculator{db: db, redis: redisClient, configMgr: configMgr, queueMgr: queueMgr, logger: logger}
|
||||
}
|
||||
|
||||
// Run 执行重算;carrierID 非空时只处理该运营商账户的卡。每处理一批回调一次进度。
|
||||
func (r *DueRecalculator) Run(ctx context.Context, carrierID *uint, report func(RecalculateProgress)) (RecalculateProgress, error) {
|
||||
var progress RecalculateProgress
|
||||
if r == nil || r.db == nil || r.redis == nil || r.configMgr == nil || r.queueMgr == nil {
|
||||
return progress, fmt.Errorf("重算到期时间能力未配置")
|
||||
}
|
||||
if err := r.configMgr.Load(ctx); err != nil {
|
||||
return progress, err
|
||||
}
|
||||
base := r.db.WithContext(ctx).Model(&model.IotCard{}).Where("enable_polling = true AND deleted_at IS NULL")
|
||||
if carrierID != nil {
|
||||
base = base.Where("carrier_id = ?", *carrierID)
|
||||
}
|
||||
var total int64
|
||||
if err := base.Count(&total).Error; err != nil {
|
||||
return progress, err
|
||||
}
|
||||
progress.Total = int(total)
|
||||
if report != nil {
|
||||
report(progress)
|
||||
}
|
||||
var lastID uint
|
||||
for {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return progress, err
|
||||
}
|
||||
var cards []*model.IotCard
|
||||
query := r.db.WithContext(ctx).Where("id > ? AND enable_polling = true AND deleted_at IS NULL", lastID)
|
||||
if carrierID != nil {
|
||||
query = query.Where("carrier_id = ?", *carrierID)
|
||||
}
|
||||
if err := query.Order("id ASC").Limit(recalculateBatchSize).Find(&cards).Error; err != nil {
|
||||
return progress, err
|
||||
}
|
||||
if len(cards) == 0 {
|
||||
break
|
||||
}
|
||||
lastID = cards[len(cards)-1].ID
|
||||
advanced, err := r.recalculateBatch(ctx, cards)
|
||||
if err != nil {
|
||||
return progress, err
|
||||
}
|
||||
progress.Processed += len(cards)
|
||||
progress.Advanced += advanced
|
||||
if report != nil {
|
||||
report(progress)
|
||||
}
|
||||
}
|
||||
return progress, nil
|
||||
}
|
||||
|
||||
func (r *DueRecalculator) recalculateBatch(ctx context.Context, cards []*model.IotCard) (int, error) {
|
||||
if tier := r.configMgr.TierResolver(); tier != nil {
|
||||
tier.Prefill(ctx, cards)
|
||||
}
|
||||
now := time.Now()
|
||||
pipe := r.redis.Pipeline()
|
||||
var cmds []*redis.IntCmd
|
||||
for _, card := range cards {
|
||||
shardID := int(card.ID) % r.queueMgr.shardCount
|
||||
member := strconv.FormatUint(uint64(card.ID), 10)
|
||||
for taskType, info := range r.configMgr.MergedTaskIntervals(card) {
|
||||
due := calculateNextCheckTime(lastCheckAtByTaskType(card, taskType), info.Interval, now)
|
||||
if windowDue := r.configMgr.NextCheckAt(ctx, card, taskType, info, now); windowDue.Before(due) && windowDue.After(now) {
|
||||
due = windowDue
|
||||
}
|
||||
cmds = append(cmds, pipe.ZAddArgs(ctx, constants.RedisPollingShardQueueKey(shardID, taskType), redis.ZAddArgs{
|
||||
XX: true, LT: true, Ch: true, Members: []redis.Z{{Score: float64(due.Unix()), Member: member}},
|
||||
}))
|
||||
}
|
||||
}
|
||||
if len(cmds) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
if _, err := pipe.Exec(ctx); err != nil && err != redis.Nil {
|
||||
return 0, err
|
||||
}
|
||||
advanced := 0
|
||||
for _, cmd := range cmds {
|
||||
if n, err := cmd.Result(); err == nil {
|
||||
advanced += int(n)
|
||||
}
|
||||
}
|
||||
return advanced, nil
|
||||
}
|
||||
83
internal/polling/recalculate_task.go
Normal file
83
internal/polling/recalculate_task.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package polling
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/bytedance/sonic"
|
||||
"github.com/hibiken/asynq"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
// RecalculateDuePayload 是重算到期时间异步任务的载荷。
|
||||
type RecalculateDuePayload struct {
|
||||
TaskID string `json:"task_id"`
|
||||
CarrierID *uint `json:"carrier_id,omitempty"`
|
||||
}
|
||||
|
||||
// RecalculateTaskHandler 是重算到期时间的 Asynq 任务处理器;进度写入 Redis 供后台查询。
|
||||
type RecalculateTaskHandler struct {
|
||||
recalculator *DueRecalculator
|
||||
redis *redis.Client
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// NewRecalculateTaskHandler 创建重算任务处理器。
|
||||
func NewRecalculateTaskHandler(recalculator *DueRecalculator, redisClient *redis.Client, logger *zap.Logger) *RecalculateTaskHandler {
|
||||
if logger == nil {
|
||||
logger = zap.NewNop()
|
||||
}
|
||||
return &RecalculateTaskHandler{recalculator: recalculator, redis: redisClient, logger: logger}
|
||||
}
|
||||
|
||||
// Handle 执行重算;失败写入进度并返回 SkipRetry,避免按指数退避反复全量扫描。
|
||||
func (h *RecalculateTaskHandler) Handle(ctx context.Context, task *asynq.Task) error {
|
||||
var payload RecalculateDuePayload
|
||||
if err := sonic.Unmarshal(task.Payload(), &payload); err != nil || payload.TaskID == "" {
|
||||
return asynq.SkipRetry
|
||||
}
|
||||
key := constants.RedisPollingRecalcProgressKey(payload.TaskID)
|
||||
h.write(key, map[string]any{"status": constants.PollingRecalcStatusRunning, "started_at": time.Now().Format(time.RFC3339)})
|
||||
progress, err := h.recalculator.Run(ctx, payload.CarrierID, func(p RecalculateProgress) {
|
||||
h.write(key, map[string]any{"total": p.Total, "processed": p.Processed, "advanced": p.Advanced})
|
||||
})
|
||||
fields := map[string]any{"total": progress.Total, "processed": progress.Processed, "advanced": progress.Advanced, "finished_at": time.Now().Format(time.RFC3339)}
|
||||
if err != nil {
|
||||
fields["status"] = constants.PollingRecalcStatusFailed
|
||||
fields["error"] = "重算到期时间失败,请稍后重试"
|
||||
h.write(key, fields)
|
||||
h.logger.Error("重算轮询到期时间失败", zap.String("task_id", payload.TaskID), zap.Error(err))
|
||||
return asynq.SkipRetry
|
||||
}
|
||||
fields["status"] = constants.PollingRecalcStatusCompleted
|
||||
h.write(key, fields)
|
||||
h.logger.Info("重算轮询到期时间完成", zap.String("task_id", payload.TaskID),
|
||||
zap.Int("processed", progress.Processed), zap.Int("advanced", progress.Advanced))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *RecalculateTaskHandler) write(key string, fields map[string]any) {
|
||||
if h.redis == nil {
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
pipe := h.redis.Pipeline()
|
||||
pipe.HSet(ctx, key, fields)
|
||||
pipe.Expire(ctx, key, constants.PollingRecalcProgressTTLSeconds*time.Second)
|
||||
if _, err := pipe.Exec(ctx); err != nil {
|
||||
h.logger.Warn("写重算到期时间进度失败", zap.String("key", key), zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
// carrierText 把可选运营商账户转为进度展示文本。
|
||||
func carrierText(carrierID *uint) string {
|
||||
if carrierID == nil {
|
||||
return ""
|
||||
}
|
||||
return strconv.FormatUint(uint64(*carrierID), 10)
|
||||
}
|
||||
168
internal/polling/tier.go
Normal file
168
internal/polling/tier.go
Normal file
@@ -0,0 +1,168 @@
|
||||
package polling
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// TierFacts 是分层轮询匹配所需、卡表之外的业务事实。
|
||||
type TierFacts struct {
|
||||
PackageState string // 套餐状态,见 constants.PollingPackageState*
|
||||
Known bool // 是否成功解析;解析失败时要求套餐状态的配置不匹配
|
||||
}
|
||||
|
||||
type tierCacheEntry struct {
|
||||
facts TierFacts
|
||||
loadedAt time.Time
|
||||
}
|
||||
|
||||
// TierResolver 计算卡的套餐状态(绑定设备的卡按设备、独立卡按卡,与停复机"是否有有效套餐"口径一致),
|
||||
// 并读取运营商账户的流量重置日。结果在进程内短时缓存,层级最迟在缓存过期后的下一次执行时更新。
|
||||
type TierResolver struct {
|
||||
usage *postgres.PackageUsageStore
|
||||
bindings *postgres.DeviceSimBindingStore
|
||||
carriers *postgres.CarrierStore
|
||||
logger *zap.Logger
|
||||
|
||||
mu sync.Mutex
|
||||
cache map[uint]tierCacheEntry
|
||||
resetDays map[uint]int
|
||||
resetsAt time.Time
|
||||
}
|
||||
|
||||
// NewTierResolver 创建分层事实解析器。
|
||||
func NewTierResolver(usage *postgres.PackageUsageStore, bindings *postgres.DeviceSimBindingStore, carriers *postgres.CarrierStore, logger *zap.Logger) *TierResolver {
|
||||
if logger == nil {
|
||||
logger = zap.NewNop()
|
||||
}
|
||||
return &TierResolver{usage: usage, bindings: bindings, carriers: carriers, logger: logger, cache: map[uint]tierCacheEntry{}, resetDays: map[uint]int{}}
|
||||
}
|
||||
|
||||
// Resolve 返回卡的分层事实;缓存未过期时直接返回。
|
||||
func (r *TierResolver) Resolve(ctx context.Context, card *model.IotCard) TierFacts {
|
||||
if r == nil || card == nil {
|
||||
return TierFacts{}
|
||||
}
|
||||
if facts, ok := r.cached(card.ID); ok {
|
||||
return facts
|
||||
}
|
||||
r.Prefill(ctx, []*model.IotCard{card})
|
||||
facts, _ := r.cached(card.ID)
|
||||
return facts
|
||||
}
|
||||
|
||||
// Prefill 批量解析一组卡的分层事实并写入缓存,供全量初始化等批处理避免逐卡查库。
|
||||
func (r *TierResolver) Prefill(ctx context.Context, cards []*model.IotCard) {
|
||||
if r == nil || r.usage == nil || len(cards) == 0 {
|
||||
return
|
||||
}
|
||||
var standaloneIDs, boundCardIDs []uint
|
||||
for _, card := range cards {
|
||||
if card == nil {
|
||||
continue
|
||||
}
|
||||
if card.IsStandalone {
|
||||
standaloneIDs = append(standaloneIDs, card.ID)
|
||||
} else {
|
||||
boundCardIDs = append(boundCardIDs, card.ID)
|
||||
}
|
||||
}
|
||||
results := make(map[uint]TierFacts, len(cards))
|
||||
if len(standaloneIDs) > 0 {
|
||||
states, err := r.usage.PackageStatesByCarriers(ctx, constants.AssetTypeIotCard, standaloneIDs)
|
||||
if err != nil {
|
||||
r.logger.Warn("批量查询卡套餐状态失败,相关卡本次不按套餐状态匹配", zap.Error(err))
|
||||
}
|
||||
for _, id := range standaloneIDs {
|
||||
if err == nil {
|
||||
results[id] = TierFacts{PackageState: states[id], Known: true}
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(boundCardIDs) > 0 && r.bindings != nil {
|
||||
bindings, err := r.bindings.GetActiveBindingsByCardIDs(ctx, boundCardIDs)
|
||||
if err != nil {
|
||||
r.logger.Warn("批量查询卡绑定设备失败,相关卡本次不按套餐状态匹配", zap.Error(err))
|
||||
} else {
|
||||
deviceOf := make(map[uint]uint, len(bindings))
|
||||
deviceIDs := make([]uint, 0, len(bindings))
|
||||
for _, binding := range bindings {
|
||||
deviceOf[binding.IotCardID] = binding.DeviceID
|
||||
deviceIDs = append(deviceIDs, binding.DeviceID)
|
||||
}
|
||||
deviceStates, devErr := r.usage.PackageStatesByCarriers(ctx, constants.AssetTypeDevice, deviceIDs)
|
||||
cardStates, cardErr := r.usage.PackageStatesByCarriers(ctx, constants.AssetTypeIotCard, boundCardIDs)
|
||||
for _, id := range boundCardIDs {
|
||||
// 有有效设备绑定按设备计算;绑定已失效的卡按卡自身计算(与 resolvePackageCarrier 口径一致)。
|
||||
if deviceID, bound := deviceOf[id]; bound {
|
||||
if devErr == nil {
|
||||
results[id] = TierFacts{PackageState: deviceStates[deviceID], Known: true}
|
||||
}
|
||||
} else if cardErr == nil {
|
||||
results[id] = TierFacts{PackageState: cardStates[id], Known: true}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
now := time.Now()
|
||||
r.mu.Lock()
|
||||
for _, card := range cards {
|
||||
if card == nil {
|
||||
continue
|
||||
}
|
||||
r.cache[card.ID] = tierCacheEntry{facts: results[card.ID], loadedAt: now}
|
||||
}
|
||||
r.mu.Unlock()
|
||||
}
|
||||
|
||||
// Invalidate 清除某卡的分层事实缓存(套餐变化后可调用,未调用时最迟缓存过期后更新)。
|
||||
func (r *TierResolver) Invalidate(cardID uint) {
|
||||
if r == nil {
|
||||
return
|
||||
}
|
||||
r.mu.Lock()
|
||||
delete(r.cache, cardID)
|
||||
r.mu.Unlock()
|
||||
}
|
||||
|
||||
func (r *TierResolver) cached(cardID uint) (TierFacts, bool) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
entry, ok := r.cache[cardID]
|
||||
if !ok || time.Since(entry.loadedAt) > constants.PollingTierFactsCacheSeconds*time.Second {
|
||||
return TierFacts{}, false
|
||||
}
|
||||
return entry.facts, true
|
||||
}
|
||||
|
||||
// ResetDay 返回运营商账户的流量重置日(1~28),缓存 10 分钟;读取失败按 1 号。
|
||||
func (r *TierResolver) ResetDay(ctx context.Context, carrierID uint) int {
|
||||
if r == nil || r.carriers == nil {
|
||||
return 1
|
||||
}
|
||||
r.mu.Lock()
|
||||
if time.Since(r.resetsAt) > 10*time.Minute {
|
||||
r.resetDays = map[uint]int{}
|
||||
r.resetsAt = time.Now()
|
||||
}
|
||||
day, ok := r.resetDays[carrierID]
|
||||
r.mu.Unlock()
|
||||
if ok {
|
||||
return day
|
||||
}
|
||||
day = r.carriers.GetDataResetDay(ctx, carrierID)
|
||||
if day < 1 || day > 28 {
|
||||
day = 1
|
||||
}
|
||||
r.mu.Lock()
|
||||
r.resetDays[carrierID] = day
|
||||
r.mu.Unlock()
|
||||
return day
|
||||
}
|
||||
Reference in New Issue
Block a user