fix: 停复机成功后上游状态滞后导致暂停轮询的卡到期或退款漏停
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m29s

- 停复机成功后保护窗口内(polling.stop_resume.status_grace_seconds,默认 300 秒)查卡状态返回操作前的状态视为上游滞后,不回写网络状态
- 套餐到期、退款场景不看库里的开停状态,资产无可用主套餐即停机,上游已停时按已是目标状态结束
- 迁移 000257 插入保护窗口配置

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
邹进
2026-10-26 10:41:45 +09:00
parent f6a740b9f3
commit d4fde99cc3
11 changed files with 109 additions and 5 deletions

View File

@@ -2,6 +2,7 @@ package cardobservation
import (
"strings"
"time"
"github.com/break/junhong_cmp_fiber/pkg/constants"
"github.com/break/junhong_cmp_fiber/pkg/errors"
@@ -22,6 +23,10 @@ type CardNetworkSnapshot struct {
NetworkStatus int
StopReason string
EnablePolling bool
// StoppedAt、ResumedAt 是本系统最近一次停机、复机成功回写的时间;CommandGrace 是停复机成功后的状态保护窗口。
StoppedAt *time.Time
ResumedAt *time.Time
CommandGrace time.Duration
}
// NetworkDecision 描述一次网络观测可安全持久化的最终值。
@@ -36,6 +41,8 @@ type NetworkDecision struct {
StopReason string
StopReasonChanged bool
StopPolling bool
// StatusSuppressed 为 true 表示观测到的状态与保护窗口内刚成功的停复机相反,判定为上游状态滞后,不回写网络状态。
StatusSuppressed bool
}
// ApplyNetwork 应用 Gateway 状态映射、运营商停机原因和风险终止卡关闭轮询规则。
@@ -57,6 +64,11 @@ func ApplyNetwork(snapshot CardNetworkSnapshot, observation NetworkObservation)
if !known {
return decision, nil
}
if status != snapshot.NetworkStatus && withinCommandGrace(snapshot, observation.Metadata.ObservedAt) {
// 停复机成功后上游状态有延迟:保护窗口内观测到操作前的状态不回写,窗口过后的检查以上游为准。
decision.StatusSuppressed = true
return decision, nil
}
decision.AfterStatus = status
decision.StatusChanged = status != snapshot.NetworkStatus
if decision.StatusChanged && status == constants.NetworkStatusOffline && snapshot.StopReason == "" &&
@@ -88,3 +100,22 @@ func MapGatewayNetworkStatus(cardStatus, extend string) (int, bool) {
func ShouldStopPollingForRisk(extend string) bool {
return extend == constants.GatewayCardExtendRiskStop || extend == constants.GatewayCardExtendCancelled
}
// withinCommandGrace 判断观测时间是否落在本系统最近一次停复机成功后的保护窗口内:
// 库里为停机时看停机时间,为开机时看复机时间。
func withinCommandGrace(snapshot CardNetworkSnapshot, observedAt time.Time) bool {
if snapshot.CommandGrace <= 0 {
return false
}
last := snapshot.ResumedAt
if snapshot.NetworkStatus == constants.NetworkStatusOffline {
last = snapshot.StoppedAt
}
if last == nil || last.IsZero() {
return false
}
if observedAt.IsZero() {
observedAt = time.Now()
}
return observedAt.Sub(*last) < snapshot.CommandGrace
}