feat: 轮询观测入库异步化,调网关与入库分离,采集记录经消息攒批写入
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 16m2s

- 轮询步骤异步模式下只调网关并发布 card.ingest 入库消息,入库消费者固定并发 30、按采集 ID 去重、可重试错误不限次数退避重投
- 采集记录经 card.collection 攒批写入,失败整批重投,新增 (observation_id, observed_at) 唯一索引
- 网络、实名观测增加乱序保护;轮询读查询走缓存,套餐状态查询各自计时且失败不缓存
- 入库积压告警与反压;系统配置 polling.ingest.mode 默认 sync 可回退;测试环境 nsqd 消息全部落盘

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
邹进
2026-11-06 16:33:29 +09:00
parent 7e0e37933e
commit 1214181e78
41 changed files with 2007 additions and 46 deletions

View File

@@ -118,6 +118,16 @@ func (h *PollingCardStatusHandler) Step(ctx context.Context, cardID uint) StepOu
return h.fail(ctx, cardID, startedAt, "卡网络观测能力未配置", nil)
}
now := time.Now()
if h.base.publishIngest(ctx, priority, IngestMessage{
Kind: ingestKindNetwork, CardID: cardID, CarrierID: card.CarrierID, ICCID: card.ICCID,
ObservationID: attempt.IntegrationID, ObservedAt: now, Source: constants.CardObservationSourcePolling,
Scene: constants.CardObservationSceneNetworkPolling, CorrelationID: attempt.IntegrationID,
TaskType: constants.TaskTypePollingCardStatus,
Network: &IngestNetworkPayload{Status: result.CardStatus, Extend: result.Extend, IMEI: result.IMEI},
}) {
h.base.updateStats(ctx, constants.TaskTypePollingCardStatus, true, time.Since(startedAt))
return asyncNetworkOutcome(card.EnablePolling, result.CardStatus, result.Extend)
}
decision, err := h.observation.ApplyNetworkObservation(ctx, carddomain.NetworkObservation{
CardID: cardID, GatewayStatus: result.CardStatus, GatewayExtend: result.Extend, GatewayIMEI: result.IMEI,
Metadata: carddomain.ObservationMetadata{
@@ -170,3 +180,20 @@ func (h *PollingCardStatusHandler) fail(ctx context.Context, cardID uint, starte
h.base.updateStats(ctx, constants.TaskTypePollingCardStatus, false, time.Since(startedAt))
return h.base.stepFail(ctx, constants.GatewayOpQueryStatus, message, err)
}
// asyncNetworkOutcome 在异步入库时按网关结果直接得出步骤结果:命中风险停机或已销户时停止轮询(入库段同时关闭卡轮询开关),
// 观测到的网络状态按网关状态映射,供延迟确认比较。
func asyncNetworkOutcome(enablePolling bool, gatewayStatus, gatewayExtend string) StepOutcome {
extend := strings.TrimSpace(gatewayExtend)
if enablePolling && carddomain.ShouldStopPollingForRisk(extend) {
return StepOutcome{Result: StepStopPolling}
}
outcome := stepOK()
if status, known := carddomain.MapGatewayNetworkStatus(gatewayStatus, extend); known {
outcome.Observed = ObservedOffline
if status == constants.NetworkStatusOnline {
outcome.Observed = ObservedOnline
}
}
return outcome
}