mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-11-29 23:45:03 +08:00
fix(u115): avoid blocking worker threads for 3600s during rate-limit cooldown (#7356)
This commit is contained in:
@@ -95,6 +95,8 @@ class U115Pan(StorageBase, metaclass=WeakSingleton):
|
||||
self._limit_until = 0.0
|
||||
self._limit_lock = Lock()
|
||||
self._rate_stats = RateStats(source="115")
|
||||
self._usage_cache: Optional[_SchemaStorageUsage] = None
|
||||
self._usage_cache_until: float = 0.0
|
||||
|
||||
@property
|
||||
def snapshot_check_folder_modtime(self) -> bool:
|
||||
@@ -415,15 +417,15 @@ class U115Pan(StorageBase, metaclass=WeakSingleton):
|
||||
self._api_limiter.acquire()
|
||||
self._rate_stats.record()
|
||||
|
||||
# 风控冷却期间阻止所有接口调用,统一等待
|
||||
# 风控冷却期间快速返回失败,避免在同步工作线程中休眠 3600 秒导致线程池耗尽
|
||||
with self._limit_lock:
|
||||
wait_until = self._limit_until
|
||||
if wait_until > time.time():
|
||||
wait_secs = wait_until - time.time()
|
||||
logger.info(
|
||||
f"【115】风控冷却中,本请求等待 {wait_secs:.0f} 秒后再调用接口..."
|
||||
logger.warning(
|
||||
f"【115】风控冷却中(剩余 {wait_secs:.0f} 秒),跳过本次请求 {endpoint}"
|
||||
)
|
||||
time.sleep(wait_secs)
|
||||
return None
|
||||
|
||||
try:
|
||||
resp = self._request_utils.request(
|
||||
@@ -455,18 +457,9 @@ class U115Pan(StorageBase, metaclass=WeakSingleton):
|
||||
time.time() + self.limit_sleep_seconds,
|
||||
)
|
||||
logger.warning(
|
||||
f"【115】触发限流(429),全体接口进入风控冷却 {self.limit_sleep_seconds} 秒,随后重试..."
|
||||
)
|
||||
time.sleep(self.limit_sleep_seconds)
|
||||
kwargs["retry_limit"] = retry_times - 1
|
||||
kwargs["no_error_log"] = no_error_log
|
||||
return self._request_api(
|
||||
method,
|
||||
endpoint,
|
||||
result_key,
|
||||
accepted_codes=accepted_codes,
|
||||
**kwargs,
|
||||
f"【115】触发限流(429),全体接口进入风控冷却 {self.limit_sleep_seconds} 秒,本次请求直接返回失败"
|
||||
)
|
||||
return None
|
||||
|
||||
try:
|
||||
resp.raise_for_status()
|
||||
@@ -512,18 +505,9 @@ class U115Pan(StorageBase, metaclass=WeakSingleton):
|
||||
time.time() + self.limit_sleep_seconds,
|
||||
)
|
||||
logger.warning(
|
||||
f"【115】触发风控(访问上限),全体接口进入风控冷却 {self.limit_sleep_seconds} 秒,随后重试..."
|
||||
)
|
||||
time.sleep(self.limit_sleep_seconds)
|
||||
kwargs["retry_limit"] = retry_times - 1
|
||||
kwargs["no_error_log"] = no_error_log
|
||||
return self._request_api(
|
||||
method,
|
||||
endpoint,
|
||||
result_key,
|
||||
accepted_codes=accepted_codes,
|
||||
**kwargs,
|
||||
f"【115】触发风控(访问上限),全体接口进入风控冷却 {self.limit_sleep_seconds} 秒,本次请求直接返回失败"
|
||||
)
|
||||
return None
|
||||
return None
|
||||
|
||||
if result_key:
|
||||
@@ -1424,17 +1408,26 @@ class U115Pan(StorageBase, metaclass=WeakSingleton):
|
||||
|
||||
def usage(self) -> Optional[_SchemaStorageUsage]:
|
||||
"""
|
||||
查询账号空间的总容量与可用容量。
|
||||
查询账号空间的总容量与可用容量(短期缓存并在风控冷却期返回缓存值,避免高频轮询耗尽配额或阻塞线程)。
|
||||
|
||||
:return: 以字节计的容量信息;未登录或接口无数据时返回 None
|
||||
:return: 以字节计的容量信息;未登录或接口无数据时返回缓存或 None
|
||||
"""
|
||||
now = time.time()
|
||||
if self._usage_cache is not None and now < self._usage_cache_until:
|
||||
return self._usage_cache
|
||||
with self._limit_lock:
|
||||
if self._limit_until > now:
|
||||
return self._usage_cache
|
||||
try:
|
||||
resp = self._request_api("GET", "/open/user/info", "data")
|
||||
if not isinstance(resp, dict) or not resp:
|
||||
return None
|
||||
return self._usage_cache
|
||||
space = resp["rt_space_info"]
|
||||
return _SchemaStorageUsage(
|
||||
usage = _SchemaStorageUsage(
|
||||
total=space["all_total"]["size"], available=space["all_remain"]["size"]
|
||||
)
|
||||
self._usage_cache = usage
|
||||
self._usage_cache_until = now + 300.0
|
||||
return usage
|
||||
except NoCheckInException:
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user