refactor: geo 走原生异步 HTTP,清掉短链展开死链

「同步转异步」的残留清理:

1) auth_system/geo.query_ip_province 原经 core.sync_http 同步门面(在后台
   事件循环桥接)查询归属地,但其唯一调用方 record_login_location 的入口
   本来就是 async 的登录链路(web/routers/auth 里还套了一层 to_thread)。
   改为直接 await core.http_client——桥接纯属多余,顺带把 record_login_location
   转 async,落库仍走同步 sqlite 但经 asyncio.to_thread 卸载,避免 sqlite busy
   等待(最长 15s)冻结事件循环。
   调用方两处(普通用户/管理员登录)去掉 to_thread 包装,直接 await。

2) MusicAPIService._extract_music_id 是死代码:ID 归一化早已迁到
   web.request_utils(注释里明说是它的迁移口径),全仓零调用。它也是
   core.utils.expand_shortlink 的唯一调用方——一并删除,core.utils 由此
   彻底不再 import core.sync_http。

门禁:ruff 全过 / pyright 0-0 / pytest 387 项全绿(0 失败)。
This commit is contained in:
snowy-yang committed 2026-09-28 19:10:45 +08:00
1 parent 064a89d726
commit b089dadac2
5 files changed
+29 -81

No files matched your search

+1 -31
View File
@@ -1,11 +1,10 @@
# -*- coding: utf-8 -*-
"""纯函数工具(框架无关,无 Web 框架依赖)。
SRT 时间轴、LRC→SRT 转换、字段参数解析、163cn.tv 短链展开(含私有缓存)。
SRT 时间轴、LRC→SRT 转换、字段参数解析。
"""
import re
import threading
def srt_time(t: float) -> str:
@@ -69,32 +68,3 @@ def parse_meta_fields(raw) -> list[str] | None:
return list(raw) or None
except Exception:
return None
# 短链展开结果缓存:用户反复解析同一分享链接不再重复请求 163cn.tv(有界防膨胀)
_SHORTLINK_CACHE: dict[str, str] = {}
_SHORTLINK_CACHE_LOCK = threading.Lock()
def expand_shortlink(url: str) -> str:
"""展开 163cn.tv 短链(带进程内缓存;失败原样返回)"""
with _SHORTLINK_CACHE_LOCK:
hit = _SHORTLINK_CACHE.get(url)
if hit is not None:
return hit
expanded = url
try:
from core import sync_http
response = sync_http.get(url, allow_redirects=False, timeout=10)
expanded = response.headers.get('Location', url)
except Exception:
expanded = url
# 展开失败(Location 缺失/网络异常,expanded 原样等于 url)不写
# 缓存——旧口径一次网络抖动会让该分享链接在整个缓存存续期内必然解析失败
if expanded != url:
with _SHORTLINK_CACHE_LOCK:
if len(_SHORTLINK_CACHE) > 500:
_SHORTLINK_CACHE.clear()
_SHORTLINK_CACHE[url] = expanded
return expanded