perf(douyu): 加速外部协作单账号推荐
This commit is contained in:
@@ -136,6 +136,18 @@ class ProbeClient:
|
|||||||
return _bind("角色A", bound_act=True, bound_role=True)
|
return _bind("角色A", bound_act=True, bound_role=True)
|
||||||
|
|
||||||
|
|
||||||
|
class LimitedOnlyClient(ProbeClient):
|
||||||
|
def acf_ccn(self, **_kwargs):
|
||||||
|
raise AssertionError("推荐探针不应查询积分令牌")
|
||||||
|
|
||||||
|
def query_points(self, **_kwargs):
|
||||||
|
raise AssertionError("推荐探针不应查询积分接口")
|
||||||
|
|
||||||
|
def query_limited_goods(self, **_kwargs):
|
||||||
|
self.calls += 1
|
||||||
|
return {"limited_goods": [{"commodityId": "limited-goods"}]}
|
||||||
|
|
||||||
|
|
||||||
def test_probe_account_reads_identity_and_does_not_call_activity_for_invalid_ck():
|
def test_probe_account_reads_identity_and_does_not_call_activity_for_invalid_ck():
|
||||||
account = Account(
|
account = Account(
|
||||||
id=1,
|
id=1,
|
||||||
@@ -178,6 +190,34 @@ def test_probe_account_reads_identity_and_does_not_call_activity_for_invalid_ck(
|
|||||||
assert invalid_client.calls == 0
|
assert invalid_client.calls == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_probe_limited_goods_does_not_run_full_eligibility_flow():
|
||||||
|
account = Account(
|
||||||
|
id=2,
|
||||||
|
username="limited-only",
|
||||||
|
password="p",
|
||||||
|
email="limited@example.com",
|
||||||
|
email_password="ep",
|
||||||
|
cookie="acf_uid=200; acf_nickname=limited",
|
||||||
|
)
|
||||||
|
client = LimitedOnlyClient()
|
||||||
|
|
||||||
|
result = probe_account(
|
||||||
|
account,
|
||||||
|
CONFIG,
|
||||||
|
client=client,
|
||||||
|
session_checker=lambda _cookie: {
|
||||||
|
"valid": True,
|
||||||
|
"message": "有效",
|
||||||
|
"checked_at": datetime.now(UTC),
|
||||||
|
},
|
||||||
|
with_eligibility=False,
|
||||||
|
with_limited_goods=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["eligibility"]["limited_ids"] == ["limited-goods"]
|
||||||
|
assert result["eligibility"]["points"] == 100
|
||||||
|
|
||||||
|
|
||||||
def test_persist_probe_error_preserves_last_known_role(db):
|
def test_persist_probe_error_preserves_last_known_role(db):
|
||||||
account = Account(
|
account = Account(
|
||||||
username="preserve-role",
|
username="preserve-role",
|
||||||
|
|||||||
@@ -44,6 +44,15 @@ class FakeClient:
|
|||||||
"user_info": {"nickname": "斗鱼昵称"},
|
"user_info": {"nickname": "斗鱼昵称"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def acf_ccn(self, **_):
|
||||||
|
return "fake-csrf"
|
||||||
|
|
||||||
|
def query_points(self, **_):
|
||||||
|
return {"points": self.points}
|
||||||
|
|
||||||
|
def query_limited_goods(self, **_):
|
||||||
|
return {"limited_goods": []}
|
||||||
|
|
||||||
def bind_info(self, **_):
|
def bind_info(self, **_):
|
||||||
return {
|
return {
|
||||||
"is_bound_account": True,
|
"is_bound_account": True,
|
||||||
|
|||||||
@@ -588,6 +588,7 @@ def probe_account(
|
|||||||
),
|
),
|
||||||
with_elite: bool = True,
|
with_elite: bool = True,
|
||||||
with_eligibility: bool = False,
|
with_eligibility: bool = False,
|
||||||
|
with_limited_goods: bool = False,
|
||||||
with_gold: bool = False,
|
with_gold: bool = False,
|
||||||
with_goods: bool = False,
|
with_goods: bool = False,
|
||||||
with_elite_role: bool = True,
|
with_elite_role: bool = True,
|
||||||
@@ -602,6 +603,7 @@ def probe_account(
|
|||||||
[
|
[
|
||||||
f"elite={int(with_elite)}",
|
f"elite={int(with_elite)}",
|
||||||
f"elig={int(with_eligibility)}",
|
f"elig={int(with_eligibility)}",
|
||||||
|
f"limited={int(with_limited_goods)}",
|
||||||
f"gold={int(with_gold)}",
|
f"gold={int(with_gold)}",
|
||||||
f"goods={int(with_goods)}",
|
f"goods={int(with_goods)}",
|
||||||
f"role={int(with_elite_role)}",
|
f"role={int(with_elite_role)}",
|
||||||
@@ -734,6 +736,7 @@ def probe_account(
|
|||||||
points_response = points_result
|
points_response = points_result
|
||||||
except Exception as exc: # noqa: BLE001
|
except Exception as exc: # noqa: BLE001
|
||||||
errors["points"] = f"{type(exc).__name__}: {_text(exc)}"
|
errors["points"] = f"{type(exc).__name__}: {_text(exc)}"
|
||||||
|
if with_eligibility or with_limited_goods:
|
||||||
try:
|
try:
|
||||||
limited_result = query_limited_goods_result(
|
limited_result = query_limited_goods_result(
|
||||||
active_client,
|
active_client,
|
||||||
|
|||||||
@@ -14,6 +14,8 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Callable, Mapping, Sequence
|
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 datetime import UTC, datetime
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
@@ -83,7 +85,7 @@ from .file_service import build_file_url
|
|||||||
from .platform_fulfillment.account_selector import (
|
from .platform_fulfillment.account_selector import (
|
||||||
acquire_account_lease,
|
acquire_account_lease,
|
||||||
active_lease_for_account,
|
active_lease_for_account,
|
||||||
has_recent_exchange,
|
capability_candidate_accounts,
|
||||||
live_candidate_accounts,
|
live_candidate_accounts,
|
||||||
release_lease,
|
release_lease,
|
||||||
)
|
)
|
||||||
@@ -146,7 +148,9 @@ STATUS_FLOW: tuple[str, ...] = (
|
|||||||
|
|
||||||
SOURCE_TYPE_PLATFORM_FULFILLMENT = "platform_fulfillment"
|
SOURCE_TYPE_PLATFORM_FULFILLMENT = "platform_fulfillment"
|
||||||
ACCOUNT_REPLACE_ROLES = frozenset({"super_admin", "operation"})
|
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(
|
PLATFORM_TAKEOVER_BLOCKING_TASK_TYPES = frozenset(
|
||||||
{
|
{
|
||||||
"lock_goods",
|
"lock_goods",
|
||||||
@@ -587,6 +591,24 @@ def unlink_account(
|
|||||||
return order
|
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(
|
def recommend_accounts(
|
||||||
db: Session,
|
db: Session,
|
||||||
current: User,
|
current: User,
|
||||||
@@ -619,155 +641,201 @@ def recommend_accounts(
|
|||||||
1,
|
1,
|
||||||
min(
|
min(
|
||||||
int(scan_limit or ACCOUNT_RECOMMEND_DEFAULT_SCAN_LIMIT),
|
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,
|
db,
|
||||||
limit=effective_scan_limit,
|
limit=effective_scan_limit,
|
||||||
required_points=required,
|
required_points=required,
|
||||||
commodity_id=cid,
|
commodity_id=cid,
|
||||||
prefer_unbound=True,
|
prefer_unbound=True,
|
||||||
empty_only=not include_rebindable,
|
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)
|
config = resolve_config(db)
|
||||||
manual_id = str(config.manual_id or "")
|
config_info = config_mapping(
|
||||||
rid = str(config.rid or "")
|
{
|
||||||
act_alias = str(config.bind_act_alias or "")
|
"manual_id": str(config.manual_id or ""),
|
||||||
confirm_act_alias = str(config.confirm_act_alias or "")
|
"rid": str(config.rid or ""),
|
||||||
legacy_act_alias = str(config.legacy_act_alias 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]] = []
|
output: list[dict[str, Any]] = []
|
||||||
scanned = 0
|
scanned = 0
|
||||||
|
result_limit = max(1, min(int(limit), 10))
|
||||||
|
|
||||||
for account in candidates:
|
def inspect_candidate(account: Account) -> tuple[Account, dict[str, Any]] | None:
|
||||||
if order.account_id and int(account.id) == int(order.account_id):
|
|
||||||
continue
|
|
||||||
scanned += 1
|
|
||||||
try:
|
try:
|
||||||
probe = _probe_with_client(
|
active_client = (
|
||||||
account,
|
client_factory(account)
|
||||||
manual_id=manual_id,
|
if client_factory
|
||||||
rid=rid,
|
else activity_client(
|
||||||
act_alias=act_alias,
|
str(account.cookie or ""),
|
||||||
confirm_act_alias=confirm_act_alias,
|
logger_callback=lambda message: logger.debug(
|
||||||
legacy_act_alias=legacy_act_alias,
|
"[douyu-collab] recommend account={} {}",
|
||||||
client=client_factory(account) if client_factory else None,
|
account.id,
|
||||||
with_goods=bool(cid),
|
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 - 单账号失败继续扫描
|
except Exception as exc: # noqa: BLE001 - 单账号失败继续扫描
|
||||||
logger.info(
|
logger.info(
|
||||||
"[douyu-collab] 推荐账号跳过 account_id={} error={}",
|
"[douyu-collab] 推荐账号跳过 account_id={} error={}",
|
||||||
account.id,
|
account.id,
|
||||||
exc,
|
exc,
|
||||||
)
|
)
|
||||||
continue
|
return None
|
||||||
|
|
||||||
if str(probe.get("ck_status") or "") != CK_VALID:
|
def accept_candidate(account: Account, probe: dict[str, Any]) -> None:
|
||||||
continue
|
session = probe.get("session") or {}
|
||||||
role_status = str(probe.get("role_status") or ROLE_UNKNOWN)
|
if str(session.get("status") or "") != SESSION_VALID:
|
||||||
canonical = probe.get("_probe_result")
|
return
|
||||||
canonical_role = (
|
role = (probe.get("roles") or {}).get("elite") or {}
|
||||||
(canonical.get("roles") or {}).get("elite") or {}
|
role_status = str(role.get("status") or ROLE_UNKNOWN)
|
||||||
if isinstance(canonical, Mapping)
|
|
||||||
else {}
|
|
||||||
)
|
|
||||||
bind_info = (
|
bind_info = (
|
||||||
canonical_role.get("bind_info")
|
role.get("bind_info") if isinstance(role.get("bind_info"), dict) else None
|
||||||
if isinstance(canonical_role.get("bind_info"), dict)
|
|
||||||
else None
|
|
||||||
)
|
)
|
||||||
if role_status == ROLE_UNBOUND:
|
if role_status == ROLE_UNBOUND:
|
||||||
match_type = "empty"
|
match_type = "empty"
|
||||||
match_label = "未绑定"
|
match_label = "未绑定"
|
||||||
elif role_status == ROLE_BOUND and can_change_role(bind_info):
|
elif role_status == ROLE_BOUND and can_change_role(bind_info):
|
||||||
if not include_rebindable:
|
if not include_rebindable:
|
||||||
continue
|
return
|
||||||
match_type = "rebind"
|
match_type = "rebind"
|
||||||
match_label = "可换绑"
|
match_label = "可换绑"
|
||||||
else:
|
else:
|
||||||
continue
|
return
|
||||||
|
|
||||||
canonical_eligibility = (
|
eligibility = probe.get("eligibility") or {}
|
||||||
canonical.get("eligibility") or {}
|
|
||||||
if isinstance(canonical, Mapping)
|
|
||||||
else {}
|
|
||||||
)
|
|
||||||
limited_ids = {
|
limited_ids = {
|
||||||
str(item)
|
str(item)
|
||||||
for item in canonical_eligibility.get("limited_ids") or []
|
for item in eligibility.get("limited_ids") or []
|
||||||
if str(item)
|
if str(item)
|
||||||
}
|
}
|
||||||
if cid and cid in limited_ids:
|
if cid and cid in limited_ids:
|
||||||
continue
|
return
|
||||||
if cid and has_recent_exchange(
|
|
||||||
db,
|
|
||||||
account_id=int(account.id),
|
|
||||||
commodity_id=cid,
|
|
||||||
):
|
|
||||||
continue
|
|
||||||
|
|
||||||
goods = probe.get("goods") or []
|
elite = probe.get("elite") or {}
|
||||||
target_goods = next(
|
live_points = _to_int(eligibility.get("points"))
|
||||||
(
|
if live_points is None:
|
||||||
item
|
live_points = _to_int(elite.get("manual_score"))
|
||||||
for item in goods
|
elite_opened = bool(elite.get("opened"))
|
||||||
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
|
|
||||||
ready = bool(
|
ready = bool(
|
||||||
elite_opened_value is True
|
elite_opened and live_points is not None and live_points >= required
|
||||||
and live_points is not None
|
|
||||||
and live_points >= required
|
|
||||||
)
|
)
|
||||||
reasons = [
|
reasons = [
|
||||||
"未绑定角色,可直接绑定"
|
"未绑定角色,可直接绑定"
|
||||||
if match_type == "empty"
|
if match_type == "empty"
|
||||||
else "已绑定角色,当前可换绑"
|
else "已绑定角色,当前可换绑"
|
||||||
]
|
]
|
||||||
if elite_opened_value is not True:
|
if not elite_opened:
|
||||||
reasons.append("精英宝典未开通")
|
reasons.append("精英宝典未开通")
|
||||||
if live_points is None or live_points < required:
|
if live_points is None or live_points < required:
|
||||||
reasons.append(f"积分不足({live_points or 0}/{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(
|
output.append(
|
||||||
{
|
{
|
||||||
"account_id": int(account.id),
|
"account_id": int(account.id),
|
||||||
# 后续“使用此账号”会按内部 username 反查,不能用接口返回的展示昵称。
|
# 后续“使用此账号”会按内部 username 反查,不能用接口返回的展示昵称。
|
||||||
"account_username": str(account.username or ""),
|
"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(
|
"account_nickname": str(
|
||||||
probe.get("account_nickname")
|
identity.get("nickname")
|
||||||
or account.nickname
|
or account.nickname
|
||||||
or account.username
|
or account.username
|
||||||
or ""
|
or ""
|
||||||
),
|
),
|
||||||
"ck_status": str(probe.get("ck_status") or ""),
|
"ck_status": CK_VALID,
|
||||||
"role_status": role_status,
|
"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),
|
"role_change_available_at": role_change_available_at(bind_info),
|
||||||
"points": live_points,
|
"points": live_points,
|
||||||
"elite_opened": elite_opened_value,
|
"elite_opened": elite_opened,
|
||||||
"match_type": match_type,
|
"match_type": match_type,
|
||||||
"match_type_label": match_label,
|
"match_type_label": match_label,
|
||||||
"ready": ready,
|
"ready": ready,
|
||||||
"reasons": reasons,
|
"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(
|
output.sort(
|
||||||
key=lambda item: (
|
key=lambda item: (
|
||||||
|
|||||||
Reference in New Issue
Block a user