perf(douyu): 加速外部协作单账号推荐
This commit is contained in:
@@ -588,6 +588,7 @@ def probe_account(
|
||||
),
|
||||
with_elite: bool = True,
|
||||
with_eligibility: bool = False,
|
||||
with_limited_goods: bool = False,
|
||||
with_gold: bool = False,
|
||||
with_goods: bool = False,
|
||||
with_elite_role: bool = True,
|
||||
@@ -602,6 +603,7 @@ def probe_account(
|
||||
[
|
||||
f"elite={int(with_elite)}",
|
||||
f"elig={int(with_eligibility)}",
|
||||
f"limited={int(with_limited_goods)}",
|
||||
f"gold={int(with_gold)}",
|
||||
f"goods={int(with_goods)}",
|
||||
f"role={int(with_elite_role)}",
|
||||
@@ -734,6 +736,7 @@ def probe_account(
|
||||
points_response = points_result
|
||||
except Exception as exc: # noqa: BLE001
|
||||
errors["points"] = f"{type(exc).__name__}: {_text(exc)}"
|
||||
if with_eligibility or with_limited_goods:
|
||||
try:
|
||||
limited_result = query_limited_goods_result(
|
||||
active_client,
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable, Mapping, Sequence
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from concurrent.futures import TimeoutError as FutureTimeoutError
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
@@ -83,7 +85,7 @@ from .file_service import build_file_url
|
||||
from .platform_fulfillment.account_selector import (
|
||||
acquire_account_lease,
|
||||
active_lease_for_account,
|
||||
has_recent_exchange,
|
||||
capability_candidate_accounts,
|
||||
live_candidate_accounts,
|
||||
release_lease,
|
||||
)
|
||||
@@ -146,7 +148,9 @@ STATUS_FLOW: tuple[str, ...] = (
|
||||
|
||||
SOURCE_TYPE_PLATFORM_FULFILLMENT = "platform_fulfillment"
|
||||
ACCOUNT_REPLACE_ROLES = frozenset({"super_admin", "operation"})
|
||||
ACCOUNT_RECOMMEND_DEFAULT_SCAN_LIMIT = 30
|
||||
ACCOUNT_RECOMMEND_DEFAULT_SCAN_LIMIT = 8
|
||||
ACCOUNT_RECOMMEND_CONCURRENCY = 8
|
||||
ACCOUNT_RECOMMEND_TIMEOUT_SECONDS = 20
|
||||
PLATFORM_TAKEOVER_BLOCKING_TASK_TYPES = frozenset(
|
||||
{
|
||||
"lock_goods",
|
||||
@@ -587,6 +591,24 @@ def unlink_account(
|
||||
return order
|
||||
|
||||
|
||||
def _recommendation_account_snapshot(account: Account) -> Account:
|
||||
"""复制探针所需的标量字段,避免工作线程访问 SQLAlchemy Session。"""
|
||||
return Account(
|
||||
id=int(account.id),
|
||||
username=str(account.username or ""),
|
||||
password="",
|
||||
email="",
|
||||
email_password="",
|
||||
cookie=str(account.cookie or ""),
|
||||
uid=str(account.uid or ""),
|
||||
nickname=str(account.nickname or ""),
|
||||
elite_opened=account.elite_opened,
|
||||
points=account.points,
|
||||
gold_balance=account.gold_balance,
|
||||
points_expire_at=account.points_expire_at,
|
||||
)
|
||||
|
||||
|
||||
def recommend_accounts(
|
||||
db: Session,
|
||||
current: User,
|
||||
@@ -619,155 +641,201 @@ def recommend_accounts(
|
||||
1,
|
||||
min(
|
||||
int(scan_limit or ACCOUNT_RECOMMEND_DEFAULT_SCAN_LIMIT),
|
||||
100,
|
||||
ACCOUNT_RECOMMEND_CONCURRENCY,
|
||||
),
|
||||
)
|
||||
candidates = live_candidate_accounts(
|
||||
candidates_by_id: dict[int, Account] = {}
|
||||
if cid:
|
||||
for account in capability_candidate_accounts(
|
||||
db,
|
||||
limit=effective_scan_limit,
|
||||
required_points=required,
|
||||
commodity_id=cid,
|
||||
scope="elite",
|
||||
):
|
||||
candidates_by_id[int(account.id)] = account
|
||||
for account in live_candidate_accounts(
|
||||
db,
|
||||
limit=effective_scan_limit,
|
||||
required_points=required,
|
||||
commodity_id=cid,
|
||||
prefer_unbound=True,
|
||||
empty_only=not include_rebindable,
|
||||
):
|
||||
candidates_by_id.setdefault(int(account.id), account)
|
||||
|
||||
candidates = [
|
||||
account
|
||||
for account in candidates_by_id.values()
|
||||
if (not order.account_id or int(account.id) != int(order.account_id))
|
||||
and (include_rebindable or account.role_bind_status == "unbound")
|
||||
]
|
||||
candidates.sort(
|
||||
key=lambda account: (
|
||||
0 if account.role_bind_status == "unbound" else 1,
|
||||
0 if account.elite_opened is True else 1,
|
||||
0 if (account.points or 0) >= required else 1,
|
||||
int(account.id),
|
||||
)
|
||||
)
|
||||
candidates = candidates[:effective_scan_limit]
|
||||
|
||||
config = resolve_config(db)
|
||||
manual_id = str(config.manual_id or "")
|
||||
rid = str(config.rid or "")
|
||||
act_alias = str(config.bind_act_alias or "")
|
||||
confirm_act_alias = str(config.confirm_act_alias or "")
|
||||
legacy_act_alias = str(config.legacy_act_alias or "")
|
||||
config_info = config_mapping(
|
||||
{
|
||||
"manual_id": str(config.manual_id or ""),
|
||||
"rid": str(config.rid or ""),
|
||||
"bind_act_alias": str(config.bind_act_alias or ""),
|
||||
"confirm_act_alias": str(config.confirm_act_alias or ""),
|
||||
"legacy_act_alias": str(config.legacy_act_alias or ""),
|
||||
}
|
||||
)
|
||||
output: list[dict[str, Any]] = []
|
||||
scanned = 0
|
||||
result_limit = max(1, min(int(limit), 10))
|
||||
|
||||
for account in candidates:
|
||||
if order.account_id and int(account.id) == int(order.account_id):
|
||||
continue
|
||||
scanned += 1
|
||||
def inspect_candidate(account: Account) -> tuple[Account, dict[str, Any]] | None:
|
||||
try:
|
||||
probe = _probe_with_client(
|
||||
account,
|
||||
manual_id=manual_id,
|
||||
rid=rid,
|
||||
act_alias=act_alias,
|
||||
confirm_act_alias=confirm_act_alias,
|
||||
legacy_act_alias=legacy_act_alias,
|
||||
client=client_factory(account) if client_factory else None,
|
||||
with_goods=bool(cid),
|
||||
active_client = (
|
||||
client_factory(account)
|
||||
if client_factory
|
||||
else activity_client(
|
||||
str(account.cookie or ""),
|
||||
logger_callback=lambda message: logger.debug(
|
||||
"[douyu-collab] recommend account={} {}",
|
||||
account.id,
|
||||
message,
|
||||
),
|
||||
)
|
||||
)
|
||||
probe = probe_account(
|
||||
account,
|
||||
config_info,
|
||||
client=active_client,
|
||||
session_checker=(
|
||||
None if client_factory is not None else check_douyu_cookie
|
||||
),
|
||||
with_elite=True,
|
||||
with_eligibility=False,
|
||||
with_limited_goods=bool(cid),
|
||||
with_gold=False,
|
||||
with_goods=False,
|
||||
with_elite_role=True,
|
||||
use_cache=client_factory is None,
|
||||
)
|
||||
return account, probe
|
||||
except Exception as exc: # noqa: BLE001 - 单账号失败继续扫描
|
||||
logger.info(
|
||||
"[douyu-collab] 推荐账号跳过 account_id={} error={}",
|
||||
account.id,
|
||||
exc,
|
||||
)
|
||||
continue
|
||||
return None
|
||||
|
||||
if str(probe.get("ck_status") or "") != CK_VALID:
|
||||
continue
|
||||
role_status = str(probe.get("role_status") or ROLE_UNKNOWN)
|
||||
canonical = probe.get("_probe_result")
|
||||
canonical_role = (
|
||||
(canonical.get("roles") or {}).get("elite") or {}
|
||||
if isinstance(canonical, Mapping)
|
||||
else {}
|
||||
)
|
||||
def accept_candidate(account: Account, probe: dict[str, Any]) -> None:
|
||||
session = probe.get("session") or {}
|
||||
if str(session.get("status") or "") != SESSION_VALID:
|
||||
return
|
||||
role = (probe.get("roles") or {}).get("elite") or {}
|
||||
role_status = str(role.get("status") or ROLE_UNKNOWN)
|
||||
bind_info = (
|
||||
canonical_role.get("bind_info")
|
||||
if isinstance(canonical_role.get("bind_info"), dict)
|
||||
else None
|
||||
role.get("bind_info") if isinstance(role.get("bind_info"), dict) else None
|
||||
)
|
||||
if role_status == ROLE_UNBOUND:
|
||||
match_type = "empty"
|
||||
match_label = "未绑定"
|
||||
elif role_status == ROLE_BOUND and can_change_role(bind_info):
|
||||
if not include_rebindable:
|
||||
continue
|
||||
return
|
||||
match_type = "rebind"
|
||||
match_label = "可换绑"
|
||||
else:
|
||||
continue
|
||||
return
|
||||
|
||||
canonical_eligibility = (
|
||||
canonical.get("eligibility") or {}
|
||||
if isinstance(canonical, Mapping)
|
||||
else {}
|
||||
)
|
||||
eligibility = probe.get("eligibility") or {}
|
||||
limited_ids = {
|
||||
str(item)
|
||||
for item in canonical_eligibility.get("limited_ids") or []
|
||||
for item in eligibility.get("limited_ids") or []
|
||||
if str(item)
|
||||
}
|
||||
if cid and cid in limited_ids:
|
||||
continue
|
||||
if cid and has_recent_exchange(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
commodity_id=cid,
|
||||
):
|
||||
continue
|
||||
return
|
||||
|
||||
goods = probe.get("goods") or []
|
||||
target_goods = next(
|
||||
(
|
||||
item
|
||||
for item in goods
|
||||
if isinstance(item, Mapping)
|
||||
and str(item.get("commodity_id") or "") == cid
|
||||
),
|
||||
None,
|
||||
)
|
||||
if target_goods is not None:
|
||||
if str(target_goods.get("status") or "") == "1":
|
||||
continue
|
||||
if _to_int(target_goods.get("open_status")) == 0:
|
||||
continue
|
||||
|
||||
live_points = _to_int(probe.get("manual_score"))
|
||||
elite_opened = probe.get("manual_opened")
|
||||
elite_opened_value = bool(elite_opened) if elite_opened is not None else None
|
||||
elite = probe.get("elite") or {}
|
||||
live_points = _to_int(eligibility.get("points"))
|
||||
if live_points is None:
|
||||
live_points = _to_int(elite.get("manual_score"))
|
||||
elite_opened = bool(elite.get("opened"))
|
||||
ready = bool(
|
||||
elite_opened_value is True
|
||||
and live_points is not None
|
||||
and live_points >= required
|
||||
elite_opened and live_points is not None and live_points >= required
|
||||
)
|
||||
reasons = [
|
||||
"未绑定角色,可直接绑定"
|
||||
if match_type == "empty"
|
||||
else "已绑定角色,当前可换绑"
|
||||
]
|
||||
if elite_opened_value is not True:
|
||||
if not elite_opened:
|
||||
reasons.append("精英宝典未开通")
|
||||
if live_points is None or live_points < required:
|
||||
reasons.append(f"积分不足({live_points or 0}/{required})")
|
||||
if cid and target_goods is None:
|
||||
reasons.append("商品列表未返回该礼包,绑定后请复核")
|
||||
|
||||
identity = probe.get("identity") or {}
|
||||
output.append(
|
||||
{
|
||||
"account_id": int(account.id),
|
||||
# 后续“使用此账号”会按内部 username 反查,不能用接口返回的展示昵称。
|
||||
"account_username": str(account.username or ""),
|
||||
"account_uid": str(probe.get("account_uid") or account.uid or ""),
|
||||
"account_uid": str(identity.get("uid") or account.uid or ""),
|
||||
"account_nickname": str(
|
||||
probe.get("account_nickname")
|
||||
identity.get("nickname")
|
||||
or account.nickname
|
||||
or account.username
|
||||
or ""
|
||||
),
|
||||
"ck_status": str(probe.get("ck_status") or ""),
|
||||
"ck_status": CK_VALID,
|
||||
"role_status": role_status,
|
||||
"bind_game_role": str(probe.get("bind_game_role") or ""),
|
||||
"bind_game_role": str(role.get("role_name") or ""),
|
||||
"role_change_available_at": role_change_available_at(bind_info),
|
||||
"points": live_points,
|
||||
"elite_opened": elite_opened_value,
|
||||
"elite_opened": elite_opened,
|
||||
"match_type": match_type,
|
||||
"match_type_label": match_label,
|
||||
"ready": ready,
|
||||
"reasons": reasons,
|
||||
}
|
||||
)
|
||||
if len(output) >= max(1, min(int(limit), 10)):
|
||||
break
|
||||
|
||||
snapshots = [_recommendation_account_snapshot(account) for account in candidates]
|
||||
if snapshots:
|
||||
executor = ThreadPoolExecutor(
|
||||
max_workers=min(ACCOUNT_RECOMMEND_CONCURRENCY, len(snapshots))
|
||||
)
|
||||
futures = {
|
||||
executor.submit(inspect_candidate, account): account
|
||||
for account in snapshots
|
||||
}
|
||||
scanned = len(futures)
|
||||
try:
|
||||
for future in as_completed(
|
||||
futures,
|
||||
timeout=ACCOUNT_RECOMMEND_TIMEOUT_SECONDS,
|
||||
):
|
||||
result = future.result()
|
||||
if result is not None:
|
||||
accept_candidate(*result)
|
||||
if len(output) >= result_limit:
|
||||
break
|
||||
except FutureTimeoutError:
|
||||
logger.warning(
|
||||
"[douyu-collab] 推荐账号实时校验超时,已返回当前结果 scanned={} "
|
||||
"timeout={}s",
|
||||
scanned,
|
||||
ACCOUNT_RECOMMEND_TIMEOUT_SECONDS,
|
||||
)
|
||||
finally:
|
||||
for future in futures:
|
||||
future.cancel()
|
||||
executor.shutdown(wait=False, cancel_futures=True)
|
||||
|
||||
output.sort(
|
||||
key=lambda item: (
|
||||
|
||||
Reference in New Issue
Block a user