feat(seckill): 增加只读预演并强化代理风控切换
This commit is contained in:
@@ -34,3 +34,6 @@ Do not launch browsers or browser automation for UI validation unless explicitly
|
||||
|
||||
## Proxy Documentation
|
||||
`docs/代理池与白名单.md` is the source of truth for proxy extraction, whitelist handling, account grouping, standby proxies, runtime failover, and proxy scheduling. Any change to `internal/proxy/`, `internal/seckill/proxy_groups.go`, proxy timing/settings, or proxy logs/statistics must update that document in the same change.
|
||||
|
||||
## Seckill Drill Documentation
|
||||
`docs/抢购预演.md` is the source of truth for the read-only seckill rehearsal. Any change to rehearsal timing, account requests, proxy preparation, cancellation, or homepage drill status must update that document in the same change.
|
||||
|
||||
@@ -72,6 +72,8 @@ GOOS=linux GOARCH=amd64 wails build
|
||||
|
||||
代理默认启用严格模式:定时任务提前 2 分钟准备,代理未就绪时不启动且不回退直连;品赞提取强制 JSON,同机多实例共享白名单协调锁,主界面直接显示 `G1`、`G2` 或“直连”。
|
||||
|
||||
主界面“抢购预演”按正式时序执行:`T-120s` 准备真实账号和代理,`T-2s` 预热,`T0` 执行只读账号请求;不会预兑换、锁单、支付或消耗积分。详细说明见 [docs/抢购预演.md](docs/抢购预演.md)。
|
||||
|
||||
构建并启动控制中心:
|
||||
|
||||
```bash
|
||||
|
||||
+34
-1
@@ -204,6 +204,22 @@
|
||||
|
||||
`whiteList-verify` 返回的业务错误也会保留原始消息并执行同样的致命/可重试分类,不再把所有非零 `code` 都判定为致命错误。
|
||||
|
||||
### 准备阶段业务风控探测
|
||||
|
||||
代理池校验完成后、标记为就绪前,会对每个活动组执行只读 `batchExchangeLimit`。组内第一个账号无法取得明确业务响应时,会继续尝试同组其他账号:
|
||||
|
||||
- Cookie 已有 CSRF 时直接复用。
|
||||
- `1644400007` 来自斗鱼接口响应 JSON 的 `error` 字段,不是本地生成;日志现在会显示 `error=1644400007`。
|
||||
- 错误码是 `1644400007`、错误文案包含“活动火爆”或纯文本中出现该数字状态码时,都视为当前代理出口被风控。
|
||||
- 同一出口 IP 的代理组一起切换。
|
||||
- 备用代理优先选择不同公网 IP;只有相同 IP 的备用时不直接使用。
|
||||
- 切换后必须重新检查全部活动组,不能只检查被替换的单组。
|
||||
- 新代理仍返回活动火爆时继续切换;备用耗尽时同步调用提取 API 获取新代理,再重新检查。
|
||||
- 补提取数量按“替换失败组所需数量 + 备用目标缺口”计算,避免替换完成后没有剩余备用。
|
||||
- 任一活动组在截止时间前没有拿到明确业务响应,严格模式保持未就绪并阻止启动。
|
||||
- `1644400011 您点击太快` 和商品售罄不等于代理风控。
|
||||
- 电竞手册和和平小店没有可用的批量限制接口,启动前只做各自只读业务连通性校验。
|
||||
|
||||
## 8. 账号分组
|
||||
|
||||
账号按名称排序后顺序分组,保证相同账号集合尽量得到稳定分组。
|
||||
@@ -362,10 +378,15 @@ T-0s 本地定时触发
|
||||
8. 运行期间每 20 秒探测一次备用代理,连续 2 次失败后移出备用池并补提取。
|
||||
9. 严格模式失败组等待备用期间,该组账号暂停发请求,不继续消耗故障代理和抢购次数。
|
||||
10. 补提取失败后按 2 秒节奏继续重试,直到任务停止、代理恢复或代理池失效。
|
||||
11. 锁定、支付、兑换、CSRF 或批量限制接口返回 `1644400007` / “活动火爆”时,不等第二次错误,立即按出口 IP 切换代理组。
|
||||
12. `1644400011 您点击太快` 仍然只触发账号级降频,不切换代理。
|
||||
13. 直连模式出现活动火爆不会触发代理切换。
|
||||
|
||||
非严格模式已经主动切换到直连的组,不会在运行中自动切回代理;严格模式保留失败组状态,因此可以在新备用到达后重新绑定。
|
||||
|
||||
运行前门禁会检查代理池是否过期、是否被配置更新、是否存在失败组,以及是否覆盖本次选择的全部账号。备用数量不足不会导致已覆盖账号的活动代理被重建。
|
||||
运行前门禁会检查代理池是否过期、是否被配置更新、是否存在失败组、是否完成逐组业务风控预检,以及是否覆盖本次选择的全部账号。代理切换会清除预检状态,必须重新通过后才能启动。备用数量不足不会导致已覆盖账号的活动代理被重建。
|
||||
|
||||
业务错误必须保留斗鱼错误码。代理风控识别明确的 `1644400007` 或“活动火爆”文案;不能仅凭“繁忙”“稍后再试”等宽泛文案切换,避免把普通频控误判为代理故障。
|
||||
|
||||
## 13. 代理有效期
|
||||
|
||||
@@ -389,9 +410,16 @@ T-0s 本地定时触发
|
||||
- 不支付
|
||||
- 不消耗积分
|
||||
- 抢购运行中禁止执行
|
||||
- 最多持续 2 分钟,避免提取接口限频时无限等待
|
||||
|
||||
演练可模拟账号数,计算活动代理和备用代理数量,并逐个验证代理节点。
|
||||
|
||||
演练通过口径与正式启动门禁一致:
|
||||
|
||||
- 所有活动代理必须能够并发完成只读业务请求。
|
||||
- 备用代理不足或部分备用不可用时,演练仍标记为可启动,但会显示降级警告。
|
||||
- 任意活动代理失败时演练不通过。
|
||||
|
||||
演练通过只说明当前时点可用;不能保证开抢瞬间不会集中故障。
|
||||
|
||||
## 15. 2026-11-21 实测摘要
|
||||
@@ -657,3 +685,8 @@ cd frontend && npm run type-check
|
||||
| 2026-11-21 | 备用不足不得导致活动代理池重复提取 | 已实现 |
|
||||
| 2026-11-21 | 严格模式失败组暂停请求并持续补提取,不空打故障代理 | 已实现 |
|
||||
| 2026-11-21 | 运行期持续探测备用代理,失败节点移出备用池 | 已实现 |
|
||||
| 2026-11-22 | 代理锁定接口返回活动火爆时立即切换出口 IP | 已实现 |
|
||||
| 2026-11-22 | 准备阶段通过只读兑换限制接口探测代理业务风控 | 已实现 |
|
||||
| 2026-11-22 | 启动前逐组预检,切换后重新检查全部代理组,未通过禁止启动 | 已实现 |
|
||||
| 2026-11-22 | 新备用仍活动火爆时同步补提取,并按失败组加备用缺口计算数量 | 已实现 |
|
||||
| 2026-11-22 | 订单业务错误日志保留斗鱼数字错误码 | 已实现 |
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
# 抢购预演
|
||||
|
||||
抢购预演用于验证正式抢购前的本地链路,不执行任何兑换动作。
|
||||
|
||||
## 使用方式
|
||||
|
||||
主界面点击“抢购预演”后:
|
||||
|
||||
1. 使用当前勾选账号和商品建立任务。
|
||||
2. 点击时作为模拟 `T-120s`,立即准备并校验代理池。
|
||||
3. 以点击后 2 分钟作为模拟 `T0`,进入倒计时;代理准备耗时计入这 2 分钟。
|
||||
4. `T-2s` 使用当前账号 Cookie 执行预热。
|
||||
5. `T0` 按配置并发数执行账号级只读请求。
|
||||
6. 显示成功、失败和完成进度。
|
||||
|
||||
## 代理风控检测
|
||||
|
||||
抢购预演与正式抢购共用同一套代理准备和启动门禁:
|
||||
|
||||
- 准备阶段逐组执行只读 `batchExchangeLimit`。
|
||||
- 返回 `1644400007` 或“活动火爆”时,立即切换备用代理。
|
||||
- 新代理仍命中活动火爆时继续切换;备用不足时同步提取新代理。
|
||||
- 切换后重新检查全部活动组,无法全部通过则预演直接失败,不进入倒计时。
|
||||
- `T0` 的只读请求如果再次命中活动火爆,预演会记录失败并触发同一套代理切换逻辑。
|
||||
|
||||
## 请求边界
|
||||
|
||||
允许调用:
|
||||
|
||||
- CSRF/CTN 获取。
|
||||
- 角色绑定信息查询。
|
||||
- 批量兑换限制查询。
|
||||
- 和平小店角色查询。
|
||||
- 代理时间或商品接口探测。
|
||||
|
||||
禁止调用:
|
||||
|
||||
- 预兑换。
|
||||
- 创建兑换订单。
|
||||
- 兑换。
|
||||
- 支付。
|
||||
|
||||
预演不会写入真实抢购结果,也不会自动修改积分或点券。
|
||||
|
||||
## 优先级
|
||||
|
||||
- 真实抢购运行中不能启动预演。
|
||||
- 预演等待或执行期间,如果真实抢购启动,预演立即终止。
|
||||
- 预演使用独立状态,不占用真实抢购运行锁。
|
||||
- 关闭应用或停止调度器时自动取消预演。
|
||||
@@ -5,7 +5,8 @@ import { useAccounts } from '@/composables/useAccounts'
|
||||
import { useActivity } from '@/composables/useActivity'
|
||||
import { useProducts } from '@/composables/useProducts'
|
||||
import { useToast } from '@/composables/useToast'
|
||||
import { GetResults } from '@wails/go/app/App'
|
||||
import { CancelSeckillDrill, GetResults, GetSeckillDrillStatus, StartSeckillDrill } from '@wails/go/app/App'
|
||||
import { types } from '@wails/go/models'
|
||||
|
||||
const { scheduleActive, seckillRunning, startNow, stopSeckill, startFixed, startSchedule, cancelSchedule, restoreSchedule, subscribeSeckillState, unsubscribeSchedulerEvents } = useScheduler()
|
||||
const {
|
||||
@@ -50,6 +51,9 @@ let stockWatchTimer: number | null = null
|
||||
let stockWatchStartTimer: number | null = null
|
||||
let stockRefreshing = false
|
||||
let stockWatchSettling = false
|
||||
const seckillDrill = ref<types.SeckillDrillStatus>(types.SeckillDrillStatus.createFrom({ state: 'idle' }))
|
||||
const drillStarting = ref(false)
|
||||
let drillStatusTimer: number | null = null
|
||||
|
||||
// 从本地缓存恢复定时设置
|
||||
const { savedTime } = restoreSchedule()
|
||||
@@ -57,6 +61,7 @@ const targetTime = ref(savedTime || '18:00:00')
|
||||
|
||||
onMounted(() => {
|
||||
subscribeSeckillState()
|
||||
void refreshSeckillDrill()
|
||||
})
|
||||
|
||||
const handleMainAction = async () => {
|
||||
@@ -71,6 +76,78 @@ const handleSchedule = () => {
|
||||
startSchedule(targetTime.value, fixedAttempts.value, fixedDelayMs.value)
|
||||
}
|
||||
|
||||
const drillStateLabel = computed(() => {
|
||||
const labels: Record<string, string> = {
|
||||
idle: '未开始',
|
||||
preparing: '准备代理',
|
||||
scheduled: '等待触发',
|
||||
warming: '预热中',
|
||||
running: '并发预演中',
|
||||
completed: '预演完成',
|
||||
failed: '预演失败',
|
||||
cancelled: '已取消'
|
||||
}
|
||||
return labels[seckillDrill.value.state] || seckillDrill.value.state
|
||||
})
|
||||
|
||||
const drillCountdown = computed(() => {
|
||||
const seconds = Math.max(0, seckillDrill.value.remainingSeconds || 0)
|
||||
return `${String(Math.floor(seconds / 60)).padStart(2, '0')}:${String(seconds % 60).padStart(2, '0')}`
|
||||
})
|
||||
|
||||
const stopDrillPolling = () => {
|
||||
if (drillStatusTimer) {
|
||||
clearInterval(drillStatusTimer)
|
||||
drillStatusTimer = null
|
||||
}
|
||||
}
|
||||
|
||||
const ensureDrillPolling = () => {
|
||||
if (drillStatusTimer) return
|
||||
drillStatusTimer = window.setInterval(() => {
|
||||
void refreshSeckillDrill()
|
||||
}, 500)
|
||||
}
|
||||
|
||||
const refreshSeckillDrill = async () => {
|
||||
try {
|
||||
seckillDrill.value = await GetSeckillDrillStatus()
|
||||
if (seckillDrill.value.active) {
|
||||
ensureDrillPolling()
|
||||
} else {
|
||||
stopDrillPolling()
|
||||
}
|
||||
} catch {
|
||||
stopDrillPolling()
|
||||
}
|
||||
}
|
||||
|
||||
const toggleSeckillDrill = async () => {
|
||||
if (seckillDrill.value.active) {
|
||||
await CancelSeckillDrill()
|
||||
await refreshSeckillDrill()
|
||||
showToast('预演已取消', '抢购预演已停止', 'warn')
|
||||
return
|
||||
}
|
||||
const ap = buildAccountProducts(selectedProductId.value || '')
|
||||
if (!ap) return
|
||||
|
||||
drillStarting.value = true
|
||||
showToast('抢购预演', '正在按 T-120s 准备代理,随后倒计时到模拟抢购时刻', 'info', 4000)
|
||||
try {
|
||||
const result = await StartSeckillDrill(ap)
|
||||
if (result.success === false) throw new Error(result.error || '预演启动失败')
|
||||
await refreshSeckillDrill()
|
||||
ensureDrillPolling()
|
||||
showToast('抢购预演已设置', result.msg || '将在 T0 执行只读预演', 'success', 5000)
|
||||
} catch (e: any) {
|
||||
await refreshSeckillDrill()
|
||||
showToast('预演启动失败', e?.message || String(e), 'error')
|
||||
} finally {
|
||||
drillStarting.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const queryAssets = () => {
|
||||
if (isXiaodian.value) {
|
||||
queryAllXiaodianAssets()
|
||||
@@ -364,6 +441,7 @@ watch(seckillRunning, (running, wasRunning) => {
|
||||
onUnmounted(() => {
|
||||
stopKeywordWatch()
|
||||
stopStockWatch()
|
||||
stopDrillPolling()
|
||||
unsubscribeSchedulerEvents()
|
||||
})
|
||||
|
||||
@@ -380,6 +458,14 @@ onUnmounted(() => {
|
||||
>
|
||||
{{ seckillRunning ? '停止抢购' : '立即抢购' }}
|
||||
</button>
|
||||
<button
|
||||
class="btn btn-drill"
|
||||
:class="{ active: seckillDrill.active }"
|
||||
:disabled="drillStarting || (!seckillDrill.active && seckillRunning)"
|
||||
@click="toggleSeckillDrill"
|
||||
>
|
||||
{{ seckillDrill.active ? '取消预演' : (drillStarting ? '准备中...' : '抢购预演') }}
|
||||
</button>
|
||||
<button
|
||||
v-if="!scheduleActive"
|
||||
class="btn btn-success"
|
||||
@@ -406,6 +492,21 @@ onUnmounted(() => {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
v-if="seckillDrill.state !== 'idle'"
|
||||
class="drill-row"
|
||||
:class="{ active: seckillDrill.active, failed: seckillDrill.state === 'failed' || seckillDrill.failedAccounts > 0 }"
|
||||
>
|
||||
<span class="drill-title">抢购预演</span>
|
||||
<strong>{{ drillStateLabel }}</strong>
|
||||
<span v-if="seckillDrill.active">剩余 {{ drillCountdown }}</span>
|
||||
<span v-if="seckillDrill.totalAccounts > 0">
|
||||
完成 {{ seckillDrill.completedAccounts }}/{{ seckillDrill.totalAccounts }} ·
|
||||
成功 {{ seckillDrill.successAccounts }} · 失败 {{ seckillDrill.failedAccounts }}
|
||||
</span>
|
||||
<span v-if="seckillDrill.message" class="drill-message">{{ seckillDrill.message }}</span>
|
||||
</div>
|
||||
|
||||
<div v-if="isXiaodian" class="keyword-row">
|
||||
<span class="settings-title keyword-title">关键词抢购</span>
|
||||
<div class="xiaodian-settings">
|
||||
@@ -594,6 +695,15 @@ onUnmounted(() => {
|
||||
}
|
||||
.buy-title { margin-left: 8px; }
|
||||
.btn-fixed { border-color: var(--warning); color: var(--warning); }
|
||||
.btn-drill {
|
||||
border-color: var(--accent-border);
|
||||
color: var(--accent);
|
||||
}
|
||||
.btn-drill.active {
|
||||
border-color: var(--danger);
|
||||
color: var(--danger);
|
||||
background: var(--danger-soft);
|
||||
}
|
||||
.btn-watch { border-color: var(--accent-border); color: var(--accent); }
|
||||
.btn-stock-watch { border-color: var(--accent-border); color: var(--accent); }
|
||||
.btn-watch.watching,
|
||||
@@ -613,6 +723,40 @@ onUnmounted(() => {
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.drill-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
width: 100%;
|
||||
padding: 7px 10px;
|
||||
border: 1px solid var(--accent-border);
|
||||
background: var(--accent-soft);
|
||||
color: var(--muted);
|
||||
font-size: 11.5px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.drill-row.active {
|
||||
border-color: var(--accent);
|
||||
}
|
||||
|
||||
.drill-row.failed {
|
||||
border-color: var(--danger);
|
||||
background: var(--danger-soft);
|
||||
}
|
||||
|
||||
.drill-title,
|
||||
.drill-row strong {
|
||||
color: var(--text-strong);
|
||||
}
|
||||
|
||||
.drill-message {
|
||||
min-width: 0;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
@media (max-width: 1180px) {
|
||||
.control-strip {
|
||||
flex-direction: column;
|
||||
@@ -621,7 +765,8 @@ onUnmounted(() => {
|
||||
|
||||
.schedule-bar,
|
||||
.action-row,
|
||||
.keyword-row {
|
||||
.keyword-row,
|
||||
.drill-row {
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
|
||||
@@ -161,7 +161,12 @@ const handleProxyDrill = async () => {
|
||||
}))
|
||||
drillResult.value = res
|
||||
if (res.success) {
|
||||
showToast('演练通过', `活动代理 ${res.healthyActive}/${res.activeCount},备用代理 ${res.healthyStandby}/${res.standbyCount}`, 'success', 5000)
|
||||
showToast(
|
||||
res.warning ? '演练通过(有警告)' : '演练通过',
|
||||
res.warning || `活动代理 ${res.healthyActive}/${res.activeCount},备用代理 ${res.healthyStandby}/${res.standbyCount}`,
|
||||
res.warning ? 'warn' : 'success',
|
||||
5000
|
||||
)
|
||||
} else {
|
||||
showToast('演练未通过', res.error || '部分活动代理未通过业务请求验证', 'error', 6000)
|
||||
}
|
||||
@@ -508,8 +513,8 @@ defineExpose({ open, close })
|
||||
</div>
|
||||
|
||||
<div v-if="drillResult" class="proxy-drill-result">
|
||||
<div class="proxy-drill-summary" :class="{ success: drillResult.success, error: !drillResult.success }">
|
||||
<strong>{{ drillResult.success ? '演练通过' : '演练未通过' }}</strong>
|
||||
<div class="proxy-drill-summary" :class="{ success: drillResult.success && !drillResult.warning, warning: !!drillResult.warning, error: !drillResult.success }">
|
||||
<strong>{{ drillResult.success ? (drillResult.warning ? '演练通过(有警告)' : '演练通过') : '演练未通过' }}</strong>
|
||||
<span>
|
||||
{{ drillResult.simulatedAccounts }} 账号 →
|
||||
{{ drillResult.activeCount }} 活动 + {{ drillResult.standbyCount }} 备用
|
||||
@@ -518,6 +523,7 @@ defineExpose({ open, close })
|
||||
<span>耗时 {{ drillResult.durationMs }}ms</span>
|
||||
</div>
|
||||
<div v-if="drillResult.error" class="proxy-drill-error">{{ drillResult.error }}</div>
|
||||
<div v-if="drillResult.warning" class="proxy-drill-warning">{{ drillResult.warning }}</div>
|
||||
<div class="proxy-drill-endpoint" :title="drillResult.endpoint">
|
||||
探测接口:{{ drillResult.endpoint }}
|
||||
</div>
|
||||
@@ -1400,7 +1406,13 @@ defineExpose({ open, close })
|
||||
color: var(--danger);
|
||||
}
|
||||
|
||||
.proxy-drill-error {
|
||||
.proxy-drill-summary.warning strong,
|
||||
.proxy-drill-warning {
|
||||
color: var(--warning);
|
||||
}
|
||||
|
||||
.proxy-drill-error,
|
||||
.proxy-drill-warning {
|
||||
margin-top: 8px;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
@@ -56,7 +56,11 @@ func (a *App) FetchProxy(apiURL string) types.ProxyFetchResult {
|
||||
}
|
||||
|
||||
func extractProxyEndpoints(cfg types.Config, apiURL string, count int) ([]proxyprovider.Endpoint, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
return extractProxyEndpointsWithTimeout(cfg, apiURL, count, 30*time.Second)
|
||||
}
|
||||
|
||||
func extractProxyEndpointsWithTimeout(cfg types.Config, apiURL string, count int, timeout time.Duration) ([]proxyprovider.Endpoint, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
service := proxyprovider.NewService()
|
||||
endpoints, err := service.ExtractWithWhitelist(ctx, apiURL, count, proxyprovider.WhitelistConfig{
|
||||
|
||||
@@ -19,6 +19,7 @@ const (
|
||||
maxProxyDrillAccounts = 200
|
||||
proxyDrillExtractRounds = 3
|
||||
proxyDrillMaxRounds = 20
|
||||
proxyDrillTotalTimeout = 2 * time.Minute
|
||||
)
|
||||
|
||||
type proxyDrillPlan struct {
|
||||
@@ -133,9 +134,14 @@ func (a *App) RunProxyDrill(req types.ProxyDrillRequest) types.ProxyDrillResult
|
||||
result.HealthyStandby++
|
||||
}
|
||||
}
|
||||
result.Success = result.HealthyActive >= plan.active && result.HealthyStandby >= plan.standby
|
||||
if !result.Success {
|
||||
result.Error = "活动代理或备用代理未全部通过演练"
|
||||
result.Success, result.Error, result.Warning = evaluateProxyDrill(
|
||||
plan,
|
||||
result.HealthyActive,
|
||||
result.HealthyStandby,
|
||||
len(endpoints),
|
||||
)
|
||||
if err != nil && result.Warning == "" {
|
||||
result.Warning = err.Error()
|
||||
}
|
||||
result.DurationMs = time.Since(startedAt).Milliseconds()
|
||||
logger.LogEvent("代理", "success", "代理池演练完成: 成功=%v,活动代理=%d/%d,备用代理=%d/%d,耗时=%s",
|
||||
@@ -144,6 +150,24 @@ func (a *App) RunProxyDrill(req types.ProxyDrillRequest) types.ProxyDrillResult
|
||||
return result
|
||||
}
|
||||
|
||||
func evaluateProxyDrill(plan proxyDrillPlan, healthyActive, healthyStandby, extracted int) (bool, string, string) {
|
||||
if healthyActive < plan.active {
|
||||
return false, fmt.Sprintf("活动代理未全部通过演练: %d/%d", healthyActive, plan.active), ""
|
||||
}
|
||||
var warnings []string
|
||||
if extracted < plan.active+plan.standby {
|
||||
warnings = append(warnings, fmt.Sprintf(
|
||||
"备用代理提取不足: %d/%d", max(0, extracted-plan.active), plan.standby,
|
||||
))
|
||||
}
|
||||
if healthyStandby < plan.standby {
|
||||
warnings = append(warnings, fmt.Sprintf(
|
||||
"备用代理健康数不足: %d/%d", healthyStandby, plan.standby,
|
||||
))
|
||||
}
|
||||
return true, "", strings.Join(warnings, ";")
|
||||
}
|
||||
|
||||
func proxyDrillStandbyCount(activeCount, sparePercent int) int {
|
||||
if activeCount <= 0 {
|
||||
return 0
|
||||
@@ -173,8 +197,15 @@ func (a *App) extractProxyDrillPool(cfg types.Config, desired int) ([]proxyprovi
|
||||
seen := make(map[string]struct{}, desired)
|
||||
endpoints := make([]proxyprovider.Endpoint, 0, desired)
|
||||
var lastErr error
|
||||
deadline := time.Now().Add(proxyDrillTotalTimeout)
|
||||
for round := 0; round < rounds && len(endpoints) < desired; round++ {
|
||||
batch, err := extractProxyEndpoints(cfg, cfg.ProxyAPI, desired-len(endpoints))
|
||||
remaining := time.Until(deadline)
|
||||
if remaining <= 0 {
|
||||
lastErr = fmt.Errorf("代理池演练总时长超过 %s", proxyDrillTotalTimeout)
|
||||
break
|
||||
}
|
||||
requestTimeout := min(30*time.Second, remaining)
|
||||
batch, err := extractProxyEndpointsWithTimeout(cfg, cfg.ProxyAPI, desired-len(endpoints), requestTimeout)
|
||||
if err != nil {
|
||||
lastErr = err
|
||||
if proxyprovider.IsFatal(err) {
|
||||
@@ -195,7 +226,10 @@ func (a *App) extractProxyDrillPool(cfg types.Config, desired int) ([]proxyprovi
|
||||
}
|
||||
}
|
||||
if round < rounds-1 {
|
||||
time.Sleep(1200 * time.Millisecond)
|
||||
wait := min(1200*time.Millisecond, time.Until(deadline))
|
||||
if wait > 0 {
|
||||
time.Sleep(wait)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(endpoints) < desired {
|
||||
|
||||
@@ -29,6 +29,39 @@ func TestProxyDrillPlanAndTargets(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestEvaluateProxyDrillMatchesRuntimeGate(t *testing.T) {
|
||||
plan := proxyDrillPlan{active: 4, standby: 2}
|
||||
cases := []struct {
|
||||
name string
|
||||
healthyActive int
|
||||
healthyStandby int
|
||||
extracted int
|
||||
wantSuccess bool
|
||||
wantWarning bool
|
||||
}{
|
||||
{name: "all healthy", healthyActive: 4, healthyStandby: 2, extracted: 6, wantSuccess: true},
|
||||
{name: "standby shortage still starts", healthyActive: 4, healthyStandby: 1, extracted: 5, wantSuccess: true, wantWarning: true},
|
||||
{name: "active failure blocks", healthyActive: 3, healthyStandby: 2, extracted: 6, wantSuccess: false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
success, errMessage, warning := evaluateProxyDrill(plan, tc.healthyActive, tc.healthyStandby, tc.extracted)
|
||||
if success != tc.wantSuccess {
|
||||
t.Fatalf("success=%v, want %v", success, tc.wantSuccess)
|
||||
}
|
||||
if tc.wantSuccess && errMessage != "" {
|
||||
t.Fatalf("unexpected error: %s", errMessage)
|
||||
}
|
||||
if !tc.wantSuccess && errMessage == "" {
|
||||
t.Fatal("active failure should return an error")
|
||||
}
|
||||
if (warning != "") != tc.wantWarning {
|
||||
t.Fatalf("warning=%q, wantWarning=%v", warning, tc.wantWarning)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractProxyDrillPoolDeduplicates(t *testing.T) {
|
||||
apiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
for i := 0; i < 8; i++ {
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"douyu_go/internal/types"
|
||||
)
|
||||
|
||||
// StartSeckillDrill schedules the one-minute read-only seckill rehearsal.
|
||||
func (a *App) StartSeckillDrill(accountProducts map[string]string) types.MessageResult {
|
||||
scheduler := a.getScheduler()
|
||||
if scheduler == nil {
|
||||
return types.MessageResult{Error: "调度器未初始化"}
|
||||
}
|
||||
status, err := scheduler.StartSeckillDrill(accountProducts)
|
||||
if err != nil {
|
||||
return types.MessageResult{Error: err.Error()}
|
||||
}
|
||||
return types.MessageResult{
|
||||
Success: true,
|
||||
Msg: fmt.Sprintf("抢购预演已设置,触发时间 %s", status.TriggerTime),
|
||||
}
|
||||
}
|
||||
|
||||
// GetSeckillDrillStatus returns the current read-only rehearsal state.
|
||||
func (a *App) GetSeckillDrillStatus() types.SeckillDrillStatus {
|
||||
scheduler := a.getScheduler()
|
||||
if scheduler == nil {
|
||||
return types.SeckillDrillStatus{State: "idle"}
|
||||
}
|
||||
status := scheduler.GetSeckillDrillStatus()
|
||||
if status.State == "" {
|
||||
status.State = "idle"
|
||||
}
|
||||
return status
|
||||
}
|
||||
|
||||
// CancelSeckillDrill cancels a pending or running rehearsal.
|
||||
func (a *App) CancelSeckillDrill() types.SimpleResult {
|
||||
scheduler := a.getScheduler()
|
||||
if scheduler == nil || !scheduler.CancelSeckillDrill() {
|
||||
return types.SimpleResult{Success: true}
|
||||
}
|
||||
return types.SimpleResult{Success: true}
|
||||
}
|
||||
+11
-10
@@ -1207,7 +1207,7 @@ func (c *DouyuClient) generateCSRF(cookie string, forceRefresh bool) (string, er
|
||||
traceErr = api.Msg
|
||||
c.Trace.RecordResponse("生成CSRF", req, resp, resp.StatusCode, time.Since(start), reqBody, body, traceErr, api.Error, api.Msg)
|
||||
c.logNetworkAPIResult("生成CSRF", req, time.Since(start), api.Error, api.Msg)
|
||||
return "", fmt.Errorf("CSRF 接口失败: %s", api.Msg)
|
||||
return "", &OrderError{Kind: "CSRF接口失败", Code: api.Error, Msg: api.Msg}
|
||||
}
|
||||
for _, ck := range resp.Cookies() {
|
||||
if ck.Name == "cvl_csrf_token" && ck.Value != "" {
|
||||
@@ -1306,7 +1306,7 @@ func (c *DouyuClient) BatchExchangeLimit(cookie, commodityID string, csrf *strin
|
||||
return 0, err
|
||||
}
|
||||
if result.Error != 0 {
|
||||
return 0, fmt.Errorf("批量限制接口失败: %s", result.Msg)
|
||||
return 0, &OrderError{Kind: "批量限制接口失败", Code: result.Error, Msg: result.Msg}
|
||||
}
|
||||
var data struct {
|
||||
Limit string `json:"limit"`
|
||||
@@ -1344,7 +1344,7 @@ func (c *DouyuClient) PreExchange(cookie, commodityID string, csrf *string) erro
|
||||
return err
|
||||
}
|
||||
if result.Error != 0 {
|
||||
return fmt.Errorf("预兑换失败: %s", result.Msg)
|
||||
return &OrderError{Kind: "预兑换失败", Code: result.Error, Msg: result.Msg}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1362,10 +1362,14 @@ type OrderError struct {
|
||||
}
|
||||
|
||||
func (e *OrderError) Error() string {
|
||||
if strings.TrimSpace(e.Msg) == "" {
|
||||
return e.Kind
|
||||
kind := e.Kind
|
||||
if e.Code != 0 {
|
||||
kind += fmt.Sprintf("(error=%d)", e.Code)
|
||||
}
|
||||
return e.Kind + ": " + e.Msg
|
||||
if strings.TrimSpace(e.Msg) == "" {
|
||||
return kind
|
||||
}
|
||||
return kind + ": " + e.Msg
|
||||
}
|
||||
|
||||
// LockedExchangeOrder 是精英手册锁单结果。
|
||||
@@ -1581,10 +1585,7 @@ func (c *DouyuClient) Exchange(cookie, commodityID string, csrf *string) (string
|
||||
}
|
||||
}
|
||||
if result.Error != 0 {
|
||||
if strings.TrimSpace(result.Msg) == "" {
|
||||
return "", errors.New("兑换失败,接口未返回原因")
|
||||
}
|
||||
return "", fmt.Errorf("兑换失败: %s", result.Msg)
|
||||
return "", &OrderError{Kind: "兑换失败", Code: result.Error, Msg: result.Msg}
|
||||
}
|
||||
var data struct {
|
||||
ExchangeID int `json:"exchangeId"`
|
||||
|
||||
@@ -16,6 +16,18 @@ import (
|
||||
"douyu_go/internal/types"
|
||||
)
|
||||
|
||||
func TestOrderErrorIncludesCode(t *testing.T) {
|
||||
err := (&OrderError{
|
||||
Kind: "锁定兑换商品失败",
|
||||
Code: 1644400007,
|
||||
Msg: "活动火爆,请稍后再试",
|
||||
}).Error()
|
||||
want := "锁定兑换商品失败(error=1644400007): 活动火爆,请稍后再试"
|
||||
if err != want {
|
||||
t.Fatalf("OrderError.Error()=%q, want %q", err, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateCSRFUseCTNReusesCookie(t *testing.T) {
|
||||
var called atomic.Bool
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -181,6 +181,9 @@ func (s *Scheduler) runPeaceLockPay(attempt types.ExchangeAttempt, ctl *runContr
|
||||
if csrf == "" {
|
||||
token, err := cli.GenerateCSRF(attempt.Account.Cookie)
|
||||
if err != nil {
|
||||
if isProxyRiskBusinessError(err) {
|
||||
s.handleProxyBusinessRisk(attempt.Account.Name, cli, err)
|
||||
}
|
||||
out.result.Message = "失败|生成鉴权token失败: " + err.Error()
|
||||
return out
|
||||
}
|
||||
@@ -214,6 +217,9 @@ func (s *Scheduler) runPeaceLockPay(attempt types.ExchangeAttempt, ctl *runContr
|
||||
if err != nil {
|
||||
out.result.Message = "失败|" + err.Error()
|
||||
kind := lockErrorKind(err)
|
||||
if isProxyRiskBusinessError(err) {
|
||||
s.handleProxyBusinessRisk(attempt.Account.Name, cli, err)
|
||||
}
|
||||
out.stockOut = kind == orderErrStockOut
|
||||
out.throttled = kind == orderErrThrottled
|
||||
out.terminal = kind == orderErrTerminal
|
||||
@@ -318,6 +324,12 @@ func (s *Scheduler) payLockedOrder(cli *client.DouyuClient, attempt types.Exchan
|
||||
if err == nil {
|
||||
return info, nil
|
||||
}
|
||||
if isProxyRiskBusinessError(err) {
|
||||
s.handleProxyBusinessRisk(attempt.Account.Name, cli, err)
|
||||
if next := s.clientForAccount(attempt.Account.Name); next != nil {
|
||||
cli = next
|
||||
}
|
||||
}
|
||||
|
||||
kind := payErrorKind(err)
|
||||
if kind == orderErrTerminal {
|
||||
@@ -394,6 +406,9 @@ func (s *Scheduler) runDirectExchange(attempt types.ExchangeAttempt, reuseCache
|
||||
if csrf == "" {
|
||||
token, err := cli.GenerateCSRF(attempt.Account.Cookie)
|
||||
if err != nil {
|
||||
if isProxyRiskBusinessError(err) {
|
||||
s.handleProxyBusinessRisk(attempt.Account.Name, cli, err)
|
||||
}
|
||||
out.result.Message = "失败|生成鉴权token失败: " + err.Error()
|
||||
return out
|
||||
}
|
||||
@@ -422,6 +437,9 @@ func (s *Scheduler) runDirectExchange(attempt types.ExchangeAttempt, reuseCache
|
||||
out.result.Message = "失败|" + errMsg
|
||||
out.terminal = isTerminalAccountMessage(errMsg)
|
||||
kind := lockErrorKind(err)
|
||||
if isProxyRiskBusinessError(err) {
|
||||
s.handleProxyBusinessRisk(attempt.Account.Name, cli, err)
|
||||
}
|
||||
out.stockOut = kind == orderErrStockOut
|
||||
out.throttled = kind == orderErrThrottled
|
||||
if isLimitReachedMessage(errMsg) {
|
||||
@@ -525,6 +543,9 @@ func (s *Scheduler) runXiaodianAttempt(attempt types.ExchangeAttempt) types.Exch
|
||||
attempt.Account.Name, attempt.Product.CommodityName, attempt.Index)
|
||||
exchangeInfo, err := cli.ExchangeProduct(attempt.Account.Cookie, attempt.Product, nil)
|
||||
if err != nil {
|
||||
if isProxyRiskBusinessError(err) {
|
||||
s.handleProxyBusinessRisk(attempt.Account.Name, cli, err)
|
||||
}
|
||||
result.Message = "失败|" + err.Error()
|
||||
return result
|
||||
}
|
||||
@@ -613,6 +634,13 @@ func lockErrorKind(err error) orderErrorKind {
|
||||
return orderErrTransient
|
||||
}
|
||||
|
||||
func isProxyRiskBusinessError(err error) bool {
|
||||
code, msg := orderErrorInfo(err)
|
||||
return code == apiActivityHot ||
|
||||
strings.Contains(msg, "1644400007") ||
|
||||
strings.Contains(msg, "活动火爆")
|
||||
}
|
||||
|
||||
// stockOutHint 判断服务端是否明确表示该商品已售罄。
|
||||
// 只有这一类才允许判定"商品当前不可抢"。
|
||||
func stockOutHint(message string) bool {
|
||||
|
||||
@@ -19,18 +19,21 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
proxyFetchRounds = 3
|
||||
proxyFetchMaxRounds = 20
|
||||
proxyFetchRetryWait = 1200 * time.Millisecond
|
||||
proxyRequestTimeout = 15 * time.Second
|
||||
proxyValidationTimeout = 3 * time.Second
|
||||
proxyValidationWorkers = 3
|
||||
proxyValidateAttempts = 1
|
||||
proxyStandbyProbeEvery = 20 * time.Second
|
||||
proxyStandbyFailLimit = 2
|
||||
proxyImmediateTimeout = 5 * time.Second
|
||||
proxyScheduledTimeout = 20 * time.Second
|
||||
proxyRefillTimeout = 20 * time.Second
|
||||
proxyFetchRounds = 3
|
||||
proxyFetchMaxRounds = 20
|
||||
proxyFetchRetryWait = 1200 * time.Millisecond
|
||||
proxyRequestTimeout = 15 * time.Second
|
||||
proxyValidationTimeout = 3 * time.Second
|
||||
proxyValidationWorkers = 3
|
||||
proxyValidateAttempts = 1
|
||||
proxyStandbyProbeEvery = 20 * time.Second
|
||||
proxyStandbyFailLimit = 2
|
||||
proxyImmediateTimeout = 5 * time.Second
|
||||
proxyScheduledTimeout = 20 * time.Second
|
||||
proxyRefillTimeout = 20 * time.Second
|
||||
proxyRiskProbeWorkers = 3
|
||||
proxyRiskProbeMaxRounds = 20
|
||||
proxyRiskPreflightReserve = 15 * time.Second
|
||||
)
|
||||
|
||||
type proxySettingsSnapshot struct {
|
||||
@@ -156,6 +159,7 @@ func (s *Scheduler) UpdateProxySettings(cfg types.Config) {
|
||||
s.clientMu.Lock()
|
||||
s.accountClientsDirty = true
|
||||
s.proxyFallbackActive = false
|
||||
s.proxyBusinessRiskVerified = false
|
||||
s.clientMu.Unlock()
|
||||
}
|
||||
|
||||
@@ -215,6 +219,7 @@ func (s *Scheduler) closeAccountClients() {
|
||||
s.proxyRefillInFlight = false
|
||||
s.proxyRefillRetryAt = time.Time{}
|
||||
s.proxyFallbackActive = false
|
||||
s.proxyBusinessRiskVerified = false
|
||||
s.clientMu.Unlock()
|
||||
closeDouyuClientList(oldClients)
|
||||
}
|
||||
@@ -256,6 +261,7 @@ func (s *Scheduler) replaceAccountClients(pool preparedProxyPool) {
|
||||
s.proxyRefillInFlight = false
|
||||
s.proxyRefillRetryAt = time.Time{}
|
||||
s.proxyFallbackActive = false
|
||||
s.proxyBusinessRiskVerified = false
|
||||
s.clientMu.Unlock()
|
||||
closeDouyuClientList(oldClients)
|
||||
}
|
||||
@@ -400,6 +406,350 @@ func proxyDegradedReason(groups []types.ProxyGroupStatus, standbyCount, standbyT
|
||||
return ""
|
||||
}
|
||||
|
||||
type proxyBusinessRiskTarget struct {
|
||||
client *client.DouyuClient
|
||||
groupIndex int
|
||||
accountNames []string
|
||||
activeURL string
|
||||
activeIP string
|
||||
}
|
||||
|
||||
type proxyBusinessRiskProbeResult struct {
|
||||
target proxyBusinessRiskTarget
|
||||
accountName string
|
||||
err error
|
||||
definitive bool
|
||||
}
|
||||
|
||||
// preflightProxyBusinessRiskUntil 对每个活动代理组执行只读批量限制请求。
|
||||
// 命中活动火爆时切换备用代理,并对切换后的完整代理组重新检查;任何一组
|
||||
// 在截止时间前没有拿到明确业务响应,都不允许严格代理模式启动。
|
||||
func (s *Scheduler) preflightProxyBusinessRiskUntil(accountProducts map[string]string, deadline time.Time) error {
|
||||
base := s.clientSnapshot()
|
||||
if base == nil || base.Activity == nil {
|
||||
return errors.New("代理风控预检缺少活动配置")
|
||||
}
|
||||
if base.Activity.IsXiaodian || base.Activity.UseCTN {
|
||||
logger.LogEvent("代理", "warn",
|
||||
"当前活动没有批量限制接口,跳过1644400007启动前预检")
|
||||
s.setProxyBusinessRiskVerified(true)
|
||||
return nil
|
||||
}
|
||||
|
||||
for round := 1; round <= proxyRiskProbeMaxRounds; round++ {
|
||||
if !time.Now().Before(deadline) {
|
||||
return fmt.Errorf("代理风控预检超过截止时间")
|
||||
}
|
||||
targets, err := s.proxyBusinessRiskTargets(accountProducts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(targets) == 0 {
|
||||
return errors.New("代理风控预检没有可检查的活动代理组")
|
||||
}
|
||||
|
||||
results := make(chan proxyBusinessRiskProbeResult, len(targets))
|
||||
limit := make(chan struct{}, proxyRiskProbeWorkers)
|
||||
var wg sync.WaitGroup
|
||||
for _, target := range targets {
|
||||
wg.Add(1)
|
||||
go func(target proxyBusinessRiskTarget) {
|
||||
defer wg.Done()
|
||||
limit <- struct{}{}
|
||||
defer func() { <-limit }()
|
||||
accountName, probeErr, definitive := s.probeProxyBusinessRiskTarget(
|
||||
target,
|
||||
accountProducts,
|
||||
deadline,
|
||||
)
|
||||
results <- proxyBusinessRiskProbeResult{
|
||||
target: target,
|
||||
accountName: accountName,
|
||||
err: probeErr,
|
||||
definitive: definitive,
|
||||
}
|
||||
}(target)
|
||||
}
|
||||
wg.Wait()
|
||||
close(results)
|
||||
|
||||
var (
|
||||
hotTargets []proxyBusinessRiskProbeResult
|
||||
unverified []string
|
||||
)
|
||||
for result := range results {
|
||||
switch {
|
||||
case result.definitive && isProxyRiskBusinessError(result.err):
|
||||
hotTargets = append(hotTargets, result)
|
||||
case result.definitive:
|
||||
logger.LogEvent("代理", "success",
|
||||
"代理风控预检通过: 组=%d 账号=%s 出口=%s",
|
||||
result.target.groupIndex,
|
||||
result.accountName,
|
||||
client.MaskProxyURL(result.target.activeURL),
|
||||
)
|
||||
default:
|
||||
unverified = append(unverified, fmt.Sprintf(
|
||||
"G%d(%s): %v",
|
||||
result.target.groupIndex,
|
||||
client.MaskProxyURL(result.target.activeURL),
|
||||
result.err,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
if len(hotTargets) > 0 {
|
||||
for _, result := range hotTargets {
|
||||
logger.LogEvent("代理", "warn",
|
||||
"代理风控预检命中活动火爆/1644400007: 组=%d 账号=%s err=%v",
|
||||
result.target.groupIndex,
|
||||
result.accountName,
|
||||
result.err,
|
||||
)
|
||||
s.handleProxyBusinessRisk(result.accountName, result.target.client, result.err)
|
||||
}
|
||||
if err := s.refillProxyStandbysForPreflight(base, s.proxySettings(), deadline); err != nil {
|
||||
s.clientMu.RLock()
|
||||
remainingFailed := countFailedProxyGroups(s.proxyGroups)
|
||||
s.clientMu.RUnlock()
|
||||
if remainingFailed > 0 {
|
||||
return fmt.Errorf("代理风控预检无法恢复全部代理组: %w", err)
|
||||
}
|
||||
logger.LogEvent("代理", "warn",
|
||||
"代理活动组已切换,但备用代理补提取未完成: %v", err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if len(unverified) > 0 {
|
||||
return fmt.Errorf("代理风控预检未通过: %s", strings.Join(unverified, ";"))
|
||||
}
|
||||
|
||||
s.setProxyBusinessRiskVerified(true)
|
||||
s.setProxyReadiness(true, "")
|
||||
if ok, reason := s.ProxyRunGateFor(accountProducts); !ok {
|
||||
s.setProxyBusinessRiskVerified(false)
|
||||
s.setProxyReadiness(false, reason)
|
||||
return fmt.Errorf("代理风控预检后门禁未通过: %s", reason)
|
||||
}
|
||||
logger.LogEvent("代理", "success",
|
||||
"代理风控预检完成: 活动组=%d,轮次=%d,全部未返回活动火爆/1644400007",
|
||||
len(targets), round)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("代理风控预检切换轮次超过上限%d", proxyRiskProbeMaxRounds)
|
||||
}
|
||||
|
||||
func (s *Scheduler) refillProxyStandbysForPreflight(
|
||||
base *client.DouyuClient,
|
||||
cfg proxySettingsSnapshot,
|
||||
deadline time.Time,
|
||||
) error {
|
||||
if strings.TrimSpace(cfg.proxyAPI) == "" {
|
||||
return errors.New("没有代理提取API,无法补充新的备用代理")
|
||||
}
|
||||
timeout := time.Until(deadline)
|
||||
if timeout <= 0 {
|
||||
return errors.New("代理风控预检已到截止时间,无法继续补提取")
|
||||
}
|
||||
|
||||
s.clientMu.RLock()
|
||||
generation := s.proxyPoolGeneration
|
||||
need := s.proxyStandbyTarget - len(s.proxyStandbys)
|
||||
if need < 0 {
|
||||
need = 0
|
||||
}
|
||||
need += countFailedProxyGroups(s.proxyGroups)
|
||||
excludedURLs := copyStringSet(s.proxyExcludedURLs)
|
||||
s.clientMu.RUnlock()
|
||||
if need <= 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
logger.LogEvent("代理", "info",
|
||||
"代理风控预检正在补提取备用代理: 需要=%d,截止=%s",
|
||||
need, deadline.In(constants.CST).Format("15:04:05.000"))
|
||||
proxies, fetchErr := s.fetchAndValidateProxyPool(base, cfg, need, excludedURLs, timeout)
|
||||
|
||||
var extras []*client.DouyuClient
|
||||
var expiredClients []*client.DouyuClient
|
||||
var switches []proxySwitchEvent
|
||||
s.clientMu.Lock()
|
||||
if generation != s.proxyPoolGeneration {
|
||||
s.clientMu.Unlock()
|
||||
closeValidatedProxies(proxies)
|
||||
return errors.New("代理池已被重建,取消本次补提取")
|
||||
}
|
||||
currentNeed := s.proxyStandbyTarget - len(s.proxyStandbys)
|
||||
if currentNeed < 0 {
|
||||
currentNeed = 0
|
||||
}
|
||||
currentNeed += countFailedProxyGroups(s.proxyGroups)
|
||||
if len(proxies) > currentNeed {
|
||||
for _, proxy := range proxies[currentNeed:] {
|
||||
extras = append(extras, proxy.client)
|
||||
}
|
||||
proxies = proxies[:currentNeed]
|
||||
}
|
||||
for _, proxy := range proxies {
|
||||
proxyClient := proxy.client
|
||||
client.MonitorProxyHealth(proxyClient, func(err error) {
|
||||
s.recordProxyResult(proxyClient, err)
|
||||
})
|
||||
s.proxyStandbys = append(s.proxyStandbys, proxy)
|
||||
s.proxyClientPool = append(s.proxyClientPool, proxy.client)
|
||||
s.proxyExcludedURLs[proxy.url] = struct{}{}
|
||||
}
|
||||
for {
|
||||
pendingClient, pendingGroup := findFailedProxyGroup(s.proxyGroups)
|
||||
if pendingClient == nil {
|
||||
break
|
||||
}
|
||||
event, expired, switched := s.switchGroupToStandbyAvoidIPLocked(
|
||||
pendingClient,
|
||||
pendingGroup,
|
||||
pendingGroup.activeIP,
|
||||
)
|
||||
expiredClients = append(expiredClients, expired...)
|
||||
if !switched {
|
||||
break
|
||||
}
|
||||
switches = append(switches, event)
|
||||
}
|
||||
remainingFailed := countFailedProxyGroups(s.proxyGroups)
|
||||
standbyCount := len(s.proxyStandbys)
|
||||
standbyTarget := s.proxyStandbyTarget
|
||||
s.clientMu.Unlock()
|
||||
|
||||
closeDouyuClientList(extras)
|
||||
closeDouyuClientList(expiredClients)
|
||||
for _, event := range switches {
|
||||
logProxySwitch("代理风控补提取后切换", event)
|
||||
}
|
||||
if len(proxies) > 0 {
|
||||
logger.LogEvent("代理", "success",
|
||||
"代理风控预检补提取完成: 新增=%d,备用=%d/%d,剩余失败组=%d",
|
||||
len(proxies), standbyCount, standbyTarget, remainingFailed)
|
||||
}
|
||||
if remainingFailed > 0 {
|
||||
if fetchErr != nil {
|
||||
return fmt.Errorf("仍有%d个代理组等待备用代理,补提取失败: %w", remainingFailed, fetchErr)
|
||||
}
|
||||
return fmt.Errorf("仍有%d个代理组没有可用的不同出口备用代理", remainingFailed)
|
||||
}
|
||||
if fetchErr != nil && len(proxies) == 0 {
|
||||
logger.LogEvent("代理", "warn", "代理风控预检备用补提取未成功: %v", fetchErr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Scheduler) proxyBusinessRiskTargets(accountProducts map[string]string) ([]proxyBusinessRiskTarget, error) {
|
||||
names := selectedAccountNames(accountProducts)
|
||||
s.clientMu.RLock()
|
||||
targetsByGroup := make(map[int]proxyBusinessRiskTarget)
|
||||
var problems []string
|
||||
for _, name := range names {
|
||||
proxyClient := s.accountClients[name]
|
||||
if proxyClient == nil || proxyClient == s.directClient {
|
||||
problems = append(problems, name+"未分配代理")
|
||||
continue
|
||||
}
|
||||
group := s.proxyGroups[proxyClient]
|
||||
if group == nil || group.failed || strings.TrimSpace(group.activeURL) == "" {
|
||||
problems = append(problems, name+"所在代理组不可用")
|
||||
continue
|
||||
}
|
||||
if _, exists := targetsByGroup[group.groupIndex]; exists {
|
||||
continue
|
||||
}
|
||||
targetsByGroup[group.groupIndex] = proxyBusinessRiskTarget{
|
||||
client: proxyClient,
|
||||
groupIndex: group.groupIndex,
|
||||
accountNames: append([]string(nil), group.accountNames...),
|
||||
activeURL: group.activeURL,
|
||||
activeIP: group.activeIP,
|
||||
}
|
||||
}
|
||||
s.clientMu.RUnlock()
|
||||
if len(problems) > 0 {
|
||||
return nil, fmt.Errorf("代理风控预检分组不完整: %s", strings.Join(problems, ";"))
|
||||
}
|
||||
|
||||
targets := make([]proxyBusinessRiskTarget, 0, len(targetsByGroup))
|
||||
for _, target := range targetsByGroup {
|
||||
targets = append(targets, target)
|
||||
}
|
||||
sort.Slice(targets, func(i, j int) bool {
|
||||
return targets[i].groupIndex < targets[j].groupIndex
|
||||
})
|
||||
return targets, nil
|
||||
}
|
||||
|
||||
func (s *Scheduler) probeProxyBusinessRiskTarget(
|
||||
target proxyBusinessRiskTarget,
|
||||
accountProducts map[string]string,
|
||||
deadline time.Time,
|
||||
) (string, error, bool) {
|
||||
var lastErr error
|
||||
for _, accountName := range target.accountNames {
|
||||
if !time.Now().Before(deadline) {
|
||||
return accountName, errors.New("代理风控预检超过截止时间"), false
|
||||
}
|
||||
productID := strings.TrimSpace(accountProducts[accountName])
|
||||
if productID == "" {
|
||||
continue
|
||||
}
|
||||
cookie, ok := s.AccountCookieByName(accountName)
|
||||
if !ok || strings.TrimSpace(cookie) == "" {
|
||||
lastErr = errors.New("账号Cookie为空")
|
||||
continue
|
||||
}
|
||||
_, csrf := s.cachedAuth(types.ExchangeAttempt{
|
||||
Account: types.Account{Name: accountName},
|
||||
Product: types.Product{CommodityID: productID},
|
||||
})
|
||||
if strings.TrimSpace(csrf) == "" {
|
||||
token, err := target.client.GenerateCSRF(cookie)
|
||||
if err != nil {
|
||||
lastErr = fmt.Errorf("获取CSRF失败: %w", err)
|
||||
logger.LogEvent("代理", "debug",
|
||||
"代理风控预检账号不可用: 组=%d 账号=%s err=%v",
|
||||
target.groupIndex, accountName, err)
|
||||
continue
|
||||
}
|
||||
csrf = token
|
||||
}
|
||||
_, err := target.client.BatchExchangeLimit(cookie, productID, &csrf)
|
||||
if err == nil {
|
||||
return accountName, nil, true
|
||||
}
|
||||
var orderErr *client.OrderError
|
||||
if errors.As(err, &orderErr) {
|
||||
return accountName, err, true
|
||||
}
|
||||
lastErr = err
|
||||
logger.LogEvent("代理", "debug",
|
||||
"代理风控预检账号请求失败: 组=%d 账号=%s err=%v",
|
||||
target.groupIndex, accountName, err)
|
||||
}
|
||||
if lastErr == nil {
|
||||
lastErr = errors.New("组内没有可检查的账号")
|
||||
}
|
||||
return "", fmt.Errorf("组内账号均未取得明确业务响应: %w", lastErr), false
|
||||
}
|
||||
|
||||
func (s *Scheduler) setProxyBusinessRiskVerified(verified bool) {
|
||||
s.clientMu.Lock()
|
||||
s.proxyBusinessRiskVerified = verified
|
||||
s.clientMu.Unlock()
|
||||
}
|
||||
|
||||
func (s *Scheduler) proxyBusinessRiskVerifiedSnapshot() bool {
|
||||
s.clientMu.RLock()
|
||||
verified := s.proxyBusinessRiskVerified
|
||||
s.clientMu.RUnlock()
|
||||
return verified
|
||||
}
|
||||
|
||||
func closeDouyuClients(clients map[string]*client.DouyuClient) {
|
||||
items := make([]*client.DouyuClient, 0, len(clients))
|
||||
for _, cli := range clients {
|
||||
@@ -632,6 +982,14 @@ func (s *Scheduler) prepareProxyGroupsUntil(accountProducts map[string]string, f
|
||||
return nil
|
||||
}
|
||||
if !forceRefresh && s.accountClientsCover(names) {
|
||||
if s.proxyBusinessRiskVerifiedSnapshot() {
|
||||
s.setProxyReadiness(true, "")
|
||||
return nil
|
||||
}
|
||||
s.setProxyPreparing("代理已覆盖,正在补做业务风控预检")
|
||||
if err := s.preflightProxyBusinessRiskUntil(accountProducts, deadline); err != nil {
|
||||
return s.handleProxyPrepareFailure(cfg, names, err)
|
||||
}
|
||||
s.setProxyReadiness(true, "")
|
||||
return nil
|
||||
}
|
||||
@@ -649,7 +1007,11 @@ func (s *Scheduler) prepareProxyGroupsUntil(accountProducts map[string]string, f
|
||||
if timeout <= 0 {
|
||||
return s.handleProxyPrepareFailure(cfg, names, errors.New("代理准备截止时间已到"))
|
||||
}
|
||||
proxies, err := s.buildProxyPool(base, cfg, activeCount, activeCount+standbyTarget, nil, timeout)
|
||||
buildTimeout := timeout
|
||||
if buildTimeout > proxyRiskPreflightReserve*2 {
|
||||
buildTimeout -= proxyRiskPreflightReserve
|
||||
}
|
||||
proxies, err := s.buildProxyPool(base, cfg, activeCount, activeCount+standbyTarget, nil, buildTimeout)
|
||||
if err != nil {
|
||||
return s.handleProxyPrepareFailure(cfg, names, err)
|
||||
}
|
||||
@@ -720,6 +1082,10 @@ func (s *Scheduler) prepareProxyGroupsUntil(accountProducts map[string]string, f
|
||||
s.replaceProxyGroupHistory(groups, activeCount, proxies)
|
||||
logger.LogEvent("代理", "success", "代理池准备完成: 账号=%d,每组=%d,活动代理=%d,备用代理=%d/%d",
|
||||
len(names), cfg.accountsPerProxy, activeCount, len(standbys), standbyTarget)
|
||||
s.setProxyPreparing("代理池已建立,正在逐组执行活动火爆预检")
|
||||
if err := s.preflightProxyBusinessRiskUntil(accountProducts, deadline); err != nil {
|
||||
return s.handleProxyPrepareFailure(cfg, names, err)
|
||||
}
|
||||
s.setProxyReadiness(true, "")
|
||||
return nil
|
||||
}
|
||||
@@ -746,6 +1112,7 @@ func (s *Scheduler) setDirectFallbackAccounts(names []string) {
|
||||
s.accountClientOrder = []*client.DouyuClient{s.directClient}
|
||||
s.accountClientsDirty = false
|
||||
}
|
||||
s.proxyBusinessRiskVerified = false
|
||||
s.clientMu.Unlock()
|
||||
}
|
||||
|
||||
@@ -1103,6 +1470,7 @@ func (s *Scheduler) recordProxyResult(proxyClient *client.DouyuClient, err error
|
||||
return
|
||||
}
|
||||
|
||||
s.proxyBusinessRiskVerified = false
|
||||
group.consecutiveFailures++
|
||||
group.lastError = err.Error()
|
||||
s.updateProxyGroupHistoryLocked(group)
|
||||
@@ -1152,8 +1520,100 @@ func (s *Scheduler) recordProxyResult(proxyClient *client.DouyuClient, err error
|
||||
}
|
||||
}
|
||||
|
||||
// handleProxyBusinessRisk switches away from the egress IP used by an account
|
||||
// when the lock API returns the explicit "activity hot" risk-control code.
|
||||
func (s *Scheduler) handleProxyBusinessRisk(accountName string, proxyClient *client.DouyuClient, cause error) {
|
||||
if !isProxyRiskBusinessError(cause) {
|
||||
return
|
||||
}
|
||||
cfg := s.proxySettings()
|
||||
|
||||
type riskGroup struct {
|
||||
client *client.DouyuClient
|
||||
group *proxyRuntimeGroup
|
||||
}
|
||||
s.clientMu.Lock()
|
||||
if proxyClient == nil || proxyClient == s.directClient || s.proxyFallbackActive {
|
||||
s.clientMu.Unlock()
|
||||
return
|
||||
}
|
||||
initial := s.proxyGroups[proxyClient]
|
||||
if initial == nil || initial.failed {
|
||||
s.clientMu.Unlock()
|
||||
return
|
||||
}
|
||||
riskIP := strings.TrimSpace(initial.activeIP)
|
||||
targets := make([]riskGroup, 0, 1)
|
||||
seenGroups := make(map[int]struct{})
|
||||
for proxyClient, group := range s.proxyGroups {
|
||||
if group == nil {
|
||||
continue
|
||||
}
|
||||
if group != initial && (riskIP == "" || strings.TrimSpace(group.activeIP) != riskIP) {
|
||||
continue
|
||||
}
|
||||
if _, seen := seenGroups[group.groupIndex]; seen {
|
||||
continue
|
||||
}
|
||||
seenGroups[group.groupIndex] = struct{}{}
|
||||
targets = append(targets, riskGroup{client: proxyClient, group: group})
|
||||
}
|
||||
|
||||
var (
|
||||
expiredClients []*client.DouyuClient
|
||||
switchEvents []proxySwitchEvent
|
||||
failedGroups []*proxyRuntimeGroup
|
||||
needRefill bool
|
||||
)
|
||||
for _, target := range targets {
|
||||
target.group.lastError = cause.Error()
|
||||
s.updateProxyGroupHistoryLocked(target.group)
|
||||
event, expired, switched := s.switchGroupToStandbyAvoidIPLocked(target.client, target.group, riskIP)
|
||||
expiredClients = append(expiredClients, expired...)
|
||||
if switched {
|
||||
switchEvents = append(switchEvents, event)
|
||||
needRefill = needRefill || len(s.proxyStandbys) < s.proxyStandbyTarget
|
||||
continue
|
||||
}
|
||||
if !cfg.required && s.directClient != nil {
|
||||
event = s.switchGroupToDirectLocked(target.client, target.group)
|
||||
switchEvents = append(switchEvents, event)
|
||||
needRefill = true
|
||||
continue
|
||||
}
|
||||
target.group.failed = true
|
||||
s.proxyBusinessRiskVerified = false
|
||||
failedGroups = append(failedGroups, target.group)
|
||||
needRefill = true
|
||||
s.updateProxyGroupHistoryLocked(target.group)
|
||||
}
|
||||
needRefill = needRefill && !s.proxyRefillInFlight
|
||||
s.clientMu.Unlock()
|
||||
|
||||
closeDouyuClientList(expiredClients)
|
||||
for _, event := range switchEvents {
|
||||
logProxySwitch("代理风控切换", event)
|
||||
}
|
||||
for _, group := range failedGroups {
|
||||
logger.LogEvent("代理", "warn",
|
||||
"代理风控命中且无不同IP备用: 组=%d 账号=%d 出口=%s,严格模式暂停并等待补提取",
|
||||
group.groupIndex, len(group.accountNames), client.MaskProxyURL(group.activeURL))
|
||||
}
|
||||
logger.LogEvent("代理", "warn",
|
||||
"代理业务风控: 账号=%s 出口IP=%s 原因=%v 切换组数=%d",
|
||||
accountName, riskIP, cause, len(switchEvents))
|
||||
if needRefill {
|
||||
s.startProxyRefill()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Scheduler) switchGroupToStandbyLocked(oldClient *client.DouyuClient, group *proxyRuntimeGroup) (proxySwitchEvent, []*client.DouyuClient, bool) {
|
||||
return s.switchGroupToStandbyAvoidIPLocked(oldClient, group, "")
|
||||
}
|
||||
|
||||
func (s *Scheduler) switchGroupToStandbyAvoidIPLocked(oldClient *client.DouyuClient, group *proxyRuntimeGroup, avoidIP string) (proxySwitchEvent, []*client.DouyuClient, bool) {
|
||||
expiredClients := make([]*client.DouyuClient, 0, 1)
|
||||
var skipped []validatedProxy
|
||||
for len(s.proxyStandbys) > 0 {
|
||||
standby := s.proxyStandbys[0]
|
||||
s.proxyStandbys = s.proxyStandbys[1:]
|
||||
@@ -1161,6 +1621,13 @@ func (s *Scheduler) switchGroupToStandbyLocked(oldClient *client.DouyuClient, gr
|
||||
expiredClients = append(expiredClients, standby.client)
|
||||
continue
|
||||
}
|
||||
if avoidIP != "" && standby.ip == avoidIP {
|
||||
skipped = append(skipped, standby)
|
||||
continue
|
||||
}
|
||||
if len(skipped) > 0 {
|
||||
s.proxyStandbys = append(skipped, s.proxyStandbys...)
|
||||
}
|
||||
|
||||
event := proxySwitchEvent{
|
||||
accountNames: append([]string(nil), group.accountNames...),
|
||||
@@ -1179,6 +1646,7 @@ func (s *Scheduler) switchGroupToStandbyLocked(oldClient *client.DouyuClient, gr
|
||||
group.consecutiveFailures = 0
|
||||
group.lastError = ""
|
||||
group.failed = false
|
||||
s.proxyBusinessRiskVerified = false
|
||||
for _, accountName := range group.accountNames {
|
||||
s.accountClients[accountName] = standby.client
|
||||
}
|
||||
@@ -1188,6 +1656,9 @@ func (s *Scheduler) switchGroupToStandbyLocked(oldClient *client.DouyuClient, gr
|
||||
s.updateProxyGroupHistoryLocked(group)
|
||||
return event, expiredClients, true
|
||||
}
|
||||
if len(skipped) > 0 {
|
||||
s.proxyStandbys = append(skipped, s.proxyStandbys...)
|
||||
}
|
||||
return proxySwitchEvent{}, expiredClients, false
|
||||
}
|
||||
|
||||
@@ -1204,6 +1675,7 @@ func (s *Scheduler) switchGroupToDirectLocked(oldClient *client.DouyuClient, gro
|
||||
group.consecutiveFailures = 0
|
||||
group.lastError = ""
|
||||
group.failed = false
|
||||
s.proxyBusinessRiskVerified = false
|
||||
for _, accountName := range group.accountNames {
|
||||
s.accountClients[accountName] = s.directClient
|
||||
}
|
||||
@@ -1267,6 +1739,10 @@ func (s *Scheduler) startProxyRefill() {
|
||||
|
||||
s.clientMu.Lock()
|
||||
need := s.proxyStandbyTarget - len(s.proxyStandbys)
|
||||
if need < 0 {
|
||||
need = 0
|
||||
}
|
||||
need += countFailedProxyGroups(s.proxyGroups)
|
||||
if need <= 0 || s.proxyRefillInFlight {
|
||||
s.clientMu.Unlock()
|
||||
return
|
||||
@@ -1300,6 +1776,7 @@ func (s *Scheduler) refillProxyStandbys(base *client.DouyuClient, cfg proxySetti
|
||||
if currentNeed < 0 {
|
||||
currentNeed = 0
|
||||
}
|
||||
currentNeed += countFailedProxyGroups(s.proxyGroups)
|
||||
if len(proxies) > currentNeed {
|
||||
for _, proxy := range proxies[currentNeed:] {
|
||||
extras = append(extras, proxy.client)
|
||||
@@ -1321,7 +1798,11 @@ func (s *Scheduler) refillProxyStandbys(base *client.DouyuClient, cfg proxySetti
|
||||
if pendingClient == nil {
|
||||
break
|
||||
}
|
||||
event, expired, switched := s.switchGroupToStandbyLocked(pendingClient, pendingGroup)
|
||||
event, expired, switched := s.switchGroupToStandbyAvoidIPLocked(
|
||||
pendingClient,
|
||||
pendingGroup,
|
||||
pendingGroup.activeIP,
|
||||
)
|
||||
expiredClients = append(expiredClients, expired...)
|
||||
if !switched {
|
||||
break
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -130,10 +131,14 @@ func TestPrepareProxyGroupsRoutesAccountsToDistinctClients(t *testing.T) {
|
||||
|
||||
act := *activity.Get("peace")
|
||||
act.StoreHomeURL = timeAPIURL
|
||||
act.BatchLimitURL = timeAPIURL + "/batchExchangeLimit"
|
||||
base := client.New(&act, timeAPIURL, &trace.Recorder{}, "")
|
||||
scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
paths := writeAccountFile(t, "a", "b", "c", "d", "e")
|
||||
scheduler := New(base, paths, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
scheduler.LoadAccounts()
|
||||
scheduler.UpdateProxySettings(types.Config{
|
||||
ProxyEnabled: true,
|
||||
ProxyRequired: true,
|
||||
ProxyAPI: apiServer.URL,
|
||||
AutoFetchProxyOnSchedule: true,
|
||||
AccountsPerProxy: 2,
|
||||
@@ -302,6 +307,395 @@ func TestStrictProxyModeDoesNotSwitchDirect(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestActivityHotSwitchesProxyButClickTooFastDoesNot(t *testing.T) {
|
||||
act := *activity.Get("peace")
|
||||
base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "")
|
||||
scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
scheduler.UpdateProxySettings(types.Config{
|
||||
ProxyEnabled: true,
|
||||
ProxyRequired: true,
|
||||
AccountsPerProxy: 5,
|
||||
ProxySparePercent: 50,
|
||||
})
|
||||
groupClient := &client.DouyuClient{}
|
||||
standbyClient := &client.DouyuClient{}
|
||||
group := &proxyRuntimeGroup{
|
||||
groupIndex: 1,
|
||||
accountNames: []string{"a"},
|
||||
activeURL: "http://127.0.0.1:9000",
|
||||
activeIP: "1.1.1.1",
|
||||
}
|
||||
scheduler.replaceAccountClients(preparedProxyPool{
|
||||
accountClients: map[string]*client.DouyuClient{"a": groupClient},
|
||||
activeClients: []*client.DouyuClient{groupClient},
|
||||
groups: map[*client.DouyuClient]*proxyRuntimeGroup{groupClient: group},
|
||||
standbys: []validatedProxy{{
|
||||
url: "http://127.0.0.1:9100",
|
||||
client: standbyClient,
|
||||
ip: "2.2.2.2",
|
||||
}},
|
||||
allClients: []*client.DouyuClient{groupClient, standbyClient},
|
||||
standbyTarget: 1,
|
||||
})
|
||||
|
||||
scheduler.handleProxyBusinessRisk("a", groupClient, &client.OrderError{
|
||||
Kind: "锁定兑换商品失败",
|
||||
Code: apiClickTooFast,
|
||||
Msg: "您点击太快,请稍等再试",
|
||||
})
|
||||
if scheduler.clientForAccount("a") != groupClient {
|
||||
t.Fatal("click-too-fast should not switch the proxy")
|
||||
}
|
||||
|
||||
scheduler.handleProxyBusinessRisk("a", groupClient, &client.OrderError{
|
||||
Kind: "锁定兑换商品失败",
|
||||
Code: apiActivityHot,
|
||||
Msg: "活动火爆,请稍后再试",
|
||||
})
|
||||
if scheduler.clientForAccount("a") != standbyClient {
|
||||
t.Fatal("activity-hot should immediately switch to standby")
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyRiskBusinessErrorMatchesCodeOrMessage(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
err error
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "code only",
|
||||
err: &client.OrderError{Code: apiActivityHot},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "message only",
|
||||
err: errors.New("锁定兑换商品失败: 活动火爆,请稍后再试"),
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "numeric code only",
|
||||
err: errors.New("接口失败 error=1644400007"),
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "click too fast",
|
||||
err: &client.OrderError{Code: apiClickTooFast, Msg: "您点击太快,请稍等再试"},
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := isProxyRiskBusinessError(tc.err); got != tc.want {
|
||||
t.Fatalf("isProxyRiskBusinessError(%v)=%v, want %v", tc.err, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectActivityHotDoesNotSwitchProxy(t *testing.T) {
|
||||
act := *activity.Get("peace")
|
||||
base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "")
|
||||
scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
scheduler.UpdateProxySettings(types.Config{
|
||||
ProxyEnabled: true,
|
||||
ProxyRequired: true,
|
||||
AccountsPerProxy: 5,
|
||||
ProxySparePercent: 50,
|
||||
})
|
||||
directClient := &client.DouyuClient{}
|
||||
standbyClient := &client.DouyuClient{}
|
||||
group := &proxyRuntimeGroup{
|
||||
groupIndex: 1,
|
||||
accountNames: []string{"direct"},
|
||||
activeURL: "http://127.0.0.1:9000",
|
||||
activeIP: "1.1.1.1",
|
||||
}
|
||||
scheduler.directClient = directClient
|
||||
scheduler.replaceAccountClients(preparedProxyPool{
|
||||
accountClients: map[string]*client.DouyuClient{"direct": directClient},
|
||||
activeClients: []*client.DouyuClient{directClient},
|
||||
groups: map[*client.DouyuClient]*proxyRuntimeGroup{directClient: group},
|
||||
standbys: []validatedProxy{{
|
||||
url: "http://127.0.0.1:9100",
|
||||
client: standbyClient,
|
||||
ip: "2.2.2.2",
|
||||
}},
|
||||
allClients: []*client.DouyuClient{directClient, standbyClient},
|
||||
standbyTarget: 1,
|
||||
})
|
||||
|
||||
scheduler.handleProxyBusinessRisk("direct", directClient, &client.OrderError{
|
||||
Kind: "锁定兑换商品失败",
|
||||
Code: apiActivityHot,
|
||||
Msg: "活动火爆,请稍后再试",
|
||||
})
|
||||
if scheduler.clientForAccount("direct") != directClient {
|
||||
t.Fatal("direct activity-hot must remain direct")
|
||||
}
|
||||
scheduler.clientMu.RLock()
|
||||
standbyCount := len(scheduler.proxyStandbys)
|
||||
scheduler.clientMu.RUnlock()
|
||||
if standbyCount != 1 {
|
||||
t.Fatalf("direct activity-hot consumed standby proxy: remaining=%d", standbyCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestActivityHotSwitchesAllGroupsSharingEgressIP(t *testing.T) {
|
||||
act := *activity.Get("peace")
|
||||
base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "")
|
||||
scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
scheduler.UpdateProxySettings(types.Config{
|
||||
ProxyEnabled: true,
|
||||
ProxyRequired: true,
|
||||
AccountsPerProxy: 5,
|
||||
ProxySparePercent: 50,
|
||||
})
|
||||
g1, g2 := &client.DouyuClient{}, &client.DouyuClient{}
|
||||
s1, s2 := &client.DouyuClient{}, &client.DouyuClient{}
|
||||
group1 := &proxyRuntimeGroup{groupIndex: 1, accountNames: []string{"a"}, activeURL: "http://127.0.0.1:9001", activeIP: "1.1.1.1"}
|
||||
group2 := &proxyRuntimeGroup{groupIndex: 2, accountNames: []string{"b"}, activeURL: "http://127.0.0.1:9002", activeIP: "1.1.1.1"}
|
||||
scheduler.replaceAccountClients(preparedProxyPool{
|
||||
accountClients: map[string]*client.DouyuClient{"a": g1, "b": g2},
|
||||
activeClients: []*client.DouyuClient{g1, g2},
|
||||
groups: map[*client.DouyuClient]*proxyRuntimeGroup{
|
||||
g1: group1,
|
||||
g2: group2,
|
||||
},
|
||||
standbys: []validatedProxy{
|
||||
{url: "http://127.0.0.1:9101", client: s1, ip: "2.2.2.2"},
|
||||
{url: "http://127.0.0.1:9102", client: s2, ip: "3.3.3.3"},
|
||||
},
|
||||
allClients: []*client.DouyuClient{g1, g2, s1, s2},
|
||||
standbyTarget: 2,
|
||||
})
|
||||
scheduler.handleProxyBusinessRisk("a", g1, &client.OrderError{
|
||||
Kind: "锁定兑换商品失败",
|
||||
Code: apiActivityHot,
|
||||
Msg: "活动火爆,请稍后再试",
|
||||
})
|
||||
if scheduler.clientForAccount("a") == g1 || scheduler.clientForAccount("b") == g2 {
|
||||
t.Fatal("all groups sharing the risky egress IP should switch")
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyPreflightSwitchesOnActivityHot(t *testing.T) {
|
||||
var requests int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if atomic.AddInt32(&requests, 1) == 1 {
|
||||
fmt.Fprint(w, `{"error":1644400007,"msg":"活动火爆,请稍后再试"}`)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(w, `{"error":0,"msg":"","data":{"limit":"1"}}`)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
act := *activity.Get("peace")
|
||||
act.BaseURL = server.URL
|
||||
act.BatchLimitURL = server.URL + "/limit"
|
||||
base := client.New(&act, server.URL+"/time", &trace.Recorder{}, "")
|
||||
paths := writeAccountFile(t, "a")
|
||||
scheduler := New(base, paths, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
scheduler.LoadAccounts()
|
||||
scheduler.UpdateProxySettings(types.Config{
|
||||
ProxyEnabled: true,
|
||||
ProxyRequired: true,
|
||||
AccountsPerProxy: 5,
|
||||
ProxySparePercent: 50,
|
||||
})
|
||||
proxyClient := client.New(&act, server.URL+"/time", &trace.Recorder{}, "")
|
||||
standbyClient := client.New(&act, server.URL+"/time", &trace.Recorder{}, "")
|
||||
group := &proxyRuntimeGroup{groupIndex: 1, accountNames: []string{"a"}, activeURL: "http://127.0.0.1:9000", activeIP: "1.1.1.1"}
|
||||
scheduler.replaceAccountClients(preparedProxyPool{
|
||||
accountClients: map[string]*client.DouyuClient{"a": proxyClient},
|
||||
activeClients: []*client.DouyuClient{proxyClient},
|
||||
groups: map[*client.DouyuClient]*proxyRuntimeGroup{proxyClient: group},
|
||||
standbys: []validatedProxy{{
|
||||
url: "http://127.0.0.1:9100",
|
||||
client: standbyClient,
|
||||
ip: "2.2.2.2",
|
||||
}},
|
||||
allClients: []*client.DouyuClient{proxyClient, standbyClient},
|
||||
standbyTarget: 1,
|
||||
})
|
||||
|
||||
if err := scheduler.preflightProxyBusinessRiskUntil(
|
||||
map[string]string{"a": "goods-1"},
|
||||
time.Now().Add(3*time.Second),
|
||||
); err != nil {
|
||||
t.Fatalf("preflightProxyBusinessRiskUntil() error = %v", err)
|
||||
}
|
||||
if scheduler.clientForAccount("a") != standbyClient {
|
||||
t.Fatal("preflight activity-hot should switch to a different-IP standby")
|
||||
}
|
||||
if !scheduler.proxyBusinessRiskVerifiedSnapshot() {
|
||||
t.Fatal("switched proxy should be rechecked and marked verified")
|
||||
}
|
||||
if got := atomic.LoadInt32(&requests); got != 2 {
|
||||
t.Fatalf("batch limit requests=%d, want 2", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyPreflightRechecksEveryGroupAfterSwitch(t *testing.T) {
|
||||
var requests int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if atomic.AddInt32(&requests, 1) <= 2 {
|
||||
fmt.Fprint(w, `{"error":1644400007,"msg":"活动火爆,请稍后再试"}`)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(w, `{"error":0,"msg":"","data":{"limit":"1"}}`)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
act := *activity.Get("peace")
|
||||
act.BaseURL = server.URL
|
||||
act.BatchLimitURL = server.URL + "/limit"
|
||||
base := client.New(&act, server.URL+"/time", &trace.Recorder{}, "")
|
||||
paths := writeAccountFile(t, "a", "b")
|
||||
scheduler := New(base, paths, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
scheduler.LoadAccounts()
|
||||
scheduler.UpdateProxySettings(types.Config{
|
||||
ProxyEnabled: true,
|
||||
ProxyRequired: true,
|
||||
AccountsPerProxy: 1,
|
||||
ProxySparePercent: 50,
|
||||
})
|
||||
active1 := client.New(&act, server.URL+"/time", &trace.Recorder{}, "")
|
||||
active2 := client.New(&act, server.URL+"/time", &trace.Recorder{}, "")
|
||||
standby1 := client.New(&act, server.URL+"/time", &trace.Recorder{}, "")
|
||||
standby2 := client.New(&act, server.URL+"/time", &trace.Recorder{}, "")
|
||||
group1 := &proxyRuntimeGroup{
|
||||
groupIndex: 1, accountNames: []string{"a"},
|
||||
activeURL: "http://127.0.0.1:9001", activeIP: "1.1.1.1",
|
||||
}
|
||||
group2 := &proxyRuntimeGroup{
|
||||
groupIndex: 2, accountNames: []string{"b"},
|
||||
activeURL: "http://127.0.0.1:9002", activeIP: "2.2.2.2",
|
||||
}
|
||||
scheduler.replaceAccountClients(preparedProxyPool{
|
||||
accountClients: map[string]*client.DouyuClient{"a": active1, "b": active2},
|
||||
activeClients: []*client.DouyuClient{active1, active2},
|
||||
groups: map[*client.DouyuClient]*proxyRuntimeGroup{
|
||||
active1: group1,
|
||||
active2: group2,
|
||||
},
|
||||
standbys: []validatedProxy{
|
||||
{url: "http://127.0.0.1:9101", client: standby1, ip: "3.3.3.3"},
|
||||
{url: "http://127.0.0.1:9102", client: standby2, ip: "4.4.4.4"},
|
||||
},
|
||||
allClients: []*client.DouyuClient{active1, active2, standby1, standby2},
|
||||
standbyTarget: 2,
|
||||
})
|
||||
|
||||
if err := scheduler.preflightProxyBusinessRiskUntil(
|
||||
map[string]string{"a": "goods-1", "b": "goods-1"},
|
||||
time.Now().Add(3*time.Second),
|
||||
); err != nil {
|
||||
t.Fatalf("preflightProxyBusinessRiskUntil() error = %v", err)
|
||||
}
|
||||
if scheduler.clientForAccount("a") == active1 || scheduler.clientForAccount("b") == active2 {
|
||||
t.Fatal("both activity-hot groups should switch before startup")
|
||||
}
|
||||
if !scheduler.proxyBusinessRiskVerifiedSnapshot() {
|
||||
t.Fatal("both replacement groups should be rechecked and verified")
|
||||
}
|
||||
if got := atomic.LoadInt32(&requests); got != 4 {
|
||||
t.Fatalf("batch limit requests=%d, want 4", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyPreflightExtractsReplacementUntilHealthy(t *testing.T) {
|
||||
safeProxyServers := make([]*httptest.Server, 0, 2)
|
||||
for i := 0; i < 2; i++ {
|
||||
safeProxyServers = append(safeProxyServers, httptest.NewServer(http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if strings.Contains(r.URL.Path, "batchExchangeLimit") {
|
||||
fmt.Fprint(w, `{"error":0,"msg":"","data":{"limit":"1"}}`)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(w, `{"error":0,"msg":"","data":{"list":[{"commodityId":"goods-1","commodityName":"测试商品"}]}}`)
|
||||
},
|
||||
)))
|
||||
}
|
||||
defer func() {
|
||||
for _, server := range safeProxyServers {
|
||||
server.Close()
|
||||
}
|
||||
}()
|
||||
hotProxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if strings.Contains(r.URL.Path, "batchExchangeLimit") {
|
||||
fmt.Fprint(w, `{"error":1644400007,"msg":"活动火爆,请稍后再试"}`)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(w, `{"error":0,"msg":"","data":{"list":[{"commodityId":"goods-1","commodityName":"测试商品"}]}}`)
|
||||
}))
|
||||
defer hotProxy.Close()
|
||||
|
||||
var apiCalls int32
|
||||
apiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
index := int(atomic.AddInt32(&apiCalls, 1)-1) % len(safeProxyServers)
|
||||
fmt.Fprint(w, safeProxyServers[index].URL)
|
||||
}))
|
||||
defer apiServer.Close()
|
||||
|
||||
act := *activity.Get("peace")
|
||||
act.StoreHomeURL = "http://douyu.test/product"
|
||||
act.BatchLimitURL = "http://douyu.test/batchExchangeLimit"
|
||||
base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "")
|
||||
paths := writeAccountFile(t, "a")
|
||||
scheduler := New(base, paths, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
scheduler.LoadAccounts()
|
||||
scheduler.UpdateProxySettings(types.Config{
|
||||
ProxyEnabled: true,
|
||||
ProxyRequired: true,
|
||||
ProxyAPI: apiServer.URL,
|
||||
AccountsPerProxy: 1,
|
||||
ProxySparePercent: 50,
|
||||
})
|
||||
active1, err := client.NewWithProxy(&act, "http://douyu.test/time", &trace.Recorder{}, hotProxy.URL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
standby1, err := client.NewWithProxy(&act, "http://douyu.test/time", &trace.Recorder{}, hotProxy.URL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
group := &proxyRuntimeGroup{
|
||||
groupIndex: 1, accountNames: []string{"a"},
|
||||
activeURL: hotProxy.URL, activeIP: "1.1.1.1",
|
||||
}
|
||||
scheduler.replaceAccountClients(preparedProxyPool{
|
||||
accountClients: map[string]*client.DouyuClient{"a": active1},
|
||||
activeClients: []*client.DouyuClient{active1},
|
||||
groups: map[*client.DouyuClient]*proxyRuntimeGroup{active1: group},
|
||||
standbys: []validatedProxy{{
|
||||
url: hotProxy.URL, client: standby1, ip: "2.2.2.2",
|
||||
}},
|
||||
allClients: []*client.DouyuClient{active1, standby1},
|
||||
standbyTarget: 1,
|
||||
})
|
||||
|
||||
if err := scheduler.preflightProxyBusinessRiskUntil(
|
||||
map[string]string{"a": "goods-1"},
|
||||
time.Now().Add(10*time.Second),
|
||||
); err != nil {
|
||||
t.Fatalf("preflightProxyBusinessRiskUntil() error = %v", err)
|
||||
}
|
||||
if scheduler.clientForAccount("a") == active1 || scheduler.clientForAccount("a") == standby1 {
|
||||
t.Fatal("preflight should keep replacing activity-hot proxies until a healthy proxy is active")
|
||||
}
|
||||
if !scheduler.proxyBusinessRiskVerifiedSnapshot() {
|
||||
t.Fatal("replacement proxy should be verified before startup")
|
||||
}
|
||||
if got := atomic.LoadInt32(&apiCalls); got < 2 {
|
||||
t.Fatalf("proxy extraction calls=%d, want at least 2", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyStatusReportsGroupAndAccountMapping(t *testing.T) {
|
||||
act := *activity.Get("peace")
|
||||
base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "")
|
||||
@@ -375,12 +769,35 @@ func TestProxyRunGateAllowsStandbyShortageWhenActiveGroupsCoverAccounts(t *testi
|
||||
expiresAt: time.Now().Add(time.Minute),
|
||||
})
|
||||
scheduler.setProxyReadiness(true, "")
|
||||
scheduler.setProxyBusinessRiskVerified(true)
|
||||
|
||||
if ok, reason := scheduler.ProxyRunGateFor(map[string]string{"a": "goods-1"}); !ok {
|
||||
t.Fatalf("standby shortage should not block covered active groups: %s", reason)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyRunGateRequiresBusinessRiskPreflight(t *testing.T) {
|
||||
act := *activity.Get("peace")
|
||||
base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "")
|
||||
scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
scheduler.UpdateProxySettings(types.Config{
|
||||
ProxyEnabled: true,
|
||||
ProxyRequired: true,
|
||||
AccountsPerProxy: 5,
|
||||
ProxySparePercent: 50,
|
||||
})
|
||||
scheduler.replaceAccountClients(preparedProxyPool{})
|
||||
scheduler.setProxyReadiness(true, "")
|
||||
|
||||
if ok, reason := scheduler.ProxyRunGate(); ok {
|
||||
t.Fatalf("unverified proxy pool should block startup, reason=%q", reason)
|
||||
}
|
||||
scheduler.setProxyBusinessRiskVerified(true)
|
||||
if ok, reason := scheduler.ProxyRunGate(); !ok {
|
||||
t.Fatalf("verified proxy pool should pass startup gate: %s", reason)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStandbyHealthProbeRemovesRepeatedlyFailingProxy(t *testing.T) {
|
||||
act := *activity.Get("peace")
|
||||
act.StoreHomeURL = "http://douyu.test/time"
|
||||
@@ -445,6 +862,10 @@ func newTimeProxyServer(t *testing.T) *httptest.Server {
|
||||
t.Helper()
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if strings.Contains(r.URL.Path, "batchExchangeLimit") {
|
||||
fmt.Fprint(w, `{"error":0,"msg":"","data":{"limit":"1"}}`)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(w, `{"error":0,"msg":"","data":{"list":[{"commodityId":"bag-1","commodityName":"测试商品"}]}}`)
|
||||
}))
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
@@ -82,8 +82,11 @@ type Scheduler struct {
|
||||
proxyRefillInFlight bool
|
||||
proxyRefillRetryAt time.Time
|
||||
proxyFallbackActive bool
|
||||
paths types.AppPaths
|
||||
font *truetype.Font
|
||||
// proxyBusinessRiskVerified 表示当前活动代理池已逐组通过只读业务风控预检。
|
||||
// 代理切换、配置变更或池重建后必须重新验证。
|
||||
proxyBusinessRiskVerified bool
|
||||
paths types.AppPaths
|
||||
font *truetype.Font
|
||||
|
||||
successImages *image.SuccessImageSaver
|
||||
|
||||
@@ -150,6 +153,11 @@ type Scheduler struct {
|
||||
|
||||
calibrationMu sync.Mutex
|
||||
calibrationOffsets []time.Duration // 最近N次校准偏移,用于中位数平滑
|
||||
|
||||
drillMu sync.Mutex
|
||||
drillStatus types.SeckillDrillStatus
|
||||
drillCancel chan struct{}
|
||||
drillTrigger time.Time
|
||||
}
|
||||
|
||||
// SetStateChangeCallback 设置运行状态变化回调。
|
||||
@@ -212,6 +220,7 @@ func New(cli *client.DouyuClient, paths types.AppPaths, font *truetype.Font, con
|
||||
|
||||
// Close 关闭调度器的后台资源。
|
||||
func (s *Scheduler) Close() {
|
||||
s.CancelSeckillDrill()
|
||||
s.closeAccountClients()
|
||||
s.clientMu.Lock()
|
||||
directClient := s.directClient
|
||||
@@ -449,6 +458,7 @@ func (s *Scheduler) TryStartRun() bool {
|
||||
s.proxyMonitor = make(chan struct{})
|
||||
monitor := s.proxyMonitor
|
||||
s.runMu.Unlock()
|
||||
s.stopSeckillDrillForRealRun()
|
||||
s.clientMu.RLock()
|
||||
generation := s.proxyPoolGeneration
|
||||
s.clientMu.RUnlock()
|
||||
@@ -528,6 +538,7 @@ func (s *Scheduler) ProxyRunGateFor(accountProducts map[string]string) (bool, st
|
||||
dirty := s.accountClientsDirty
|
||||
expired := !s.accountClientsExpireAt.IsZero() &&
|
||||
!time.Now().Before(s.accountClientsExpireAt.Add(-30*time.Second))
|
||||
businessRiskVerified := s.proxyBusinessRiskVerified
|
||||
hasFailedGroup := false
|
||||
for _, group := range s.proxyGroups {
|
||||
if group != nil && group.failed {
|
||||
@@ -545,6 +556,9 @@ func (s *Scheduler) ProxyRunGateFor(accountProducts map[string]string) (bool, st
|
||||
if hasFailedGroup {
|
||||
return false, "代理池存在失败组,等待备用代理"
|
||||
}
|
||||
if !businessRiskVerified {
|
||||
return false, "代理业务风控预检未完成或代理已切换"
|
||||
}
|
||||
names := selectedAccountNames(accountProducts)
|
||||
if len(names) > 0 && !s.accountClientsCover(names) {
|
||||
return false, "代理池未覆盖当前账号、已失效或存在失败组"
|
||||
|
||||
@@ -0,0 +1,352 @@
|
||||
package seckill
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"douyu_go/internal/constants"
|
||||
"douyu_go/internal/logger"
|
||||
"douyu_go/internal/types"
|
||||
)
|
||||
|
||||
const (
|
||||
seckillDrillDelay = 2 * time.Minute
|
||||
seckillDrillWarmupLead = 2 * time.Second
|
||||
)
|
||||
|
||||
// StartSeckillDrill schedules a read-only rehearsal at the simulated T0.
|
||||
func (s *Scheduler) StartSeckillDrill(accountProducts map[string]string) (types.SeckillDrillStatus, error) {
|
||||
return s.startSeckillDrill(accountProducts, seckillDrillDelay)
|
||||
}
|
||||
|
||||
func (s *Scheduler) startSeckillDrill(accountProducts map[string]string, delay time.Duration) (types.SeckillDrillStatus, error) {
|
||||
if delay <= 0 {
|
||||
delay = seckillDrillDelay
|
||||
}
|
||||
if len(accountProducts) == 0 {
|
||||
return types.SeckillDrillStatus{}, errors.New("请选择账号和商品")
|
||||
}
|
||||
if s.IsRunning() {
|
||||
return types.SeckillDrillStatus{}, errors.New("真实抢购正在运行,不能启动抢购预演")
|
||||
}
|
||||
|
||||
tasks := s.buildTasks(accountProducts)
|
||||
if len(tasks) == 0 {
|
||||
return types.SeckillDrillStatus{}, errors.New("没有可执行的预演账号")
|
||||
}
|
||||
|
||||
trigger := time.Now().Add(delay)
|
||||
s.drillMu.Lock()
|
||||
if s.drillCancel != nil {
|
||||
s.drillMu.Unlock()
|
||||
return types.SeckillDrillStatus{}, errors.New("抢购预演已在等待或运行")
|
||||
}
|
||||
cancel := make(chan struct{})
|
||||
s.drillCancel = cancel
|
||||
s.drillStatus = types.SeckillDrillStatus{
|
||||
Active: true,
|
||||
State: "preparing",
|
||||
TotalAccounts: len(tasks),
|
||||
Message: "正在准备预演代理池",
|
||||
}
|
||||
s.drillMu.Unlock()
|
||||
|
||||
if err := s.prepareProxyGroups(accountProducts, false); err != nil {
|
||||
s.finishSeckillDrill("failed", "预演代理准备失败: "+err.Error())
|
||||
return types.SeckillDrillStatus{}, fmt.Errorf("预演代理准备失败: %w", err)
|
||||
}
|
||||
if ok, reason := s.ProxyRunGateFor(accountProducts); !ok {
|
||||
s.finishSeckillDrill("failed", "预演代理未就绪: "+reason)
|
||||
return types.SeckillDrillStatus{}, fmt.Errorf("预演代理未就绪: %s", reason)
|
||||
}
|
||||
if s.IsRunning() {
|
||||
s.finishSeckillDrill("failed", "真实抢购已启动,预演自动终止")
|
||||
return types.SeckillDrillStatus{}, errors.New("真实抢购已启动,预演自动终止")
|
||||
}
|
||||
if s.seckillDrillStopped(cancel) {
|
||||
s.finishSeckillDrill("cancelled", "抢购预演已取消")
|
||||
return types.SeckillDrillStatus{}, errors.New("抢购预演已取消")
|
||||
}
|
||||
if !time.Now().Before(trigger) {
|
||||
s.finishSeckillDrill("failed", "代理准备耗时过长,预演触发时间已到")
|
||||
return types.SeckillDrillStatus{}, errors.New("代理准备耗时过长,预演触发时间已到")
|
||||
}
|
||||
|
||||
status := types.SeckillDrillStatus{
|
||||
Active: true,
|
||||
State: "scheduled",
|
||||
TriggerTime: trigger.In(constants.CST).Format("2006-01-02 15:04:05.000"),
|
||||
RemainingSeconds: max(1, int(time.Until(trigger).Round(time.Second)/time.Second)),
|
||||
TotalAccounts: len(tasks),
|
||||
Message: "已准备代理,等待模拟抢购触发",
|
||||
}
|
||||
|
||||
s.drillMu.Lock()
|
||||
s.drillTrigger = trigger
|
||||
s.drillStatus = status
|
||||
s.drillMu.Unlock()
|
||||
|
||||
logger.LogEvent("预演", "success",
|
||||
"抢购预演已设置: 账号=%d,触发=%s;T-120s 代理准备完成",
|
||||
len(tasks), status.TriggerTime)
|
||||
go s.runSeckillDrill(tasks, trigger, cancel)
|
||||
return status, nil
|
||||
}
|
||||
|
||||
func (s *Scheduler) GetSeckillDrillStatus() types.SeckillDrillStatus {
|
||||
s.drillMu.Lock()
|
||||
defer s.drillMu.Unlock()
|
||||
status := s.drillStatus
|
||||
if status.Active && !s.drillTrigger.IsZero() {
|
||||
remaining := time.Until(s.drillTrigger)
|
||||
if remaining < 0 {
|
||||
remaining = 0
|
||||
}
|
||||
status.RemainingSeconds = int((remaining + time.Second - 1) / time.Second)
|
||||
}
|
||||
return status
|
||||
}
|
||||
|
||||
func (s *Scheduler) CancelSeckillDrill() bool {
|
||||
s.drillMu.Lock()
|
||||
cancel := s.drillCancel
|
||||
if cancel == nil {
|
||||
s.drillMu.Unlock()
|
||||
return false
|
||||
}
|
||||
s.drillCancel = nil
|
||||
s.drillTrigger = time.Time{}
|
||||
s.drillStatus.Active = false
|
||||
s.drillStatus.State = "cancelled"
|
||||
s.drillStatus.RemainingSeconds = 0
|
||||
s.drillStatus.Message = "抢购预演已取消"
|
||||
close(cancel)
|
||||
s.drillMu.Unlock()
|
||||
logger.LogEvent("预演", "warn", "抢购预演已取消")
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *Scheduler) stopSeckillDrillForRealRun() {
|
||||
s.drillMu.Lock()
|
||||
cancel := s.drillCancel
|
||||
if cancel == nil {
|
||||
s.drillMu.Unlock()
|
||||
return
|
||||
}
|
||||
s.drillCancel = nil
|
||||
s.drillTrigger = time.Time{}
|
||||
s.drillStatus.Active = false
|
||||
s.drillStatus.State = "failed"
|
||||
s.drillStatus.RemainingSeconds = 0
|
||||
s.drillStatus.Message = "真实抢购已启动,预演自动终止"
|
||||
close(cancel)
|
||||
s.drillMu.Unlock()
|
||||
logger.LogEvent("预演", "warn", "真实抢购已启动,预演自动终止")
|
||||
}
|
||||
|
||||
func (s *Scheduler) runSeckillDrill(tasks []types.ExchangeAttempt, trigger time.Time, cancel <-chan struct{}) {
|
||||
warmupAt := trigger.Add(-seckillDrillWarmupLead)
|
||||
if !s.waitSeckillDrillUntil(warmupAt, cancel) {
|
||||
if s.IsRunning() {
|
||||
s.finishSeckillDrill("failed", "真实抢购已启动,预演自动终止")
|
||||
return
|
||||
}
|
||||
s.finishSeckillDrill("cancelled", "抢购预演已取消")
|
||||
return
|
||||
}
|
||||
if s.IsRunning() {
|
||||
s.finishSeckillDrill("failed", "真实抢购已启动,预演自动终止")
|
||||
return
|
||||
}
|
||||
|
||||
s.setSeckillDrillState("warming", "正在执行账号预热")
|
||||
logger.LogEvent("预演", "info", "抢购预演开始预热: 账号=%d", len(tasks))
|
||||
s.warmupAttempts(tasks)
|
||||
|
||||
if !s.waitSeckillDrillUntil(trigger, cancel) {
|
||||
if s.IsRunning() {
|
||||
s.finishSeckillDrill("failed", "真实抢购已启动,预演自动终止")
|
||||
return
|
||||
}
|
||||
s.finishSeckillDrill("cancelled", "抢购预演已取消")
|
||||
return
|
||||
}
|
||||
if s.IsRunning() {
|
||||
s.finishSeckillDrill("failed", "真实抢购已启动,预演自动终止")
|
||||
return
|
||||
}
|
||||
|
||||
s.setSeckillDrillState("running", "正在按正式并发执行只读抢购请求")
|
||||
logger.LogEvent("预演", "info", "抢购预演触发: 开始执行 %d 个账号", len(tasks))
|
||||
s.runSeckillDrillAccounts(tasks, cancel)
|
||||
}
|
||||
|
||||
func (s *Scheduler) runSeckillDrillAccounts(tasks []types.ExchangeAttempt, cancel <-chan struct{}) {
|
||||
settings := s.settingsSnapshot()
|
||||
limit := make(chan struct{}, max(1, settings.concurrency))
|
||||
var (
|
||||
wg sync.WaitGroup
|
||||
mu sync.Mutex
|
||||
)
|
||||
for _, task := range tasks {
|
||||
wg.Add(1)
|
||||
go func(task types.ExchangeAttempt) {
|
||||
defer wg.Done()
|
||||
if s.seckillDrillStopped(cancel) {
|
||||
return
|
||||
}
|
||||
limit <- struct{}{}
|
||||
defer func() { <-limit }()
|
||||
if s.seckillDrillStopped(cancel) {
|
||||
return
|
||||
}
|
||||
|
||||
err := s.runSeckillDrillAccount(task)
|
||||
mu.Lock()
|
||||
status := s.updateSeckillDrillProgress(err)
|
||||
mu.Unlock()
|
||||
if err != nil {
|
||||
logger.LogAccountEvent("预演", "warn", task.Account.Name,
|
||||
"抢购预演失败: 账号=%s 商品=%s err=%v",
|
||||
task.Account.Name, task.Product.CommodityName, err)
|
||||
return
|
||||
}
|
||||
logger.LogAccountEvent("预演", "success", task.Account.Name,
|
||||
"抢购预演通过: 账号=%s 商品=%s 进度=%d/%d",
|
||||
task.Account.Name, task.Product.CommodityName,
|
||||
status.CompletedAccounts, status.TotalAccounts)
|
||||
}(task)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
if s.IsRunning() {
|
||||
s.finishSeckillDrill("failed", "真实抢购已启动,预演自动终止")
|
||||
return
|
||||
}
|
||||
if s.seckillDrillStopped(cancel) {
|
||||
s.finishSeckillDrill("cancelled", "抢购预演已取消")
|
||||
return
|
||||
}
|
||||
status := s.GetSeckillDrillStatus()
|
||||
message := fmt.Sprintf("预演完成: 成功 %d/%d,失败 %d",
|
||||
status.SuccessAccounts, status.TotalAccounts, status.FailedAccounts)
|
||||
s.finishSeckillDrill("completed", message)
|
||||
}
|
||||
|
||||
func (s *Scheduler) runSeckillDrillAccount(task types.ExchangeAttempt) error {
|
||||
cli := s.clientForAccount(task.Account.Name)
|
||||
if cli == nil {
|
||||
return errors.New("没有可用代理客户端")
|
||||
}
|
||||
_, csrf := s.cachedAuth(task)
|
||||
switch {
|
||||
case cli.Activity.IsXiaodian:
|
||||
_, err := cli.XiaodianRole(task.Account.Cookie)
|
||||
if err != nil && isProxyRiskBusinessError(err) {
|
||||
s.handleProxyBusinessRisk(task.Account.Name, cli, err)
|
||||
}
|
||||
return err
|
||||
case cli.Activity.UseCTN:
|
||||
if _, err := cli.FetchCTN(task.Account.Cookie); err != nil {
|
||||
if isProxyRiskBusinessError(err) {
|
||||
s.handleProxyBusinessRisk(task.Account.Name, cli, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
_, err := cli.RoleName(task.Account.Cookie)
|
||||
if err != nil && isProxyRiskBusinessError(err) {
|
||||
s.handleProxyBusinessRisk(task.Account.Name, cli, err)
|
||||
}
|
||||
return err
|
||||
default:
|
||||
if strings.TrimSpace(csrf) == "" {
|
||||
token, err := cli.GenerateCSRF(task.Account.Cookie)
|
||||
if err != nil {
|
||||
if isProxyRiskBusinessError(err) {
|
||||
s.handleProxyBusinessRisk(task.Account.Name, cli, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
csrf = token
|
||||
}
|
||||
_, err := cli.BatchExchangeLimit(task.Account.Cookie, task.Product.CommodityID, &csrf)
|
||||
if err != nil && isProxyRiskBusinessError(err) {
|
||||
s.handleProxyBusinessRisk(task.Account.Name, cli, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Scheduler) updateSeckillDrillProgress(err error) types.SeckillDrillStatus {
|
||||
s.drillMu.Lock()
|
||||
defer s.drillMu.Unlock()
|
||||
s.drillStatus.CompletedAccounts++
|
||||
if err != nil {
|
||||
s.drillStatus.FailedAccounts++
|
||||
} else {
|
||||
s.drillStatus.SuccessAccounts++
|
||||
}
|
||||
return s.drillStatus
|
||||
}
|
||||
|
||||
func (s *Scheduler) setSeckillDrillState(state, message string) {
|
||||
s.drillMu.Lock()
|
||||
if s.drillCancel == nil {
|
||||
s.drillMu.Unlock()
|
||||
return
|
||||
}
|
||||
s.drillStatus.State = state
|
||||
s.drillStatus.Message = message
|
||||
s.drillMu.Unlock()
|
||||
}
|
||||
|
||||
func (s *Scheduler) finishSeckillDrill(state, message string) {
|
||||
s.drillMu.Lock()
|
||||
if s.drillStatus.Active {
|
||||
s.drillStatus.Active = false
|
||||
s.drillStatus.State = state
|
||||
s.drillStatus.Message = message
|
||||
s.drillStatus.RemainingSeconds = 0
|
||||
}
|
||||
s.drillCancel = nil
|
||||
s.drillTrigger = time.Time{}
|
||||
status := s.drillStatus
|
||||
s.drillMu.Unlock()
|
||||
level := "info"
|
||||
switch {
|
||||
case state == "failed" || status.FailedAccounts > 0:
|
||||
level = "warn"
|
||||
case state == "cancelled":
|
||||
level = "warn"
|
||||
case state == "completed":
|
||||
level = "success"
|
||||
}
|
||||
logger.LogEvent("预演", level, "%s", message)
|
||||
}
|
||||
|
||||
func (s *Scheduler) waitSeckillDrillUntil(target time.Time, cancel <-chan struct{}) bool {
|
||||
wait := time.Until(target)
|
||||
if wait <= 0 {
|
||||
return !s.seckillDrillStopped(cancel)
|
||||
}
|
||||
timer := time.NewTimer(wait)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-cancel:
|
||||
return false
|
||||
case <-timer.C:
|
||||
return !s.seckillDrillStopped(cancel)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Scheduler) seckillDrillStopped(cancel <-chan struct{}) bool {
|
||||
select {
|
||||
case <-cancel:
|
||||
return true
|
||||
default:
|
||||
return s.IsRunning()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
package seckill
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"douyu_go/internal/activity"
|
||||
"douyu_go/internal/client"
|
||||
"douyu_go/internal/trace"
|
||||
"douyu_go/internal/types"
|
||||
)
|
||||
|
||||
func TestSeckillDrillUsesReadOnlyAccountChecks(t *testing.T) {
|
||||
var limitCalls int
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch r.URL.Path {
|
||||
case "/bind":
|
||||
fmt.Fprint(w, `{"error":0,"data":{"gameRole":{"roleName":"测试角色"}}}`)
|
||||
case "/limit":
|
||||
limitCalls++
|
||||
fmt.Fprint(w, `{"error":0,"data":{"limit":"1"}}`)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
act := *activity.Get("peace")
|
||||
act.BaseURL = server.URL
|
||||
act.BindInfoURL = server.URL + "/bind"
|
||||
act.BatchLimitURL = server.URL + "/limit"
|
||||
base := client.New(&act, server.URL+"/time", &trace.Recorder{}, "")
|
||||
paths := writeAccountFile(t, "a")
|
||||
scheduler := New(base, paths, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
scheduler.LoadAccounts()
|
||||
scheduler.UpdateProducts([]types.Product{{
|
||||
CommodityID: "goods-1",
|
||||
CommodityName: "测试商品",
|
||||
}})
|
||||
|
||||
status, err := scheduler.startSeckillDrill(map[string]string{"a": "goods-1"}, 120*time.Millisecond)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !status.Active || status.TotalAccounts != 1 {
|
||||
t.Fatalf("unexpected initial drill status: %+v", status)
|
||||
}
|
||||
|
||||
deadline := time.Now().Add(3 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
status = scheduler.GetSeckillDrillStatus()
|
||||
if !status.Active {
|
||||
break
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
if status.Active || status.State != "completed" {
|
||||
t.Fatalf("unexpected final drill status: %+v", status)
|
||||
}
|
||||
if status.SuccessAccounts != 1 || status.FailedAccounts != 0 {
|
||||
t.Fatalf("unexpected drill counts: %+v", status)
|
||||
}
|
||||
if limitCalls != 1 {
|
||||
t.Fatalf("limitCalls=%d, want 1", limitCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSeckillDrillSwitchesActivityHotProxyBeforeStart(t *testing.T) {
|
||||
hotProxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if r.URL.Path == "/limit" {
|
||||
fmt.Fprint(w, `{"error":1644400007,"msg":"活动火爆,请稍后再试"}`)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(w, `{"error":0,"data":{"gameRole":{"roleName":"热门代理角色"}}}`)
|
||||
}))
|
||||
defer hotProxy.Close()
|
||||
safeProxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch r.URL.Path {
|
||||
case "/limit":
|
||||
fmt.Fprint(w, `{"error":0,"data":{"limit":"1"}}`)
|
||||
case "/bind":
|
||||
fmt.Fprint(w, `{"error":0,"data":{"gameRole":{"roleName":"备用代理角色"}}}`)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer safeProxy.Close()
|
||||
|
||||
act := *activity.Get("peace")
|
||||
act.BindInfoURL = "http://douyu.test/bind"
|
||||
act.BatchLimitURL = "http://douyu.test/limit"
|
||||
base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "")
|
||||
paths := writeAccountFile(t, "a")
|
||||
scheduler := New(base, paths, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
scheduler.LoadAccounts()
|
||||
scheduler.UpdateProducts([]types.Product{{
|
||||
CommodityID: "goods-1",
|
||||
CommodityName: "测试商品",
|
||||
}})
|
||||
scheduler.UpdateProxySettings(types.Config{
|
||||
ProxyEnabled: true,
|
||||
ProxyRequired: true,
|
||||
AccountsPerProxy: 1,
|
||||
ProxySparePercent: 50,
|
||||
})
|
||||
hotClient, err := client.NewWithProxy(&act, "http://douyu.test/time", &trace.Recorder{}, hotProxy.URL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
safeClient, err := client.NewWithProxy(&act, "http://douyu.test/time", &trace.Recorder{}, safeProxy.URL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
group := &proxyRuntimeGroup{
|
||||
groupIndex: 1, accountNames: []string{"a"},
|
||||
activeURL: hotProxy.URL, activeIP: "1.1.1.1",
|
||||
}
|
||||
scheduler.replaceAccountClients(preparedProxyPool{
|
||||
accountClients: map[string]*client.DouyuClient{"a": hotClient},
|
||||
activeClients: []*client.DouyuClient{hotClient},
|
||||
groups: map[*client.DouyuClient]*proxyRuntimeGroup{hotClient: group},
|
||||
standbys: []validatedProxy{{
|
||||
url: safeProxy.URL, client: safeClient, ip: "2.2.2.2",
|
||||
}},
|
||||
allClients: []*client.DouyuClient{hotClient, safeClient},
|
||||
standbyTarget: 1,
|
||||
})
|
||||
|
||||
status, err := scheduler.startSeckillDrill(map[string]string{"a": "goods-1"}, 120*time.Millisecond)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !status.Active {
|
||||
t.Fatalf("unexpected initial drill status: %+v", status)
|
||||
}
|
||||
if scheduler.clientForAccount("a") != safeClient {
|
||||
t.Fatal("drill preparation should switch activity-hot proxy to standby")
|
||||
}
|
||||
|
||||
deadline := time.Now().Add(3 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
status = scheduler.GetSeckillDrillStatus()
|
||||
if !status.Active {
|
||||
break
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
if status.Active || status.State != "completed" || status.FailedAccounts != 0 {
|
||||
t.Fatalf("unexpected final drill status: %+v", status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSeckillDrillStopsWhenRealRunStarts(t *testing.T) {
|
||||
act := *activity.Get("peace")
|
||||
base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "")
|
||||
paths := writeAccountFile(t, "a")
|
||||
scheduler := New(base, paths, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
scheduler.LoadAccounts()
|
||||
scheduler.UpdateProducts([]types.Product{{
|
||||
CommodityID: "goods-1",
|
||||
CommodityName: "测试商品",
|
||||
}})
|
||||
|
||||
if _, err := scheduler.startSeckillDrill(map[string]string{"a": "goods-1"}, 300*time.Millisecond); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !scheduler.TryStartRun() {
|
||||
t.Fatal("real run should start")
|
||||
}
|
||||
defer scheduler.finishRun()
|
||||
|
||||
deadline := time.Now().Add(2 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
status := scheduler.GetSeckillDrillStatus()
|
||||
if !status.Active {
|
||||
if status.State != "failed" || !strings.Contains(status.Message, "真实抢购已启动") {
|
||||
t.Fatalf("unexpected drill status: %+v", status)
|
||||
}
|
||||
return
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
t.Fatal("drill did not stop after real run started")
|
||||
}
|
||||
@@ -27,6 +27,7 @@ type ProxyDrillItem struct {
|
||||
type ProxyDrillResult struct {
|
||||
Success bool `json:"success"`
|
||||
Error string `json:"error,omitempty"`
|
||||
Warning string `json:"warning,omitempty"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
SimulatedAccounts int `json:"simulatedAccounts"`
|
||||
AccountsPerProxy int `json:"accountsPerProxy"`
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package types
|
||||
|
||||
// SeckillDrillStatus is the local read-only seckill rehearsal state.
|
||||
type SeckillDrillStatus struct {
|
||||
Active bool `json:"active"`
|
||||
State string `json:"state"`
|
||||
TriggerTime string `json:"triggerTime,omitempty"`
|
||||
RemainingSeconds int `json:"remainingSeconds"`
|
||||
TotalAccounts int `json:"totalAccounts"`
|
||||
CompletedAccounts int `json:"completedAccounts"`
|
||||
SuccessAccounts int `json:"successAccounts"`
|
||||
FailedAccounts int `json:"failedAccounts"`
|
||||
Message string `json:"message,omitempty"`
|
||||
}
|
||||
Reference in New Issue
Block a user