refactor: 清理 13 处生产死代码

为什么:用 AST 级引用分析(而非文本 grep——同名符号会互相掩盖)确认这些
符号全项目零引用。文本 grep 查不出的原因是它们都有「活着的同名兄弟」:

services/downloads.py(共 6 个,93 行)
  _parse_cookie_str / _global_music_u / _vip_name / _detect_vip /
  _admin_client / _user_client
  —— 活的是 core.media.parse_cookie_str、web/routers/music.py、account.py 的版本
  随之失效的 import:USER_COOKIE_FILE / GLOBAL_MUSIC_U_CACHE / VIP_PROBE_LOCK /
  account_info / detect_vip_by_probe / QRLoginClient

services/api_service.py
  APIResponse 类(29 行)—— web/response.py 才是活的响应封装

services/_shared.py
  _rate_ok —— 逐字复制 web.ratelimit.rate_ok

core/utils.py
  safe_int / safe_console_print —— 活的是 web/request_utils.safe_int

core/repo.py
  all_settings / get_user_by_name —— auth_system.get_user_by_name 才是活的

core/db.py / web/auth_state.py / web/request_utils.py
  execute_many / _safe_register_credits + secrets_rand / dedup_ids

结果:生产代码死代码候选 13 → 0(余下 118 个全是 pytest 测试函数,属正常)。
This commit is contained in:
snowy-yang committed 2026-09-26 18:54:11 +08:00
1 parent 445c16c329
commit 0569b50737
8 files changed
+10 -217

No files matched your search

+4 -21
View File
@@ -2,7 +2,6 @@
"""服务层共享状态与低层工具(限流桶、请求统计、命名与常量)。
"""
import collections
import re
import time
from typing import Any
@@ -19,25 +18,8 @@ from music_downloader import (
CountingSemaphore,
)
def _rate_ok(key: str, limit: int, window: int) -> bool:
"""滑动窗口限流:True=放行,False=超限"""
now = time.time()
with _RATE_LOCK:
dq = _RATE_BUCKETS.get(key)
if dq is None:
# 桶数量防膨胀:超限时清理不活跃桶
if len(_RATE_BUCKETS) > 8000:
for k in [k for k, v in _RATE_BUCKETS.items() if not v or now - v[-1] > window]:
_RATE_BUCKETS.pop(k, None)
dq = collections.deque()
_RATE_BUCKETS[key] = dq
while dq and now - dq[0] > window:
dq.popleft()
if len(dq) >= limit:
return False
dq.append(now)
return True
# 限流实现只有一处:web.ratelimit.rate_ok(同读 core.state 的桶,
# 此处不再保留同名副本——曾有两份逐字相同的滑动窗口实现)
# 旧版管理员口令已废除(2026-09-15):管理员权限仅认站点管理员会话(admin 登录),
@@ -76,7 +58,8 @@ def _sweep_rate_buckets() -> None:
_RATE_BUCKETS.pop(k, None)
# _safe_int / _safe_console_print 已迁出至 core.utils(此处 import 同名共享)
# 参数安全转换只有一处:web.request_utils.safe_int
# (原 core.utils.safe_int / safe_console_print 与 _shared 的副本均已删除——三份实现零调用)
# ============ 接口请求统计 ============