All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m46s
【核心变更】
1. 停复机逻辑统一(StopResumeService)
- 新增 EvaluateAndAct 统一入口,封装三条件停复机判断
- 停机条件:无套餐(no_package) / 流量耗尽(traffic_exhausted) / 未实名(not_realname)
- 复机条件:stop_reason 合规 + 有套餐且未耗尽 + 已实名或行业卡
- 修复设备套餐 Bug:hasValidPackage 按 device_id 查套餐,而非仅 iot_card_id
- 设备维度停复机加幂等锁(Redis SetNX,TTL 30s),防止多卡并发重复调 Gateway
2. Redis 分片队列(PollingQueueManager)
- 新建 queue_manager.go,封装所有轮询 Redis 操作
- 16 分片 Sorted Set,Key 格式:polling:shard:{shardID}:queue:{taskType}
- Lua 脚本原子出队(ZRANGEBYSCORE + 分批 ZREM),消除竞态窗口
- 新增背压检测:队列深度超 50 万时 Scheduler 跳过该分片
- RemoveFromAllQueues 覆盖 4 种任务类型(含 protect)
3. Handler 拆分(polling_handler.go 1360行 → 5个专注文件)
- polling_base.go:共享基类(并发控制/卡缓存/重入队)
- polling_realname_handler.go:实名采集,实名 0→1 时立即触发复机
- polling_carddata_handler.go:流量采集,保留跨月边界检测逻辑
- polling_package_handler.go:套餐采集,委托 EvaluateAndAct 决策
- polling_protect_handler.go:保护期一致性检查,保护期内强制修正
4. 配置管理(PollingConfigManager)
- 新建 config_manager.go,从 scheduler.go 提取配置职责
- 内存缓存 + 5 分钟定时刷新,刷新失败保留原缓存
- 修复 getCardCondition:停机卡返回 suspended,不再错配 activated 配置
5. 渐进式初始化(CardInitializer)
- 新建 initializer.go,分批加载(每批 10 万),批次间 sleep 500ms
- 过滤 enable_polling=false 的卡,初始化完成前 Scheduler 不出队
6. 卡生命周期服务(PollingLifecycleService)
- 新建 lifecycle_service.go,替代已删除的 callbacks.go 和 api_callback.go
- OnCardCreated/OnCardEnabled/OnCardStatusChanged 入队前检查 enable_polling
7. Scheduler 精简(1000+行 → 227行)
- 保留纯调度循环:scheduleLoop + processShardSchedule + enqueueBatch
- 保留每 10 秒触发套餐过期检测和流量重置
- 移除所有 DB 操作、配置加载、卡初始化逻辑
8. 轮询管控 API(enable_polling)
- 新增 PUT /api/admin/assets/:id/polling-status 接口
- 支持对设备/卡维度开关轮询,关闭后从所有分片队列移除
9. 数据库迁移
- 000103:tb_device 新增 enable_polling 字段(boolean, NOT NULL, DEFAULT true)
- 000104:新增 suspended 轮询配置,为 activated 配置补全 protect_check_interval
【文件统计】
- 新增:19 个文件(handler × 5、polling 组件 × 4、迁移 × 3 等)
- 修改:20 个文件(bootstrap 注入、store 接口、monitoring 适配分片等)
- 删除:3 个文件(polling_handler.go、callbacks.go、api_callback.go)
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)
Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
160 lines
5.4 KiB
Go
160 lines
5.4 KiB
Go
package polling
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"strconv"
|
||
"time"
|
||
|
||
"github.com/redis/go-redis/v9"
|
||
"go.uber.org/zap"
|
||
|
||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||
)
|
||
|
||
// allTaskTypes 轮询系统的全部任务类型(用于 RemoveFromAllQueues 遍历)
|
||
var allTaskTypes = []string{
|
||
constants.TaskTypePollingRealname,
|
||
constants.TaskTypePollingCarddata,
|
||
constants.TaskTypePollingPackage,
|
||
constants.TaskTypePollingProtect,
|
||
}
|
||
|
||
// dequeueScript Lua 脚本:原子出队(ZRANGEBYSCORE + ZREM 服务端原子执行)
|
||
// 保留时间过滤语义:只取 score ≤ now 的到期卡,不触碰未来项
|
||
// 分批 ZREM:Lua unpack() 受 LUAI_MAXCSTACK 约 8000 限制,按 7000 分批避免溢出
|
||
var dequeueScript = redis.NewScript(`
|
||
local results = redis.call('ZRANGEBYSCORE', KEYS[1], '-inf', ARGV[1], 'LIMIT', 0, tonumber(ARGV[2]))
|
||
for i = 1, #results, 7000 do
|
||
local j = math.min(i + 6999, #results)
|
||
redis.call('ZREM', KEYS[1], unpack(results, i, j))
|
||
end
|
||
return results
|
||
`)
|
||
|
||
// CardEntry 出队卡信息
|
||
type CardEntry struct {
|
||
CardID uint
|
||
}
|
||
|
||
// PollingQueueManager 统一 Redis 轮询队列操作
|
||
// 两个进程(API 进程和 Worker 进程)共享,仅依赖 Redis Client
|
||
// 支持分片 Sorted Set,实现千万级规模
|
||
type PollingQueueManager struct {
|
||
redis *redis.Client
|
||
shardCount int
|
||
logger *zap.Logger
|
||
}
|
||
|
||
// NewPollingQueueManager 创建轮询队列管理器
|
||
func NewPollingQueueManager(redisClient *redis.Client, shardCount int, logger *zap.Logger) *PollingQueueManager {
|
||
if shardCount <= 0 {
|
||
shardCount = constants.PollingShardCount
|
||
}
|
||
return &PollingQueueManager{
|
||
redis: redisClient,
|
||
shardCount: shardCount,
|
||
logger: logger,
|
||
}
|
||
}
|
||
|
||
// DequeueReady 原子出队到期卡(Lua 脚本:ZRANGEBYSCORE + ZREM 服务端原子执行)
|
||
// 只取 score ≤ now 的到期卡,不触碰未来项
|
||
// taskType: realname | carddata | package | protect
|
||
// shardID: 0 到 shardCount-1
|
||
func (m *PollingQueueManager) DequeueReady(ctx context.Context, shardID int, taskType string, batchSize int) ([]CardEntry, error) {
|
||
// 防御:batchSize 不超过 Lua unpack 栈限制
|
||
if batchSize <= 0 || batchSize > constants.PollingDequeueMaxBatchSize {
|
||
batchSize = constants.PollingDequeueMaxBatchSize
|
||
}
|
||
key := constants.RedisPollingShardQueueKey(shardID, taskType)
|
||
now := time.Now().Unix()
|
||
|
||
results, err := dequeueScript.Run(ctx, m.redis, []string{key}, now, batchSize).StringSlice()
|
||
if err != nil && err != redis.Nil {
|
||
return nil, err
|
||
}
|
||
|
||
entries := make([]CardEntry, 0, len(results))
|
||
for _, s := range results {
|
||
id, parseErr := strconv.ParseUint(s, 10, 64)
|
||
if parseErr != nil {
|
||
m.logger.Warn("解析卡ID失败", zap.String("value", s), zap.Error(parseErr))
|
||
continue
|
||
}
|
||
entries = append(entries, CardEntry{CardID: uint(id)})
|
||
}
|
||
return entries, nil
|
||
}
|
||
|
||
// Requeue 将卡重新入队(ZADD,score 为下次检查时间戳)
|
||
func (m *PollingQueueManager) Requeue(ctx context.Context, cardID uint, taskType string, nextCheckAt time.Time) error {
|
||
shardID := int(cardID) % m.shardCount
|
||
key := constants.RedisPollingShardQueueKey(shardID, taskType)
|
||
return m.redis.ZAdd(ctx, key, redis.Z{
|
||
Score: float64(nextCheckAt.Unix()),
|
||
Member: fmt.Sprintf("%d", cardID),
|
||
}).Err()
|
||
}
|
||
|
||
// RemoveFromAllQueues 从所有分片的所有4个队列(含protect)移除指定卡
|
||
// 修复 Bug3:旧实现漏掉 protect 队列
|
||
func (m *PollingQueueManager) RemoveFromAllQueues(ctx context.Context, cardID uint) error {
|
||
member := fmt.Sprintf("%d", cardID)
|
||
pipe := m.redis.Pipeline()
|
||
for i := 0; i < m.shardCount; i++ {
|
||
for _, taskType := range allTaskTypes {
|
||
key := constants.RedisPollingShardQueueKey(i, taskType)
|
||
pipe.ZRem(ctx, key, member)
|
||
}
|
||
}
|
||
_, err := pipe.Exec(ctx)
|
||
return err
|
||
}
|
||
|
||
// EnqueueManual 手动触发入队(List RPUSH,调度器优先消费)
|
||
func (m *PollingQueueManager) EnqueueManual(ctx context.Context, cardID uint, taskType string) error {
|
||
key := constants.RedisPollingManualQueueKey(taskType)
|
||
return m.redis.RPush(ctx, key, fmt.Sprintf("%d", cardID)).Err()
|
||
}
|
||
|
||
// OnCardDeleted 卡删除事件处理(移除所有队列 + 清理卡信息缓存)
|
||
func (m *PollingQueueManager) OnCardDeleted(ctx context.Context, cardID uint) error {
|
||
// 从所有分片队列移除
|
||
if err := m.RemoveFromAllQueues(ctx, cardID); err != nil {
|
||
return err
|
||
}
|
||
// 清理轮询卡信息缓存
|
||
cacheKey := constants.RedisPollingCardInfoKey(cardID)
|
||
return m.redis.Del(ctx, cacheKey).Err()
|
||
}
|
||
|
||
// GetQueueDepth 获取分片队列深度(用于背压检测)
|
||
func (m *PollingQueueManager) GetQueueDepth(ctx context.Context, shardID int, taskType string) (int64, error) {
|
||
key := constants.RedisPollingShardQueueKey(shardID, taskType)
|
||
return m.redis.ZCard(ctx, key).Result()
|
||
}
|
||
|
||
// GetTotalQueueDepth 获取指定任务类型的总队列深度(聚合所有分片)
|
||
// 供 MonitoringService 使用,替代直接读取旧的非分片 Redis Key
|
||
// 若任意分片查询失败,返回已累计的部分总量和第一个错误,调用方可据此判断数据完整性
|
||
func (m *PollingQueueManager) GetTotalQueueDepth(ctx context.Context, taskType string) (int64, error) {
|
||
var total int64
|
||
var firstErr error
|
||
for i := 0; i < m.shardCount; i++ {
|
||
depth, err := m.GetQueueDepth(ctx, i, taskType)
|
||
if err != nil {
|
||
m.logger.Warn("获取分片队列深度失败",
|
||
zap.Int("shard_id", i),
|
||
zap.String("task_type", taskType),
|
||
zap.Error(err))
|
||
if firstErr == nil {
|
||
firstErr = err
|
||
}
|
||
continue
|
||
}
|
||
total += depth
|
||
}
|
||
return total, firstErr
|
||
}
|