508d85a282
Retry stats cache commits, serialize refresh, and fall back to read-only compute so the stats API does not return 500 when the database is briefly locked. Co-authored-by: Cursor <cursoragent@cursor.com>
73 lines
2.2 KiB
Python
73 lines
2.2 KiB
Python
# Copyright (c) 2025-2026 马建军. All rights reserved.
|
|
# 专有软件 — 未经授权禁止复制、传播、转售。
|
|
# 严禁用于:带单/代客理财、向他人推荐期货品种或买卖建议、融资配资等业务。
|
|
# 详见 LICENSE.zh-CN.txt 与 docs/软件购买与使用协议.md
|
|
|
|
"""SQLite 连接统一配置(WAL + busy_timeout,降低并发锁冲突)。"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import sqlite3
|
|
import time
|
|
|
|
DB_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "futures.db")
|
|
|
|
|
|
def connect_db(path: str | None = None) -> sqlite3.Connection:
|
|
db_path = path or DB_PATH
|
|
conn = sqlite3.connect(db_path, timeout=30, check_same_thread=False)
|
|
conn.row_factory = sqlite3.Row
|
|
conn.execute("PRAGMA busy_timeout=30000")
|
|
try:
|
|
conn.execute("PRAGMA journal_mode=WAL")
|
|
except sqlite3.OperationalError:
|
|
pass
|
|
return conn
|
|
|
|
|
|
def execute_retry(
|
|
conn: sqlite3.Connection,
|
|
sql: str,
|
|
params: tuple = (),
|
|
*,
|
|
retries: int = 6,
|
|
base_delay: float = 0.05,
|
|
) -> sqlite3.Cursor:
|
|
"""遇 database is locked 时短暂退避重试。"""
|
|
last_exc: Exception | None = None
|
|
for attempt in range(retries):
|
|
try:
|
|
return conn.execute(sql, params)
|
|
except sqlite3.OperationalError as exc:
|
|
if "locked" not in str(exc).lower():
|
|
raise
|
|
last_exc = exc
|
|
if attempt < retries - 1:
|
|
time.sleep(base_delay * (attempt + 1))
|
|
if last_exc:
|
|
raise last_exc
|
|
raise sqlite3.OperationalError("database is locked")
|
|
|
|
|
|
def commit_retry(
|
|
conn: sqlite3.Connection,
|
|
*,
|
|
retries: int = 6,
|
|
base_delay: float = 0.05,
|
|
) -> None:
|
|
"""遇 database is locked 时短暂退避重试 commit。"""
|
|
last_exc: Exception | None = None
|
|
for attempt in range(retries):
|
|
try:
|
|
conn.commit()
|
|
return
|
|
except sqlite3.OperationalError as exc:
|
|
if "locked" not in str(exc).lower():
|
|
raise
|
|
last_exc = exc
|
|
if attempt < retries - 1:
|
|
time.sleep(base_delay * (attempt + 1))
|
|
if last_exc:
|
|
raise last_exc
|
|
raise sqlite3.OperationalError("database is locked")
|