feat: 按运营商账户调度的分发器、卡任务执行器与模式切换
- 四个轮询处理器拆为步骤函数 Step(返回结果分类),旧 Asynq Handle 只做并发控制与原有重入队映射,行为不变;carrier 模式下旧 handler 直接结束
- 网关限流增加等待模式(ctx 标记),NSQ 消费者等名额到单步超时,超时按限流处理不计失败;记录限流等待次数
- 卡任务执行器:核对 token 并抢执行权,按配置步骤顺序串行执行到期检查(单步超时、总超时、panic 恢复、流量已触发评估时跳过套餐),执行中续期并 Touch,续期失败立即停止;收尾写回到期、连续失败、退避、延迟确认与告警
- 分发器:每秒按账户抢分发锁领取(执行上限按消费能力收紧、优先占比、按限流组剩余 QPS 估算),发布到 polling.card.{账户},发布失败释放;消费旧优先提示与手动触发;心跳与处理中标记到期计数
- NSQ 生产者与按账户动态订阅的消费者管理器,优雅退出宽限期
- 失败告警异步写 tb_polling_failure_alert(同卡同类型一条未处理,成功转已恢复),账户窗口内告警数超阈值产生账户级告警
- 模式监听:切 carrier 时旧分片调度停止出队,加锁初始化新结构后开始领取并订阅;回 legacy 时停止消费、重建旧分片队列;生命周期(API 与 Worker)在 carrier 模式写账户队列
This commit is contained in:
199
internal/polling/carrier_bridge.go
Normal file
199
internal/polling/carrier_bridge.go
Normal file
@@ -0,0 +1,199 @@
|
||||
package polling
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/polling/carrierqueue"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
// CarrierScheduling 是按运营商账户调度在轮询包内的接缝:判断当前模式,并按卡计算各检查类型到期时间写入账户队列。
|
||||
// 所有写入轮询结构的入口(初始化、生命周期、优先提示、手动触发)按当前模式写新结构或旧分片结构。
|
||||
type CarrierScheduling struct {
|
||||
store *carrierqueue.Store
|
||||
settings *carrierqueue.SettingsProvider
|
||||
configMgr *PollingConfigManager
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// NewCarrierScheduling 创建按运营商账户调度接缝。
|
||||
func NewCarrierScheduling(store *carrierqueue.Store, settings *carrierqueue.SettingsProvider, configMgr *PollingConfigManager, logger *zap.Logger) *CarrierScheduling {
|
||||
if logger == nil {
|
||||
logger = zap.NewNop()
|
||||
}
|
||||
return &CarrierScheduling{store: store, settings: settings, configMgr: configMgr, logger: logger}
|
||||
}
|
||||
|
||||
// Active 判断当前是否为按运营商账户调度。
|
||||
func (c *CarrierScheduling) Active(ctx context.Context) bool {
|
||||
return c != nil && c.store != nil && c.settings != nil && c.settings.Current(ctx).CarrierMode()
|
||||
}
|
||||
|
||||
// Store 返回账户队列存储。
|
||||
func (c *CarrierScheduling) Store() *carrierqueue.Store {
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
return c.store
|
||||
}
|
||||
|
||||
// Settings 返回调度参数提供者。
|
||||
func (c *CarrierScheduling) Settings() *carrierqueue.SettingsProvider {
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
return c.settings
|
||||
}
|
||||
|
||||
// DuesForCard 按卡匹配的轮询配置计算各检查类型下次到期时间。
|
||||
// fromLastCheck 为 true 时按各类型上次检查时间计算(初始化);否则按当前时间加抖动(新卡、恢复轮询、状态变化)。
|
||||
// 间隔为 0 或未配置的类型不返回;重置日前加密窗口规则算出的时间更早时以它为准。
|
||||
func (c *CarrierScheduling) DuesForCard(ctx context.Context, card *model.IotCard, fromLastCheck bool) map[string]time.Time {
|
||||
dues := map[string]time.Time{}
|
||||
if c == nil || c.configMgr == nil || card == nil {
|
||||
return dues
|
||||
}
|
||||
now := time.Now()
|
||||
for taskType, info := range c.configMgr.MergedTaskIntervals(card) {
|
||||
checkType := constants.CheckTypeFromTaskType(taskType)
|
||||
if checkType == "" || info.Interval <= 0 {
|
||||
continue
|
||||
}
|
||||
var due time.Time
|
||||
if fromLastCheck {
|
||||
due = calculateNextCheckTime(lastCheckAtByTaskType(card, taskType), info.Interval, now)
|
||||
if windowDue := c.configMgr.NextCheckAt(ctx, card, taskType, info, now); windowDue.Before(due) && windowDue.After(now) {
|
||||
due = windowDue
|
||||
}
|
||||
} else {
|
||||
due = calcInitialDelay(info.Interval)
|
||||
}
|
||||
dues[checkType] = due
|
||||
}
|
||||
return dues
|
||||
}
|
||||
|
||||
// UpsertCard 把卡写入所属账户队列;fromLastCheck 含义同 DuesForCard。
|
||||
func (c *CarrierScheduling) UpsertCard(ctx context.Context, card *model.IotCard, fromLastCheck bool) {
|
||||
if c == nil || c.store == nil || card == nil {
|
||||
return
|
||||
}
|
||||
if err := c.store.Upsert(ctx, card.ID, card.CarrierID, c.DuesForCard(ctx, card, fromLastCheck)); err != nil {
|
||||
c.logger.Warn("写入运营商账户轮询队列失败", zap.Uint("card_id", card.ID), zap.Uint("carrier_id", card.CarrierID), zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
// RemoveCard 把卡移出所属账户队列;carrierID 为 0 时按到期信息中记录的账户移出。
|
||||
func (c *CarrierScheduling) RemoveCard(ctx context.Context, cardID, carrierID uint) {
|
||||
if c == nil || c.store == nil {
|
||||
return
|
||||
}
|
||||
if carrierID == 0 {
|
||||
due, err := c.store.ReadDue(ctx, cardID)
|
||||
if err != nil || due.CarrierID == 0 {
|
||||
return
|
||||
}
|
||||
carrierID = due.CarrierID
|
||||
}
|
||||
if err := c.store.Remove(ctx, cardID, carrierID); err != nil {
|
||||
c.logger.Warn("移出运营商账户轮询队列失败", zap.Uint("card_id", cardID), zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
// RequestTaskType 把卡的某检查类型(旧任务类型 polling:xxx)设为立即到期放入账户优先队列(优先提示、手动触发)。
|
||||
// 卡不在轮询中时不处理。
|
||||
func (c *CarrierScheduling) RequestTaskType(ctx context.Context, cardID, carrierID uint, taskType string) {
|
||||
checkType := constants.CheckTypeFromTaskType(taskType)
|
||||
if c == nil || c.store == nil || checkType == "" {
|
||||
return
|
||||
}
|
||||
if _, err := c.store.RequestCheck(ctx, carrierqueue.CheckRequest{CardID: cardID, CarrierID: carrierID, CheckTypes: []string{checkType}}); err != nil {
|
||||
c.logger.Warn("写入运营商账户优先队列失败", zap.Uint("card_id", cardID), zap.String("task_type", taskType), zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
// CarrierInitializer 从数据库按批初始化全部运营商账户队列与卡到期信息;进度与旧初始化器同结构,供 init-progress 展示。
|
||||
type CarrierInitializer struct {
|
||||
scheduling *CarrierScheduling
|
||||
iotCardStore *postgres.IotCardStore
|
||||
logger *zap.Logger
|
||||
|
||||
mu sync.RWMutex
|
||||
progress InitProgress
|
||||
running atomic.Bool
|
||||
}
|
||||
|
||||
// NewCarrierInitializer 创建新结构初始化器。
|
||||
func NewCarrierInitializer(scheduling *CarrierScheduling, iotCardStore *postgres.IotCardStore, logger *zap.Logger) *CarrierInitializer {
|
||||
if logger == nil {
|
||||
logger = zap.NewNop()
|
||||
}
|
||||
return &CarrierInitializer{scheduling: scheduling, iotCardStore: iotCardStore, logger: logger}
|
||||
}
|
||||
|
||||
// Progress 返回初始化进度快照。
|
||||
func (i *CarrierInitializer) Progress() InitProgress {
|
||||
i.mu.RLock()
|
||||
defer i.mu.RUnlock()
|
||||
return i.progress
|
||||
}
|
||||
|
||||
// Run 同步执行一次全量初始化:按批读取启用轮询的卡,计算各类型到期并写入账户队列。返回写入的卡数。
|
||||
func (i *CarrierInitializer) Run(ctx context.Context) (int64, error) {
|
||||
if !i.running.CompareAndSwap(false, true) {
|
||||
return 0, nil
|
||||
}
|
||||
defer i.running.Store(false)
|
||||
started := time.Now()
|
||||
total, err := i.iotCardStore.CountForPolling(ctx)
|
||||
if err != nil {
|
||||
i.setProgress(func(p *InitProgress) { p.Status, p.ErrorMessage = "failed", err.Error() })
|
||||
return 0, err
|
||||
}
|
||||
i.setProgress(func(p *InitProgress) {
|
||||
*p = InitProgress{TotalCards: total, StartTime: started, Status: "running"}
|
||||
})
|
||||
const batchSize = 2000
|
||||
var lastID uint
|
||||
var written int64
|
||||
for {
|
||||
if err := ctx.Err(); err != nil {
|
||||
i.setProgress(func(p *InitProgress) { p.Status, p.ErrorMessage = "failed", err.Error() })
|
||||
return written, err
|
||||
}
|
||||
cards, fetchErr := i.iotCardStore.ListForPollingBatch(ctx, lastID, batchSize)
|
||||
if fetchErr != nil {
|
||||
i.setProgress(func(p *InitProgress) { p.Status, p.ErrorMessage = "failed", fetchErr.Error() })
|
||||
return written, fetchErr
|
||||
}
|
||||
if len(cards) == 0 {
|
||||
break
|
||||
}
|
||||
if tier := i.scheduling.configMgr.TierResolver(); tier != nil {
|
||||
tier.Prefill(ctx, cards)
|
||||
}
|
||||
for _, card := range cards {
|
||||
i.scheduling.UpsertCard(ctx, card, true)
|
||||
written++
|
||||
}
|
||||
lastID = cards[len(cards)-1].ID
|
||||
loaded := written
|
||||
i.setProgress(func(p *InitProgress) { p.LoadedCards, p.LastBatchTime = loaded, time.Now() })
|
||||
}
|
||||
i.setProgress(func(p *InitProgress) { p.Status = "completed" })
|
||||
i.logger.Info("运营商账户轮询结构初始化完成", zap.Int64("cards", written), zap.Duration("duration", time.Since(started)))
|
||||
return written, nil
|
||||
}
|
||||
|
||||
func (i *CarrierInitializer) setProgress(update func(*InitProgress)) {
|
||||
i.mu.Lock()
|
||||
update(&i.progress)
|
||||
i.mu.Unlock()
|
||||
}
|
||||
177
internal/polling/carrierqueue/alerts.go
Normal file
177
internal/polling/carrierqueue/alerts.go
Normal file
@@ -0,0 +1,177 @@
|
||||
package carrierqueue
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
// FailureEvent 是一次需要告警的检查失败。
|
||||
type FailureEvent struct {
|
||||
CardID uint
|
||||
ICCID string
|
||||
CarrierID uint
|
||||
CheckType string
|
||||
Category string
|
||||
Message string
|
||||
FailCount int
|
||||
FirstFailed time.Time
|
||||
At time.Time
|
||||
}
|
||||
|
||||
type alertOp struct {
|
||||
failure *FailureEvent
|
||||
recovered bool
|
||||
cardID uint
|
||||
checkType string
|
||||
}
|
||||
|
||||
// AlertWriter 异步写卡轮询失败告警:同一卡同一检查类型只保留一条未处理告警,后续失败更新计数;
|
||||
// 首次成功转为已恢复;同账户窗口内新增告警超阈值时产生账户级告警。写入失败只记日志,不影响轮询。
|
||||
type AlertWriter struct {
|
||||
db *gorm.DB
|
||||
redis *redis.Client
|
||||
settings *SettingsProvider
|
||||
logger *zap.Logger
|
||||
ch chan alertOp
|
||||
}
|
||||
|
||||
// NewAlertWriter 创建告警写入器;调用 Start 后开始消费。
|
||||
func NewAlertWriter(db *gorm.DB, redisClient *redis.Client, settings *SettingsProvider, logger *zap.Logger) *AlertWriter {
|
||||
if logger == nil {
|
||||
logger = zap.NewNop()
|
||||
}
|
||||
return &AlertWriter{db: db, redis: redisClient, settings: settings, logger: logger, ch: make(chan alertOp, 2048)}
|
||||
}
|
||||
|
||||
// Start 启动后台写入协程,直到 ctx 结束。
|
||||
func (w *AlertWriter) Start(ctx context.Context) {
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case op := <-w.ch:
|
||||
w.apply(op)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// ReportFailure 提交一条失败告警(达到阈值后才调用)。
|
||||
func (w *AlertWriter) ReportFailure(event FailureEvent) {
|
||||
w.offer(alertOp{failure: &event})
|
||||
}
|
||||
|
||||
// ReportRecovered 提交一次恢复:该卡该检查类型的未处理告警转为已恢复。
|
||||
func (w *AlertWriter) ReportRecovered(cardID uint, checkType string) {
|
||||
w.offer(alertOp{recovered: true, cardID: cardID, checkType: checkType})
|
||||
}
|
||||
|
||||
func (w *AlertWriter) offer(op alertOp) {
|
||||
if w == nil {
|
||||
return
|
||||
}
|
||||
select {
|
||||
case w.ch <- op:
|
||||
default:
|
||||
w.logger.Warn("卡轮询失败告警队列已满,丢弃本次写入", zap.String("metric", "polling.failure_alert.dropped"))
|
||||
}
|
||||
}
|
||||
|
||||
func (w *AlertWriter) apply(op alertOp) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if op.recovered {
|
||||
err := w.db.WithContext(ctx).Exec(`UPDATE tb_polling_failure_alert SET status = ?, recovered_at = NOW(), updated_at = NOW()
|
||||
WHERE alert_level = ? AND card_id = ? AND check_type = ? AND status = ?`,
|
||||
constants.PollingAlertStatusRecovered, constants.PollingAlertLevelCard, op.cardID, op.checkType, constants.PollingAlertStatusOpen).Error
|
||||
if err != nil {
|
||||
w.logger.Warn("卡轮询告警转为已恢复失败", zap.Uint("card_id", op.cardID), zap.String("check_type", op.checkType), zap.Error(err))
|
||||
}
|
||||
return
|
||||
}
|
||||
event := op.failure
|
||||
var inserted bool
|
||||
err := w.db.WithContext(ctx).Raw(`INSERT INTO tb_polling_failure_alert
|
||||
(alert_level, card_id, iccid, carrier_id, check_type, error_category, fail_count, first_failed_at, last_failed_at, last_error, status)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT (alert_level, card_id, carrier_id, check_type) WHERE status = 1
|
||||
DO UPDATE SET fail_count = EXCLUDED.fail_count, error_category = EXCLUDED.error_category,
|
||||
last_failed_at = EXCLUDED.last_failed_at, last_error = EXCLUDED.last_error, updated_at = NOW()
|
||||
RETURNING (xmax = 0)`,
|
||||
constants.PollingAlertLevelCard, event.CardID, event.ICCID, event.CarrierID, event.CheckType, event.Category, event.FailCount,
|
||||
nullableTime(event.FirstFailed), event.At, truncate(event.Message, 500), constants.PollingAlertStatusOpen).Row().Scan(&inserted)
|
||||
if err != nil {
|
||||
w.logger.Warn("写卡轮询失败告警失败", zap.Uint("card_id", event.CardID), zap.String("check_type", event.CheckType), zap.Error(err))
|
||||
return
|
||||
}
|
||||
if !inserted {
|
||||
return
|
||||
}
|
||||
w.logger.Error("卡轮询持续失败告警", zap.Uint("card_id", event.CardID), zap.String("iccid", event.ICCID),
|
||||
zap.Uint("carrier_id", event.CarrierID), zap.String("check_type", event.CheckType), zap.String("category", event.Category),
|
||||
zap.Int("fail_count", event.FailCount), zap.String("metric", "polling.failure_alert.opened"))
|
||||
w.checkAccountAlert(ctx, event.CarrierID)
|
||||
}
|
||||
|
||||
// checkAccountAlert 同账户窗口内新增卡级告警数超过阈值时,产生(或更新)一条账户级告警。
|
||||
func (w *AlertWriter) checkAccountAlert(ctx context.Context, carrierID uint) {
|
||||
settings := w.settings.Current(ctx)
|
||||
window, limit := settings.AccountAlertWindow, settings.AccountAlertLimit
|
||||
if window <= 0 || limit <= 0 || w.redis == nil {
|
||||
return
|
||||
}
|
||||
now := time.Now()
|
||||
key := constants.RedisCarrierAccountAlertKey(carrierID)
|
||||
member := strconv.FormatInt(now.UnixNano(), 36)
|
||||
pipe := w.redis.TxPipeline()
|
||||
pipe.ZAdd(ctx, key, redis.Z{Score: float64(now.UnixMilli()), Member: member})
|
||||
pipe.ZRemRangeByScore(ctx, key, "-inf", strconv.FormatInt(now.Add(-window).UnixMilli(), 10))
|
||||
count := pipe.ZCard(ctx, key)
|
||||
pipe.Expire(ctx, key, window*2)
|
||||
if _, err := pipe.Exec(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
if int(count.Val()) <= limit {
|
||||
return
|
||||
}
|
||||
var inserted bool
|
||||
err := w.db.WithContext(ctx).Raw(`INSERT INTO tb_polling_failure_alert
|
||||
(alert_level, card_id, carrier_id, check_type, error_category, fail_count, first_failed_at, last_failed_at, last_error, status)
|
||||
VALUES (?, 0, ?, ?, '', ?, ?, ?, ?, ?)
|
||||
ON CONFLICT (alert_level, card_id, carrier_id, check_type) WHERE status = 1
|
||||
DO UPDATE SET fail_count = EXCLUDED.fail_count, last_failed_at = EXCLUDED.last_failed_at, updated_at = NOW()
|
||||
RETURNING (xmax = 0)`,
|
||||
constants.PollingAlertLevelCarrier, carrierID, constants.PollingAlertCheckTypeAll, count.Val(), now, now,
|
||||
"窗口内同账户新增失败告警数超过阈值,通常是账户额度、凭证或上游故障,请联系上游处理账户", constants.PollingAlertStatusOpen).Row().Scan(&inserted)
|
||||
if err != nil {
|
||||
w.logger.Warn("写账户级轮询告警失败", zap.Uint("carrier_id", carrierID), zap.Error(err))
|
||||
return
|
||||
}
|
||||
if inserted {
|
||||
w.logger.Error("运营商账户轮询失败告警数超过阈值", zap.Uint("carrier_id", carrierID), zap.Int64("count", count.Val()),
|
||||
zap.Duration("window", window), zap.String("metric", "polling.failure_alert.carrier_opened"))
|
||||
}
|
||||
}
|
||||
|
||||
func nullableTime(t time.Time) any {
|
||||
if t.IsZero() {
|
||||
return nil
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func truncate(s string, max int) string {
|
||||
runes := []rune(s)
|
||||
if len(runes) <= max {
|
||||
return s
|
||||
}
|
||||
return string(runes[:max])
|
||||
}
|
||||
326
internal/polling/carrierqueue/dispatcher.go
Normal file
326
internal/polling/carrierqueue/dispatcher.go
Normal file
@@ -0,0 +1,326 @@
|
||||
package carrierqueue
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
stderrors "errors"
|
||||
"strconv"
|
||||
"sync"
|
||||
"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/config"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
// CardMessage 是卡任务 NSQ 消息体。
|
||||
type CardMessage struct {
|
||||
CardID uint `json:"card_id"`
|
||||
CarrierID uint `json:"carrier_id"`
|
||||
Token string `json:"token"`
|
||||
DispatchedAt int64 `json:"dispatched_at"`
|
||||
}
|
||||
|
||||
// Publisher 发布 NSQ 消息。
|
||||
type Publisher interface {
|
||||
Publish(topic string, body []byte) error
|
||||
}
|
||||
|
||||
// BudgetProvider 返回账户各查询操作所属限流组本秒剩余名额;limited=false 表示不按 QPS 限制领取。
|
||||
type BudgetProvider interface {
|
||||
Budget(ctx context.Context, carrierID uint, ops []string) (groups map[string]int, opGroup map[string]string, limited bool)
|
||||
}
|
||||
|
||||
// CarrierParams 是一个运营商账户生效的调度参数(已合并全局默认)。
|
||||
type CarrierParams struct {
|
||||
InflightLimit int
|
||||
CardMaxInFlight int
|
||||
PriorityRatio float64
|
||||
Configured bool // 是否有该账户的配置行
|
||||
}
|
||||
|
||||
// carrierEntry 是分发器缓存的账户信息。
|
||||
type carrierEntry struct {
|
||||
id uint
|
||||
realnameControl bool
|
||||
}
|
||||
|
||||
// Dispatcher 每个分发间隔对每个运营商账户抢分发锁、领取到期卡并发布到账户 topic。多实例安全。
|
||||
type Dispatcher struct {
|
||||
db *gorm.DB
|
||||
store *Store
|
||||
settings *SettingsProvider
|
||||
publisher Publisher
|
||||
budgets BudgetProvider
|
||||
cfg config.CarrierSchedulerConfig
|
||||
instanceID string
|
||||
logger *zap.Logger
|
||||
|
||||
mu sync.Mutex
|
||||
carriers []carrierEntry
|
||||
params map[uint]model.CarrierPollingParam
|
||||
loadedAt time.Time
|
||||
tightened map[uint]time.Time
|
||||
}
|
||||
|
||||
// NewDispatcher 创建分发器。
|
||||
func NewDispatcher(db *gorm.DB, store *Store, settings *SettingsProvider, publisher Publisher, budgets BudgetProvider,
|
||||
cfg config.CarrierSchedulerConfig, instanceID string, logger *zap.Logger) *Dispatcher {
|
||||
if logger == nil {
|
||||
logger = zap.NewNop()
|
||||
}
|
||||
return &Dispatcher{db: db, store: store, settings: settings, publisher: publisher, budgets: budgets, cfg: cfg,
|
||||
instanceID: instanceID, logger: logger, params: map[uint]model.CarrierPollingParam{}, tightened: map[uint]time.Time{}}
|
||||
}
|
||||
|
||||
// Run 按分发间隔循环,直到 ctx 结束。
|
||||
func (d *Dispatcher) Run(ctx context.Context) {
|
||||
interval := d.cfg.DispatchInterval
|
||||
if interval <= 0 {
|
||||
interval = time.Second
|
||||
}
|
||||
ticker := time.NewTicker(interval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
d.tick(ctx, interval)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Ready 判断新调度是否可领取:模式为 carrier 且新结构初始化已完成。
|
||||
func (d *Dispatcher) Ready(ctx context.Context) bool {
|
||||
if !d.settings.Current(ctx).CarrierMode() {
|
||||
return false
|
||||
}
|
||||
state, err := d.store.redis.Get(ctx, constants.RedisCarrierSchedulerInitStateKey).Result()
|
||||
return err == nil && state == InitStateDone
|
||||
}
|
||||
|
||||
func (d *Dispatcher) tick(ctx context.Context, interval time.Duration) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
d.logger.Error("分发器 panic,已恢复", zap.Any("panic", r))
|
||||
}
|
||||
}()
|
||||
if !d.Ready(ctx) {
|
||||
return
|
||||
}
|
||||
d.store.redis.HSet(ctx, constants.RedisCarrierSchedulerHeartbeatKey, d.instanceID, time.Now().UnixMilli())
|
||||
d.drainLegacyHints(ctx)
|
||||
d.refreshCarriers(ctx)
|
||||
d.mu.Lock()
|
||||
carriers := append([]carrierEntry(nil), d.carriers...)
|
||||
d.mu.Unlock()
|
||||
lockTTL := interval * 9 / 10
|
||||
if lockTTL <= 0 {
|
||||
lockTTL = 900 * time.Millisecond
|
||||
}
|
||||
for _, carrier := range carriers {
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
ok, err := d.store.redis.SetNX(ctx, constants.RedisCarrierDispatchLockKey(carrier.id), d.instanceID, lockTTL).Result()
|
||||
if err != nil || !ok {
|
||||
continue
|
||||
}
|
||||
d.dispatchCarrier(ctx, carrier)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Dispatcher) dispatchCarrier(ctx context.Context, carrier carrierEntry) {
|
||||
params := d.ParamsFor(carrier.id)
|
||||
limit := params.InflightLimit
|
||||
// 执行上限不得大于订阅进程 max_in_flight 之和,否则领出的消息在 NSQ 中排队到租约过期;不满足时收紧本轮上限并上报。
|
||||
processes := d.cfg.SubscriberProcesses
|
||||
if processes <= 0 {
|
||||
processes = 1
|
||||
}
|
||||
if capacity := params.CardMaxInFlight * processes; capacity > 0 && limit > capacity {
|
||||
limit = capacity
|
||||
d.reportTightened(carrier.id, params.InflightLimit, capacity)
|
||||
}
|
||||
input := ClaimInput{
|
||||
CarrierID: carrier.id, Lease: d.lease(), InflightLimit: limit, PriorityRatio: params.PriorityRatio,
|
||||
TokenPrefix: d.instanceID + ":" + strconv.FormatInt(time.Now().UnixNano(), 36),
|
||||
CountRealname: carrier.realnameControl,
|
||||
}
|
||||
if d.budgets != nil {
|
||||
ops := []string{constants.GatewayOpQueryStatus, constants.GatewayOpQueryFlow, constants.GatewayOpQueryRealname}
|
||||
if groups, opGroup, limited := d.budgets.Budget(ctx, carrier.id, ops); limited {
|
||||
input.GroupBudget = groups
|
||||
input.TypeGroup = map[string]string{
|
||||
constants.CheckTypeCardStatus: opGroup[constants.GatewayOpQueryStatus],
|
||||
constants.CheckTypeCarddata: opGroup[constants.GatewayOpQueryFlow],
|
||||
constants.CheckTypeRealname: opGroup[constants.GatewayOpQueryRealname],
|
||||
}
|
||||
}
|
||||
}
|
||||
claimed, expired, err := d.store.Claim(ctx, input)
|
||||
if err != nil {
|
||||
d.logger.Warn("运营商账户领取失败", zap.Uint("carrier_id", carrier.id), zap.Error(err))
|
||||
return
|
||||
}
|
||||
if expired > 0 {
|
||||
minute := strconv.FormatInt(time.Now().Unix()/60, 10)
|
||||
key := constants.RedisCarrierExpiredClaimsKey(carrier.id)
|
||||
d.store.redis.HIncrBy(ctx, key, minute, expired)
|
||||
d.store.redis.Expire(ctx, key, 2*time.Hour)
|
||||
d.logger.Warn("运营商账户处理中标记到期(执行崩溃或超时)", zap.Uint("carrier_id", carrier.id), zap.Int64("count", expired),
|
||||
zap.String("metric", "polling.carrier.claim_expired"))
|
||||
}
|
||||
topic := constants.NSQCardTopic(carrier.id)
|
||||
for _, item := range claimed {
|
||||
body, _ := json.Marshal(CardMessage{CardID: item.CardID, CarrierID: carrier.id, Token: item.Token, DispatchedAt: time.Now().UnixMilli()})
|
||||
if err := d.publisher.Publish(topic, body); err != nil {
|
||||
// 发布失败:用独立短超时上下文按 token 删除处理中标记并恢复 score;恢复也失败则等租约到期后自然重新领取。
|
||||
releaseCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 2*time.Second)
|
||||
if releaseErr := d.store.Release(releaseCtx, item.CardID, carrier.id, item.Token); releaseErr != nil {
|
||||
d.logger.Warn("发布失败后释放处理中标记失败", zap.Uint("card_id", item.CardID), zap.Error(releaseErr))
|
||||
}
|
||||
cancel()
|
||||
d.logger.Warn("发布卡任务失败,已释放", zap.Uint("card_id", item.CardID), zap.String("topic", topic), zap.Error(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Dispatcher) lease() time.Duration {
|
||||
if d.cfg.Lease > 0 {
|
||||
return d.cfg.Lease
|
||||
}
|
||||
return 5 * time.Minute
|
||||
}
|
||||
|
||||
func (d *Dispatcher) reportTightened(carrierID uint, configured, capacity int) {
|
||||
d.mu.Lock()
|
||||
last := d.tightened[carrierID]
|
||||
if time.Since(last) < 10*time.Minute {
|
||||
d.mu.Unlock()
|
||||
return
|
||||
}
|
||||
d.tightened[carrierID] = time.Now()
|
||||
d.mu.Unlock()
|
||||
d.logger.Warn("账户执行上限大于订阅进程 max_in_flight 之和,已按消费能力收紧本轮领取上限",
|
||||
zap.Uint("carrier_id", carrierID), zap.Int("inflight_limit", configured), zap.Int("capacity", capacity),
|
||||
zap.String("metric", "polling.carrier.inflight_tightened"))
|
||||
}
|
||||
|
||||
// refreshCarriers 每 10 秒重新读取运营商账户与调度参数。
|
||||
func (d *Dispatcher) refreshCarriers(ctx context.Context) {
|
||||
d.mu.Lock()
|
||||
fresh := time.Since(d.loadedAt) < 10*time.Second
|
||||
d.mu.Unlock()
|
||||
if fresh {
|
||||
return
|
||||
}
|
||||
var carriers []model.Carrier
|
||||
if err := d.db.WithContext(ctx).Select("id", "realname_control_enabled").Order("id").Find(&carriers).Error; err != nil {
|
||||
d.logger.Warn("读取运营商账户失败,沿用上一次列表", zap.Error(err))
|
||||
return
|
||||
}
|
||||
var rows []model.CarrierPollingParam
|
||||
if err := d.db.WithContext(ctx).Find(&rows).Error; err != nil {
|
||||
d.logger.Warn("读取运营商账户调度参数失败,沿用上一次参数", zap.Error(err))
|
||||
return
|
||||
}
|
||||
entries := make([]carrierEntry, 0, len(carriers))
|
||||
for _, carrier := range carriers {
|
||||
entries = append(entries, carrierEntry{id: carrier.ID, realnameControl: carrier.RealnameControlEnabled == 1})
|
||||
}
|
||||
params := make(map[uint]model.CarrierPollingParam, len(rows))
|
||||
for _, row := range rows {
|
||||
params[row.CarrierID] = row
|
||||
}
|
||||
d.mu.Lock()
|
||||
d.carriers, d.params, d.loadedAt = entries, params, time.Now()
|
||||
d.mu.Unlock()
|
||||
}
|
||||
|
||||
// CarrierIDs 返回当前缓存的运营商账户 ID(消费者据此订阅)。
|
||||
func (d *Dispatcher) CarrierIDs(ctx context.Context) []uint {
|
||||
d.refreshCarriers(ctx)
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
ids := make([]uint, 0, len(d.carriers))
|
||||
for _, carrier := range d.carriers {
|
||||
ids = append(ids, carrier.id)
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
// ParamsFor 返回账户生效调度参数(未配置项用全局默认)。
|
||||
func (d *Dispatcher) ParamsFor(carrierID uint) CarrierParams {
|
||||
d.mu.Lock()
|
||||
row, ok := d.params[carrierID]
|
||||
d.mu.Unlock()
|
||||
return MergeParams(row, ok, d.cfg)
|
||||
}
|
||||
|
||||
// MergeParams 合并账户配置行与全局默认。
|
||||
func MergeParams(row model.CarrierPollingParam, configured bool, cfg config.CarrierSchedulerConfig) CarrierParams {
|
||||
params := CarrierParams{InflightLimit: cfg.DefaultInflightLimit, CardMaxInFlight: cfg.DefaultCardMaxInFlight, Configured: configured}
|
||||
if params.InflightLimit <= 0 {
|
||||
params.InflightLimit = 20
|
||||
}
|
||||
if params.CardMaxInFlight <= 0 {
|
||||
params.CardMaxInFlight = 20
|
||||
}
|
||||
if !configured {
|
||||
return params
|
||||
}
|
||||
if row.InflightLimit != nil && *row.InflightLimit > 0 {
|
||||
params.InflightLimit = *row.InflightLimit
|
||||
}
|
||||
if row.CardMaxInFlight != nil && *row.CardMaxInFlight > 0 {
|
||||
params.CardMaxInFlight = *row.CardMaxInFlight
|
||||
}
|
||||
if row.PriorityMaxRatio != nil && *row.PriorityMaxRatio > 0 {
|
||||
params.PriorityRatio = *row.PriorityMaxRatio
|
||||
}
|
||||
return params
|
||||
}
|
||||
|
||||
// drainLegacyHints 消费旧优先提示通道与手动触发队列:优先提示放入账户优先队列,手动触发在普通队列中立即到期。
|
||||
func (d *Dispatcher) drainLegacyHints(ctx context.Context) {
|
||||
for _, checkType := range constants.AllCheckTypes() {
|
||||
taskType := constants.TaskTypeFromCheckType(checkType)
|
||||
d.drainList(ctx, constants.RedisPollingPriorityQueueKey(taskType), checkType, false)
|
||||
d.drainList(ctx, constants.RedisPollingManualQueueKey(taskType), checkType, true)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Dispatcher) drainList(ctx context.Context, key, checkType string, normal bool) {
|
||||
for i := 0; i < 500; i++ {
|
||||
raw, err := d.store.redis.LPop(ctx, key).Result()
|
||||
if err != nil {
|
||||
if !stderrors.Is(err, redis.Nil) {
|
||||
d.logger.Warn("读取旧提示队列失败", zap.String("key", key), zap.Error(err))
|
||||
}
|
||||
return
|
||||
}
|
||||
cardID, convErr := strconv.ParseUint(raw, 10, 64)
|
||||
if convErr != nil {
|
||||
continue
|
||||
}
|
||||
due, dueErr := d.store.ReadDue(ctx, uint(cardID))
|
||||
if dueErr != nil || due.CarrierID == 0 {
|
||||
continue
|
||||
}
|
||||
if _, err := d.store.RequestCheck(ctx, CheckRequest{CardID: uint(cardID), CarrierID: due.CarrierID, CheckTypes: []string{checkType}, Normal: normal}); err != nil {
|
||||
d.logger.Warn("写入账户队列失败", zap.Uint64("card_id", cardID), zap.Error(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 新结构初始化状态(constants.RedisCarrierSchedulerInitStateKey)。
|
||||
const (
|
||||
InitStateRunning = "running"
|
||||
InitStateDone = "done"
|
||||
)
|
||||
@@ -182,7 +182,8 @@ return 1
|
||||
// 只作用于仍在轮询中的卡(在优先或普通队列中)。冷却期内(最近成功距今未超过冷却)的类型不请求。
|
||||
//
|
||||
// KEYS[1] due;KEYS[2] 优先队列;KEYS[3] 普通队列;KEYS[4] claim。
|
||||
// ARGV:now_ms、card_id、cooldown_ms(0 不冷却)、确认延迟(秒,逗号分隔,可空)、期望结果(可空)、类型…。
|
||||
// ARGV:now_ms、card_id、cooldown_ms(0 不冷却)、确认延迟(秒,逗号分隔,可空)、期望结果(可空)、
|
||||
// 是否放入优先队列(1/0;0 用于手动触发:只把到期设为现在并在普通队列中立即到期)、类型…。
|
||||
// 返回本次实际请求的类型数;卡不在轮询中返回 -1。
|
||||
const requestCheckLua = `
|
||||
local now = tonumber(ARGV[1])
|
||||
@@ -192,7 +193,8 @@ local inPriority = redis.call('ZSCORE', KEYS[2], cardId)
|
||||
local inQueue = redis.call('ZSCORE', KEYS[3], cardId)
|
||||
if not inPriority and not inQueue then return -1 end
|
||||
local requested = 0
|
||||
for i = 6, #ARGV do
|
||||
local toPriority = ARGV[6] == '1'
|
||||
for i = 7, #ARGV do
|
||||
local t = ARGV[i]
|
||||
local skip = false
|
||||
if cooldown > 0 then
|
||||
@@ -209,6 +211,11 @@ for i = 6, #ARGV do
|
||||
end
|
||||
end
|
||||
if requested == 0 then return 0 end
|
||||
if not toPriority then
|
||||
if redis.call('EXISTS', KEYS[4]) == 1 then return requested end
|
||||
if inQueue then redis.call('ZADD', KEYS[3], now, cardId) else redis.call('ZADD', KEYS[2], now, cardId) end
|
||||
return requested
|
||||
end
|
||||
redis.call('HSET', KEYS[1], 'prio', '1')
|
||||
if redis.call('EXISTS', KEYS[4]) == 1 then return requested end
|
||||
redis.call('ZREM', KEYS[3], cardId)
|
||||
|
||||
@@ -10,7 +10,9 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/infrastructure/systemconfig"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
@@ -376,3 +378,15 @@ func (p *SettingsProvider) load(ctx context.Context) (Settings, error) {
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// NewDefaultSettingsProvider 用独立注册表与读取器创建调度参数提供者(API 与 Worker 共用同一份数据库与缓存数据)。
|
||||
func NewDefaultSettingsProvider(db *gorm.DB, redisClient *redis.Client, logger *zap.Logger) *SettingsProvider {
|
||||
registry := systemconfig.NewRegistry()
|
||||
RegisterConfigDefinitions(registry, logger)
|
||||
var cache systemconfig.Cache
|
||||
if redisClient != nil {
|
||||
cache = systemconfig.NewRedisCache(redisClient)
|
||||
}
|
||||
reader := systemconfig.NewReader(db, registry, cache, systemconfig.NewLogAlertSink(logger))
|
||||
return NewSettingsProvider(reader, logger)
|
||||
}
|
||||
|
||||
@@ -136,6 +136,8 @@ type CheckRequest struct {
|
||||
Cooldown time.Duration
|
||||
ConfirmDelays []time.Duration
|
||||
Expected string // 延迟确认的期望结果(card_status 为 online / offline),满足时取消后续确认
|
||||
// Normal 为 true 时不放入优先队列:只把到期设为现在、在普通队列中立即到期(手动触发走普通通道)。
|
||||
Normal bool
|
||||
}
|
||||
|
||||
// RequestCheck 把卡的指定检查类型设为立即到期放入优先队列;返回实际请求的类型数,卡不在轮询中返回 -1。
|
||||
@@ -147,7 +149,11 @@ func (s *Store) RequestCheck(ctx context.Context, request CheckRequest) (int64,
|
||||
for _, delay := range request.ConfirmDelays {
|
||||
confirm = append(confirm, strconv.FormatInt(int64(delay/time.Second), 10))
|
||||
}
|
||||
args := []any{time.Now().UnixMilli(), request.CardID, request.Cooldown.Milliseconds(), strings.Join(confirm, ","), request.Expected}
|
||||
toPriority := "1"
|
||||
if request.Normal {
|
||||
toPriority = "0"
|
||||
}
|
||||
args := []any{time.Now().UnixMilli(), request.CardID, request.Cooldown.Milliseconds(), strings.Join(confirm, ","), request.Expected, toPriority}
|
||||
for _, checkType := range request.CheckTypes {
|
||||
args = append(args, checkType)
|
||||
}
|
||||
|
||||
@@ -22,6 +22,18 @@ type PollingLifecycleService struct {
|
||||
deviceBindingStore *postgres.DeviceSimBindingStore
|
||||
deviceStore *postgres.DeviceStore
|
||||
logger *zap.Logger
|
||||
// carrier 按运营商账户调度接缝;当前模式为 carrier 时生命周期事件写入账户队列,不写旧分片队列。
|
||||
carrier *CarrierScheduling
|
||||
}
|
||||
|
||||
// SetCarrierScheduling 注入按运营商账户调度接缝。
|
||||
func (s *PollingLifecycleService) SetCarrierScheduling(carrier *CarrierScheduling) {
|
||||
s.carrier = carrier
|
||||
}
|
||||
|
||||
// carrierActive 判断当前是否按运营商账户调度。
|
||||
func (s *PollingLifecycleService) carrierActive(ctx context.Context) bool {
|
||||
return s.carrier.Active(ctx)
|
||||
}
|
||||
|
||||
// NewPollingLifecycleService 创建卡生命周期轮询管理服务
|
||||
@@ -52,6 +64,10 @@ func (s *PollingLifecycleService) OnCardCreated(ctx context.Context, card *model
|
||||
s.logger.Debug("卡禁用轮询,跳过入队", zap.Uint("card_id", card.ID))
|
||||
return
|
||||
}
|
||||
if s.carrierActive(ctx) {
|
||||
s.carrier.UpsertCard(ctx, card, false)
|
||||
return
|
||||
}
|
||||
s.enqueueCard(ctx, card)
|
||||
}
|
||||
|
||||
@@ -64,6 +80,11 @@ func (s *PollingLifecycleService) OnBatchCardsCreated(ctx context.Context, cards
|
||||
|
||||
// OnCardStatusChanged 卡状态变化后清理缓存、重新匹配配置并更新队列
|
||||
func (s *PollingLifecycleService) OnCardStatusChanged(ctx context.Context, cardID uint) {
|
||||
if s.carrierActive(ctx) {
|
||||
s.queueMgr.InvalidateCardCache(ctx, cardID)
|
||||
s.carrierRefresh(ctx, cardID)
|
||||
return
|
||||
}
|
||||
if err := s.queueMgr.RemoveFromAllQueues(ctx, cardID); err != nil {
|
||||
s.logger.Warn("卡状态变化:从队列移除失败", zap.Uint("card_id", cardID), zap.Error(err))
|
||||
}
|
||||
@@ -87,6 +108,13 @@ func (s *PollingLifecycleService) OnBatchCardsStatusChanged(ctx context.Context,
|
||||
if len(cardIDs) == 0 {
|
||||
return
|
||||
}
|
||||
if s.carrierActive(ctx) {
|
||||
for _, cardID := range cardIDs {
|
||||
s.queueMgr.InvalidateCardCache(ctx, cardID)
|
||||
s.carrierRefresh(ctx, cardID)
|
||||
}
|
||||
return
|
||||
}
|
||||
// 单次 pipeline:批量从当前分片队列移除并清理缓存
|
||||
if err := s.queueMgr.RemoveBatchFromCurrentShardQueues(ctx, cardIDs); err != nil {
|
||||
s.logger.Warn("批量卡状态变化:从队列移除失败", zap.Int("count", len(cardIDs)), zap.Error(err))
|
||||
@@ -107,6 +135,11 @@ func (s *PollingLifecycleService) OnBatchCardsStatusChanged(ctx context.Context,
|
||||
|
||||
// OnCardDeleted 卡删除后移除所有队列并清理缓存
|
||||
func (s *PollingLifecycleService) OnCardDeleted(ctx context.Context, cardID uint) {
|
||||
if s.carrierActive(ctx) {
|
||||
s.carrier.RemoveCard(ctx, cardID, 0)
|
||||
s.queueMgr.InvalidateCardCache(ctx, cardID)
|
||||
return
|
||||
}
|
||||
if err := s.queueMgr.OnCardDeleted(ctx, cardID); err != nil {
|
||||
s.logger.Warn("卡删除:清理队列和缓存失败", zap.Uint("card_id", cardID), zap.Error(err))
|
||||
}
|
||||
@@ -123,11 +156,21 @@ func (s *PollingLifecycleService) OnCardEnabled(ctx context.Context, cardID uint
|
||||
if !s.ShouldEnqueue(ctx, card) {
|
||||
return
|
||||
}
|
||||
if s.carrierActive(ctx) {
|
||||
// 恢复轮询时各检查类型到期设为当前时间加抖动。
|
||||
s.carrier.UpsertCard(ctx, card, false)
|
||||
return
|
||||
}
|
||||
s.enqueueCard(ctx, card)
|
||||
}
|
||||
|
||||
// OnCardDisabled 卡禁用轮询后移除所有队列
|
||||
func (s *PollingLifecycleService) OnCardDisabled(ctx context.Context, cardID uint) {
|
||||
if s.carrierActive(ctx) {
|
||||
s.carrier.RemoveCard(ctx, cardID, 0)
|
||||
s.queueMgr.InvalidateCardCache(ctx, cardID)
|
||||
return
|
||||
}
|
||||
if err := s.queueMgr.RemoveFromAllQueues(ctx, cardID); err != nil {
|
||||
s.logger.Warn("卡禁用:从队列移除失败", zap.Uint("card_id", cardID), zap.Error(err))
|
||||
}
|
||||
@@ -183,3 +226,17 @@ func calcInitialDelay(interval int) time.Time {
|
||||
}
|
||||
return now.Add(time.Duration(rand.Intn(jitterMax)) * time.Second)
|
||||
}
|
||||
|
||||
// carrierRefresh 按运营商账户调度时重新匹配配置并写入账户队列:不再轮询的卡移出,其余按当前时间加抖动重排。
|
||||
func (s *PollingLifecycleService) carrierRefresh(ctx context.Context, cardID uint) {
|
||||
card, err := s.iotCardStore.GetByID(ctx, cardID)
|
||||
if err != nil {
|
||||
s.logger.Error("卡状态变化:加载卡信息失败", zap.Uint("card_id", cardID), zap.Error(err))
|
||||
return
|
||||
}
|
||||
if !s.ShouldEnqueue(ctx, card) {
|
||||
s.carrier.RemoveCard(ctx, card.ID, card.CarrierID)
|
||||
return
|
||||
}
|
||||
s.carrier.UpsertCard(ctx, card, false)
|
||||
}
|
||||
|
||||
@@ -30,6 +30,8 @@ type Scheduler struct {
|
||||
packageActivationHandler *PackageActivationHandler
|
||||
dataResetHandler *DataResetHandler
|
||||
initializer *PollingInitializer // 可选,nil 表示不守卫 Init 完成
|
||||
// carrier 按运营商账户调度接缝:当前模式为 carrier 时旧分片调度停止出队(套餐激活检查照常执行)。
|
||||
carrier *CarrierScheduling
|
||||
|
||||
stopChan chan struct{}
|
||||
wg sync.WaitGroup
|
||||
@@ -156,6 +158,10 @@ func (s *Scheduler) scheduleLoop(ctx context.Context) {
|
||||
// processShardSchedule 处理手动队列和分片定时队列(每 1 秒触发)
|
||||
// 使用 90% 的 tick 间隔作为超时,确保单分片 Redis 挂起时不阻塞下一个 tick
|
||||
func (s *Scheduler) processShardSchedule(ctx context.Context) {
|
||||
if s.carrier.Active(ctx) {
|
||||
// 已切到按运营商账户调度:旧分片调度停止出队,优先提示与手动触发由新分发器消费。
|
||||
return
|
||||
}
|
||||
// 优先提示与手动队列都不受 Init 影响(ConfigManager 已就绪即可)。
|
||||
// 同一调度周期内先排空优先提示、再排空手动触发队列:优先的强度边界仅为调度入口领先,
|
||||
// 提示排空不参与分片背压判断,因此分片积压不会抑制加急入队。
|
||||
@@ -296,3 +302,8 @@ func (s *Scheduler) enqueueBatch(ctx context.Context, taskType string, cardIDs [
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SetCarrierScheduling 注入按运营商账户调度接缝。
|
||||
func (s *Scheduler) SetCarrierScheduling(carrier *CarrierScheduling) {
|
||||
s.carrier = carrier
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user