fix(批次E): 可信代理、地图快照失败不空白、SSE 硬超时、bike_history 去重
- routes/auth._client_ip:可信代理前缀可配(默认 127./::1/172./10./192.168.), 容器/Docker 部署下所有客户端不再塌成同一个限流 key(原来 45 次/分钟 变成全站共享,一两个人就能把所有人打成 429) - map_cache:采集失败**不再覆盖上一份好数据**,只放租约并把过期时间后推一个窗口 (一次抖动不该让全站地图空白 60 秒);同时去掉 published=False 时的同名递归 (deadline 每次重置 → 竞争下无界重试)。从来没过好数据时才写错误 payload, 保证窗口内返回值稳定 - /api/ring_progress SSE 加 180 秒硬超时(同步长连接占死 worker,-w 4 时 4 个进度页即可打满站点) - bike_history:迁移去重 + (bike_code, recorded_at) 唯一索引,采集改 INSERT OR IGNORE (多进程各写一份会让行数 ×N,污染 /api/history 统计) - 测试同步更新:地图快照失败语义改为「保留上一份好数据」 测试:167 pytest + 32 node 全绿
This commit is contained in:
@@ -276,6 +276,24 @@ def init_db():
|
||||
)
|
||||
""")
|
||||
|
||||
# 多进程/多实例采集会重复写同一秒的位置:先去重再建唯一索引,
|
||||
# 让 INSERT OR IGNORE 把重复写入挡掉,避免行数 ×N 污染 /api/history 统计。
|
||||
try:
|
||||
cursor.execute("""
|
||||
DELETE FROM bike_history
|
||||
WHERE id NOT IN (
|
||||
SELECT MIN(id) FROM bike_history GROUP BY bike_code, recorded_at
|
||||
)
|
||||
""")
|
||||
if cursor.rowcount:
|
||||
print(f"[迁移] bike_history 去重 {cursor.rowcount} 行")
|
||||
cursor.execute("""
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_bike_history_code_time
|
||||
ON bike_history (bike_code, recorded_at)
|
||||
""")
|
||||
except sqlite3.OperationalError as exc:
|
||||
print(f"[迁移] bike_history 唯一索引创建失败(忽略): {exc}")
|
||||
|
||||
for col_def in [("state", "INTEGER DEFAULT 1"), ("electric", "REAL DEFAULT 100")]:
|
||||
try:
|
||||
cursor.execute(f"ALTER TABLE bike_history ADD COLUMN {col_def[0]} {col_def[1]}")
|
||||
|
||||
Reference in New Issue
Block a user