fix: 资源包查询修复翻页死循环并加 Status 过滤

- debug_volc_sync.py 缺少 seen_tokens 防循环保护,火山 API 翻页到头后
  NextToken 循环回起始 token,导致无限翻页(实测 900s 拉 1893 页假象,
  真实数据仅 8 页 137 个包)
- 两个 fetch_all_packages 请求加 Status=Effective,服务端直接过滤
  UsedUp/Expired/Refunded 等余额为 0 的无用包(实测过滤后 0 余额包为 0)
- 主脚本循环检测日志从 DEBUG 提升为 WARNING,循环收尾不再静默
This commit is contained in:
root
2026-08-11 03:39:57 +00:00
parent e29d94f9e8
commit 8feb38db37
2 changed files with 26 additions and 5 deletions
+15 -1
View File
@@ -116,12 +116,15 @@ def fetch_all_packages():
version = "2022-01-01"
all_packages = []
current_token = ""
seen_tokens = set()
while True:
try:
payload = {
"ResourceType": "Package",
"MaxResults": "20",
# 只拉生效中的数据包,跳过 UsedUp/Expired/Refunded 等余额为 0 的无用包
"Status": "Effective",
"NextToken": current_token,
}
headers, body = get_signed_headers(action, version, payload)
@@ -134,8 +137,19 @@ def fetch_all_packages():
if items:
all_packages.extend(items)
next_token = data.get("Result", {}).get("NextToken", "")
if not next_token or next_token == current_token:
# Infinite Loop Protection: VolcEngine API 翻页到头后 NextToken 会
# 循环回起始 token(实测:数据只有 ~8 页,第 9 页绕回第一个 token),
# 没有该保护会无限翻页。
if not next_token:
break
if next_token == current_token:
break
if next_token in seen_tokens:
print(f"WARNING: Detected pagination cycle with token {next_token}. Stopping.")
break
seen_tokens.add(next_token)
current_token = next_token
time.sleep(0.2)
except Exception as e:
+10 -3
View File
@@ -863,6 +863,10 @@ def fetch_all_packages():
payload = {
"ResourceType": "Package",
"MaxResults": "20",
# 服务端只返回生效中的数据包,跳过 UsedUp/Expired/Refunded 等
# 余额为 0 的无用包,显著减少翻页次数与 QPS 占用。
# 周期型包(月度重置)状态仍为 Effective,不会被误过滤。
"Status": "Effective",
"NextToken": current_token,
}
headers, body = get_signed_headers(action, version, payload)
@@ -876,14 +880,17 @@ def fetch_all_packages():
next_token = data.get("Result", {}).get("NextToken", "")
# Infinite Loop Protection
# 火山 API 翻页到头后 NextToken 会循环回起始 token(实测数据只有
# ~8 页时第 9 页绕回第一个 token),必须用 seen_tokens 拦下载断。
if not next_token:
break
if next_token == current_token:
break
if next_token in seen_tokens:
if DEBUG_MODE:
print(
f"DEBUG: Detected pagination cycle with token {next_token}. Stopping."
log(
f"⚠️ VolcEngine pagination cycle detected (token {next_token} repeated). "
f"Stopping after {len(all_packages)} packages.",
level="WARNING",
)
break