From 6bc1cd027cda1480b3d993691ce84dabd4b9e84b Mon Sep 17 00:00:00 2001 From: Nixevol Date: Mon, 16 Nov 2026 17:51:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=8E=A2=E6=B5=8B=E6=95=B4=E6=89=B9?= =?UTF-8?q?=E4=BB=A3=E7=90=86=E5=8A=A0=E4=B8=8A=E6=80=BB=E6=97=B6=E9=99=90?= =?UTF-8?q?=EF=BC=8C=E5=AD=A6=E6=97=B6=E7=BF=BB=E9=A1=B5=E5=85=88=E5=88=A4?= =?UTF-8?q?=E9=87=8D=E5=86=8D=E7=B4=AF=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/euniv/autostudy.py | 21 ++++----------------- backend/euniv/client.py | 34 ++++++++++++++++++++-------------- backend/euniv/proxy_pool.py | 23 +++++++++++++++-------- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/backend/euniv/autostudy.py b/backend/euniv/autostudy.py index 296c532..685e6be 100644 --- a/backend/euniv/autostudy.py +++ b/backend/euniv/autostudy.py @@ -1065,9 +1065,9 @@ class StudySession: async def month_history_minutes(self, now: datetime | None = None) -> int: from euniv.client import ( HISTORY_PAGE_SIZE, - fold_month_minutes, history_items_from_payload, history_search_body, + take_history_page, ) from euniv.study_window import CN_TZ @@ -1075,7 +1075,7 @@ class StudySession: year, month = current.year, current.month offset = 0 total = 0 - first_mark: tuple | None = None + seen: set[tuple] = set() for _ in range(50): resp = await self._api( "POST", @@ -1087,22 +1087,9 @@ class StudySession: if offset == 0: raise RuntimeError("学时历史没有获取到") return total - if not rows: - return total - add, done = fold_month_minutes(rows, year, month) + add, stop = take_history_page(rows, seen, year, month, HISTORY_PAGE_SIZE) total += add - if done or len(rows) != HISTORY_PAGE_SIZE: - return total - mark = ( - len(rows), - rows[0].get("studied_at"), - rows[0].get("title"), - rows[-1].get("studied_at"), - rows[-1].get("title"), - ) - if offset == 0: - first_mark = mark - elif mark == first_mark: + if stop: return total offset += HISTORY_PAGE_SIZE return total diff --git a/backend/euniv/client.py b/backend/euniv/client.py index 503e30d..0bb8e0d 100644 --- a/backend/euniv/client.py +++ b/backend/euniv/client.py @@ -439,6 +439,24 @@ def history_hour_totals(items: list[dict], now: datetime | None = None) -> dict[ } +def take_history_page(rows: list, seen: set[tuple], year: int, month: int, page_size: int) -> tuple[int, bool]: + """先判断这一页是否重复。返回 (计入的分钟, 是否停止翻页)。""" + if not rows: + return 0, True + mark = ( + len(rows), + rows[0].get("studied_at"), + rows[0].get("title"), + rows[-1].get("studied_at"), + rows[-1].get("title"), + ) + if mark in seen: + return 0, True + seen.add(mark) + add, done = fold_month_minutes(rows, year, month) + return add, bool(done or len(rows) != page_size) + + def accumulate_month_minutes(fetch_page, year: int, month: int, page_size: int = HISTORY_PAGE_SIZE) -> tuple[int, bool]: """倒序分页累加本月分钟,遇到上个月即停。重复页直接返回,不再累加。""" offset = 0 @@ -450,21 +468,9 @@ def accumulate_month_minutes(fetch_page, year: int, month: int, page_size: int = if offset == 0: raise RuntimeError("学时历史没有获取到") return total, False - if not rows: - return total, False - mark = ( - len(rows), - rows[0].get("studied_at"), - rows[0].get("title"), - rows[-1].get("studied_at"), - rows[-1].get("title"), - ) - if mark in seen: - return total, False - seen.add(mark) - add, done = fold_month_minutes(rows, year, month) + add, stop = take_history_page(rows, seen, year, month, page_size) total += add - if done or len(rows) != page_size: + if stop: return total, False offset += page_size return total, True diff --git a/backend/euniv/proxy_pool.py b/backend/euniv/proxy_pool.py index e77e948..26906f9 100644 --- a/backend/euniv/proxy_pool.py +++ b/backend/euniv/proxy_pool.py @@ -31,6 +31,7 @@ LATENCY_LIMIT = 3.0 LATENCY_LIMIT_MS = 3000 PROBE_CONCURRENCY = 16 MAX_PROBES = 80 +PROBE_BATCH_TIMEOUT = 20.0 ALIVE_REPROBE_SLOTS = 20 MAX_SWITCHES = 8 PROBE_URL = EUNIV + "/" @@ -558,18 +559,24 @@ def _probe_many(nodes: list[tuple[int, str, str, int]]) -> list[tuple[int, bool, return [] workers = min(PROBE_CONCURRENCY, len(nodes)) results: list[tuple[int, bool, int, str]] = [] - with ThreadPoolExecutor(max_workers=workers) as pool: + pool = ThreadPoolExecutor(max_workers=workers) + try: futures = { pool.submit(probe, scheme, host, port): node_id for node_id, scheme, host, port in nodes } - for future in as_completed(futures): - node_id = futures[future] - try: - ok, latency, reason = future.result() - except Exception as exc: - ok, latency, reason = False, 0, _short_reason(exc) - results.append((node_id, ok, latency, reason)) + try: + for future in as_completed(futures, timeout=PROBE_BATCH_TIMEOUT): + node_id = futures[future] + try: + ok, latency, reason = future.result() + except Exception as exc: + ok, latency, reason = False, 0, _short_reason(exc) + results.append((node_id, ok, latency, reason)) + except TimeoutError: + pass + finally: + pool.shutdown(wait=False, cancel_futures=True) return results