修复(斗鱼登录): 邮箱验证码按攒批投递放大等待窗口,取码超时不再重发
生产实测斗鱼验证码邮件是攒批投递的:早高峰延迟 6~20 秒,高峰劣化到 8~13 分钟,同批请求的邮件会在同一分钟内成批到达。抽 40 个「超时」账号核对邮箱, 30 个其实收到了邮件(75%),只是迟到;另有 25% 在批次内根本没投递。 原实现等 60 秒就判超时,并在超时后换通道重试 —— 重试 = 重新发信,把邮件队列 压得更长:每账号发信量从 1.5 封涨到 3.3 封,延迟从 10 秒级恶化到 8~13 分钟, 取码成功率从 100% 掉到 20%。两个通道写的是同一个队列,发两次不会多拿码。 - 等待窗口 60 → 900 秒,新增补等 600 秒(不重发,只等迟到邮件) - 取码超时不换通道、不整轮重试;只有发信/提交接口报错才切 iframe/wgapi - 自动重登与接入作业不再对取码失败账号切 iframe 重跑 - 单账号总时长上限 240/300 → 1800 秒,等待接入 deadline 约束 - 文档记录实测机制与判定依据 验证:1727 passed / 6 skipped,前端 tsc -b 通过,pyright 生产代码 0 错误
This commit is contained in:
@@ -116,7 +116,8 @@ def test_run_account_onboarding_returns_batch_summary(db, monkeypatch):
|
||||
assert result["detail"]["fallbackBatchIds"] == ["batch-iframe"]
|
||||
|
||||
|
||||
def test_email_fallback_switches_to_iframe_only_for_email_code(db, monkeypatch):
|
||||
def test_email_code_failure_does_not_resend_via_iframe(db, monkeypatch):
|
||||
"""取码失败不再切 iframe 重跑:重跑 = 重新发信,会把斗鱼邮件队列压更长。"""
|
||||
from web.backend.services import login_automation as automation
|
||||
|
||||
calls: list[tuple[str, list[int]]] = []
|
||||
@@ -144,11 +145,12 @@ def test_email_fallback_switches_to_iframe_only_for_email_code(db, monkeypatch):
|
||||
created_by=1,
|
||||
)
|
||||
|
||||
assert calls == [("wgapi", [11, 12, 13]), ("iframe", [11])]
|
||||
assert outcome.success_ids == [11, 13]
|
||||
assert outcome.failed_ids == [12]
|
||||
assert outcome.failure_kinds == {12: "credential"}
|
||||
assert outcome.fallback_success_ids == [11]
|
||||
assert calls == [("wgapi", [11, 12, 13])]
|
||||
assert outcome.success_ids == [13]
|
||||
assert outcome.failed_ids == [11, 12]
|
||||
assert outcome.failure_kinds == {11: "email_code", 12: "credential"}
|
||||
assert outcome.fallback_success_ids == []
|
||||
assert outcome.fallback_used is False
|
||||
|
||||
|
||||
def test_onboarding_job_is_registered_but_default_off():
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
"""邮箱验证码渠道回退:默认通道失败后自动换另一条。
|
||||
"""邮箱验证码通道切换:只有接口级错误才换通道。
|
||||
|
||||
运营以前要在「登录设置 → 登录接口」里手工切 wgapi / iframe:某个渠道收不到
|
||||
斗鱼的验证码邮件时,整批账号都会卡在同一个渠道上。现在的规则是:**先按人工
|
||||
设置的默认通道登录**,只有它在取码这一步失败后才自动切到另一条再试一次。
|
||||
运营以前要在「登录设置 → 登录接口」里手工切 wgapi / iframe。现在的规则:
|
||||
|
||||
1. **发信/提交验证码接口本身报错** → 换另一条通道再试一次;
|
||||
2. **取码等超时** → 不换通道、不重新发信:斗鱼验证码是攒批投递的,迟到的
|
||||
邮件还在路上,重新发信只会把队列压更长(生产实测每账号发信量 1.5 → 3.3
|
||||
封,延迟从 10 秒级恶化到 8~13 分钟)。改由补等迟到邮件 + 批次级退避重试。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -37,8 +40,8 @@ def _stub(monkeypatch, client: DouyuLogin) -> None:
|
||||
monkeypatch.setattr(client, "_sleep_interruptible", lambda _s: None)
|
||||
|
||||
|
||||
def test_iframe_default_switches_to_wgapi_after_code_timeouts(monkeypatch):
|
||||
"""默认通道是 iframe 时:先用 iframe,两轮收不到码后切 wgapi。"""
|
||||
def test_code_timeout_does_not_switch_channel(monkeypatch):
|
||||
"""取码超时不换通道:换通道会重新发信,把斗鱼邮件队列压得更长。"""
|
||||
monkeypatch.delenv("DOUYU_LOGIN_EMAIL_FALLBACK_API", raising=False)
|
||||
client = _login("iframe")
|
||||
assert client.api.name == "iframe"
|
||||
@@ -46,49 +49,27 @@ def test_iframe_default_switches_to_wgapi_after_code_timeouts(monkeypatch):
|
||||
|
||||
def steps(deadline: float = 0) -> str:
|
||||
calls.append(client.api.name)
|
||||
if len(calls) <= 2:
|
||||
raise EmailCodeTimeoutError("等待验证码超时")
|
||||
return "cookie"
|
||||
raise EmailCodeTimeoutError("等待验证码超时")
|
||||
|
||||
monkeypatch.setattr(client, "_run_login_steps", steps)
|
||||
_stub(monkeypatch, client)
|
||||
|
||||
result = client.login()
|
||||
|
||||
assert result.success is True
|
||||
assert client.api.name == "wgapi"
|
||||
assert calls == ["iframe", "iframe", "wgapi"]
|
||||
assert result.success is False
|
||||
assert result.code == "email_code_timeout"
|
||||
assert client.api.name == "iframe"
|
||||
# 只跑一轮:不再靠「整轮重试」去重发邮件
|
||||
assert calls == ["iframe"]
|
||||
|
||||
|
||||
def test_wgapi_default_switches_to_iframe(monkeypatch):
|
||||
"""默认通道是 wgapi 时:反向切到 iframe。"""
|
||||
def test_code_timeout_does_not_resend_email(monkeypatch):
|
||||
"""wgapi 默认通道下,取码超时同样只跑一轮(只发一封信)。"""
|
||||
monkeypatch.delenv("DOUYU_LOGIN_EMAIL_FALLBACK_API", raising=False)
|
||||
client = _login()
|
||||
assert client.api.name == "wgapi"
|
||||
calls: list[str] = []
|
||||
|
||||
def steps(deadline: float = 0) -> str:
|
||||
calls.append(client.api.name)
|
||||
if len(calls) <= 2:
|
||||
raise EmailCodeTimeoutError("等待验证码超时")
|
||||
return "cookie"
|
||||
|
||||
monkeypatch.setattr(client, "_run_login_steps", steps)
|
||||
_stub(monkeypatch, client)
|
||||
|
||||
result = client.login()
|
||||
|
||||
assert result.success is True
|
||||
assert client.api.name == "iframe"
|
||||
assert calls == ["wgapi", "wgapi", "iframe"]
|
||||
|
||||
|
||||
def test_fallback_only_switches_once(monkeypatch):
|
||||
"""两条通道都收不到码时收口,不能在 wgapi/iframe 之间来回打转。"""
|
||||
monkeypatch.delenv("DOUYU_LOGIN_EMAIL_FALLBACK_API", raising=False)
|
||||
client = _login("iframe")
|
||||
calls: list[str] = []
|
||||
|
||||
def steps(deadline: float = 0) -> str:
|
||||
calls.append(client.api.name)
|
||||
raise EmailCodeTimeoutError("等待验证码超时")
|
||||
@@ -101,18 +82,45 @@ def test_fallback_only_switches_once(monkeypatch):
|
||||
assert result.success is False
|
||||
assert result.code == "email_code_timeout"
|
||||
assert client.api.name == "wgapi"
|
||||
# iframe 两轮 + wgapi 两轮,之后直接判定失败
|
||||
assert calls == ["iframe", "iframe", "wgapi", "wgapi"]
|
||||
assert calls == ["wgapi"]
|
||||
|
||||
|
||||
def test_late_mail_is_rescued_without_resending(monkeypatch):
|
||||
"""首轮等超时后补等一轮,拿到迟到验证码;全程只发一次信。"""
|
||||
monkeypatch.delenv("DOUYU_LOGIN_EMAIL_FALLBACK_API", raising=False)
|
||||
client = _login("wgapi")
|
||||
|
||||
waits: list[int] = []
|
||||
|
||||
class _LateVerifier:
|
||||
def get_verification_code(self, max_wait: int, **kwargs):
|
||||
waits.append(max_wait)
|
||||
if len(waits) == 1:
|
||||
raise TimeoutError("等待验证码超时")
|
||||
return "123456"
|
||||
|
||||
code = client._wait_for_email_code(
|
||||
_LateVerifier(), # type: ignore[arg-type]
|
||||
baseline_uid=7,
|
||||
email_sent_at=0.0,
|
||||
)
|
||||
|
||||
assert code == "123456"
|
||||
# 首轮按配置窗口等,超时后补等一轮;两次等待都不是「重新发信」
|
||||
assert len(waits) == 2
|
||||
assert waits[0] == DouyuLogin._email_code_wait_seconds()
|
||||
assert waits[1] == DouyuLogin._email_code_rescue_seconds()
|
||||
|
||||
|
||||
def test_fallback_can_be_disabled_with_env(monkeypatch):
|
||||
"""关闭自动切换后,接口级错误也不再换通道。"""
|
||||
monkeypatch.setenv("DOUYU_LOGIN_EMAIL_FALLBACK_API", "off")
|
||||
client = _login("iframe")
|
||||
calls: list[str] = []
|
||||
|
||||
def steps(deadline: float = 0) -> str:
|
||||
calls.append(client.api.name)
|
||||
raise EmailCodeTimeoutError("等待验证码超时")
|
||||
raise ValueError("发送验证邮件失败: 服务繁忙")
|
||||
|
||||
monkeypatch.setattr(client, "_run_login_steps", steps)
|
||||
_stub(monkeypatch, client)
|
||||
@@ -121,7 +129,8 @@ def test_fallback_can_be_disabled_with_env(monkeypatch):
|
||||
|
||||
assert result.success is False
|
||||
assert client.api.name == "iframe"
|
||||
assert calls == ["iframe", "iframe"]
|
||||
# 一直在 iframe 上重试,从没切到 wgapi
|
||||
assert set(calls) == {"iframe"}
|
||||
|
||||
|
||||
def test_send_email_failure_also_switches_channel(monkeypatch):
|
||||
|
||||
@@ -49,7 +49,7 @@ def test_must_bind_mobile_is_terminal(monkeypatch):
|
||||
|
||||
|
||||
def test_email_code_timeout_stops_after_attempt_limit(monkeypatch):
|
||||
"""每条发信通道只给 2 轮,两条都失败就收口,不会拖到 20 轮。"""
|
||||
"""取码超时不再整轮重试:重试=重新发信,会把斗鱼邮件队列压更长。"""
|
||||
monkeypatch.delenv("DOUYU_LOGIN_EMAIL_FALLBACK_API", raising=False)
|
||||
client = _login()
|
||||
calls: list[int] = []
|
||||
@@ -66,10 +66,9 @@ def test_email_code_timeout_stops_after_attempt_limit(monkeypatch):
|
||||
|
||||
assert result.success is False
|
||||
assert result.code == "email_code_timeout"
|
||||
# 默认通道 + 自动切换后的另一条通道,各 2 轮
|
||||
assert len(calls) == client.EMAIL_CODE_MAX_ATTEMPTS * 2
|
||||
assert client.api.name == "iframe"
|
||||
assert client.EMAIL_CODE_MAX_ATTEMPTS * 2 < 20
|
||||
# 只跑一轮,不再靠重试补发邮件;重试交给批次级退避
|
||||
assert len(calls) == 1
|
||||
assert client.api.name == "wgapi"
|
||||
|
||||
|
||||
def test_retryable_error_still_retries(monkeypatch):
|
||||
@@ -162,8 +161,17 @@ def test_missing_email_fails_fast_before_waiting(monkeypatch):
|
||||
|
||||
def test_email_wait_seconds_is_configurable(monkeypatch):
|
||||
monkeypatch.delenv("DOUYU_EMAIL_CODE_WAIT_SECONDS", raising=False)
|
||||
assert DouyuLogin._email_code_wait_seconds() == 60
|
||||
# 默认 900 秒:斗鱼验证码是攒批投递的,高峰延迟实测 8~13 分钟
|
||||
assert DouyuLogin._email_code_wait_seconds() == 900
|
||||
monkeypatch.setenv("DOUYU_EMAIL_CODE_WAIT_SECONDS", "1800")
|
||||
assert DouyuLogin._email_code_wait_seconds() == 1800
|
||||
# 下限 60:给再小也会把「邮件在路上」误判成没收到
|
||||
monkeypatch.setenv("DOUYU_EMAIL_CODE_WAIT_SECONDS", "15")
|
||||
assert DouyuLogin._email_code_wait_seconds() == 15
|
||||
monkeypatch.setenv("DOUYU_EMAIL_CODE_WAIT_SECONDS", "0")
|
||||
assert DouyuLogin._email_code_wait_seconds() == 5
|
||||
assert DouyuLogin._email_code_wait_seconds() == 60
|
||||
|
||||
|
||||
def test_email_rescue_seconds_is_configurable(monkeypatch):
|
||||
monkeypatch.delenv("DOUYU_EMAIL_CODE_RESCUE_SECONDS", raising=False)
|
||||
assert DouyuLogin._email_code_rescue_seconds() == 600
|
||||
monkeypatch.setenv("DOUYU_EMAIL_CODE_RESCUE_SECONDS", "0")
|
||||
assert DouyuLogin._email_code_rescue_seconds() == 0
|
||||
|
||||
Reference in New Issue
Block a user