fix(seckill): 避免预热阻塞抢购预演T0
This commit is contained in:
+10
-1
@@ -9,7 +9,7 @@
|
|||||||
1. 使用当前勾选账号和商品建立任务。
|
1. 使用当前勾选账号和商品建立任务。
|
||||||
2. 点击时作为模拟 `T-120s`,立即准备并校验代理池。
|
2. 点击时作为模拟 `T-120s`,立即准备并校验代理池。
|
||||||
3. 以点击后 2 分钟作为模拟 `T0`,进入倒计时;代理准备耗时计入这 2 分钟。
|
3. 以点击后 2 分钟作为模拟 `T0`,进入倒计时;代理准备耗时计入这 2 分钟。
|
||||||
4. `T-2s` 使用当前账号 Cookie 执行预热。
|
4. `T-2s` 使用当前账号 Cookie 启动预热;CSRF 成功即写入缓存,角色信息查询只做补充,不阻塞 `T0`。
|
||||||
5. `T0` 按配置并发数执行账号级只读请求。
|
5. `T0` 按配置并发数执行账号级只读请求。
|
||||||
6. 显示成功、失败和完成进度。
|
6. 显示成功、失败和完成进度。
|
||||||
|
|
||||||
@@ -23,6 +23,15 @@
|
|||||||
- 切换后重新检查全部活动组,无法全部通过则预演直接失败,不进入倒计时。
|
- 切换后重新检查全部活动组,无法全部通过则预演直接失败,不进入倒计时。
|
||||||
- `T0` 的只读请求如果再次命中活动火爆,预演会记录失败并触发同一套代理切换逻辑。
|
- `T0` 的只读请求如果再次命中活动火爆,预演会记录失败并触发同一套代理切换逻辑。
|
||||||
|
|
||||||
|
## 预热时序
|
||||||
|
|
||||||
|
预演预热与正式定时抢购保持一致:
|
||||||
|
|
||||||
|
- 预热和 `T0` 是两个独立时间点,预演不会等待所有角色查询结束才触发。
|
||||||
|
- CSRF 获取成功后立即进入缓存,角色接口超时只影响角色名展示,不会丢掉已取得的 CSRF。
|
||||||
|
- 角色信息稍后查询成功时会补充更新缓存。
|
||||||
|
- 取消预演只记录一次日志,等待协程不会重复写入取消状态。
|
||||||
|
|
||||||
## 请求边界
|
## 请求边界
|
||||||
|
|
||||||
允许调用:
|
允许调用:
|
||||||
|
|||||||
@@ -164,7 +164,8 @@ func (s *Scheduler) runSeckillDrill(tasks []types.ExchangeAttempt, trigger time.
|
|||||||
|
|
||||||
s.setSeckillDrillState("warming", "正在执行账号预热")
|
s.setSeckillDrillState("warming", "正在执行账号预热")
|
||||||
logger.LogEvent("预演", "info", "抢购预演开始预热: 账号=%d", len(tasks))
|
logger.LogEvent("预演", "info", "抢购预演开始预热: 账号=%d", len(tasks))
|
||||||
s.warmupAttempts(tasks)
|
// 真实定时抢购的预热与 T0 是两个独立定时器;预演也不能等待角色查询结束。
|
||||||
|
go s.warmupAttempts(tasks)
|
||||||
|
|
||||||
if !s.waitSeckillDrillUntil(trigger, cancel) {
|
if !s.waitSeckillDrillUntil(trigger, cancel) {
|
||||||
if s.IsRunning() {
|
if s.IsRunning() {
|
||||||
@@ -305,12 +306,14 @@ func (s *Scheduler) setSeckillDrillState(state, message string) {
|
|||||||
|
|
||||||
func (s *Scheduler) finishSeckillDrill(state, message string) {
|
func (s *Scheduler) finishSeckillDrill(state, message string) {
|
||||||
s.drillMu.Lock()
|
s.drillMu.Lock()
|
||||||
if s.drillStatus.Active {
|
if !s.drillStatus.Active {
|
||||||
s.drillStatus.Active = false
|
s.drillMu.Unlock()
|
||||||
s.drillStatus.State = state
|
return
|
||||||
s.drillStatus.Message = message
|
|
||||||
s.drillStatus.RemainingSeconds = 0
|
|
||||||
}
|
}
|
||||||
|
s.drillStatus.Active = false
|
||||||
|
s.drillStatus.State = state
|
||||||
|
s.drillStatus.Message = message
|
||||||
|
s.drillStatus.RemainingSeconds = 0
|
||||||
s.drillCancel = nil
|
s.drillCancel = nil
|
||||||
s.drillTrigger = time.Time{}
|
s.drillTrigger = time.Time{}
|
||||||
status := s.drillStatus
|
status := s.drillStatus
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -157,6 +159,89 @@ func TestSeckillDrillSwitchesActivityHotProxyBeforeStart(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSeckillDrillDoesNotWaitForSlowWarmup(t *testing.T) {
|
||||||
|
roleStarted := make(chan struct{}, 1)
|
||||||
|
roleRelease := make(chan struct{})
|
||||||
|
var releaseOnce sync.Once
|
||||||
|
releaseRole := func() {
|
||||||
|
releaseOnce.Do(func() { close(roleRelease) })
|
||||||
|
}
|
||||||
|
var limitCalls atomic.Int32
|
||||||
|
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":
|
||||||
|
select {
|
||||||
|
case roleStarted <- struct{}{}:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
<-roleRelease
|
||||||
|
fmt.Fprint(w, `{"error":0,"data":{"gameRole":{"roleName":"慢角色"}}}`)
|
||||||
|
case "/limit":
|
||||||
|
limitCalls.Add(1)
|
||||||
|
fmt.Fprint(w, `{"error":0,"data":{"limit":"1"}}`)
|
||||||
|
default:
|
||||||
|
http.NotFound(w, r)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
defer releaseRole()
|
||||||
|
|
||||||
|
act := *activity.Get("peace")
|
||||||
|
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: "测试商品",
|
||||||
|
}})
|
||||||
|
|
||||||
|
if _, err := scheduler.startSeckillDrill(
|
||||||
|
map[string]string{"a": "goods-1"},
|
||||||
|
120*time.Millisecond,
|
||||||
|
); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-roleStarted:
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Fatal("slow warmup did not start")
|
||||||
|
}
|
||||||
|
|
||||||
|
deadline := time.Now().Add(800 * time.Millisecond)
|
||||||
|
for time.Now().Before(deadline) {
|
||||||
|
status := scheduler.GetSeckillDrillStatus()
|
||||||
|
if !status.Active {
|
||||||
|
if status.State != "completed" || status.SuccessAccounts != 1 {
|
||||||
|
t.Fatalf("unexpected drill status while warmup blocked: %+v", status)
|
||||||
|
}
|
||||||
|
if got := limitCalls.Load(); got != 1 {
|
||||||
|
t.Fatalf("T0 batch limit calls=%d, want 1", got)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
}
|
||||||
|
t.Fatal("drill T0 waited for slow warmup role lookup")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFinishSeckillDrillKeepsAlreadyCancelledState(t *testing.T) {
|
||||||
|
scheduler := &Scheduler{
|
||||||
|
drillStatus: types.SeckillDrillStatus{
|
||||||
|
State: "cancelled",
|
||||||
|
Message: "抢购预演已取消",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
scheduler.finishSeckillDrill("completed", "不应覆盖")
|
||||||
|
status := scheduler.GetSeckillDrillStatus()
|
||||||
|
if status.State != "cancelled" || status.Message != "抢购预演已取消" {
|
||||||
|
t.Fatalf("unexpected status after duplicate finish: %+v", status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSeckillDrillStopsWhenRealRunStarts(t *testing.T) {
|
func TestSeckillDrillStopsWhenRealRunStarts(t *testing.T) {
|
||||||
act := *activity.Get("peace")
|
act := *activity.Get("peace")
|
||||||
base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "")
|
base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "")
|
||||||
|
|||||||
@@ -71,15 +71,27 @@ func (s *Scheduler) warmupAttempts(attempts []types.ExchangeAttempt) {
|
|||||||
logger.LogAccountEvent("预热", "warn", attempt.Account.Name, "鉴权token预热失败: 账号=%s 商品=%s err=%v", attempt.Account.Name, attempt.Product.CommodityName, err)
|
logger.LogAccountEvent("预热", "warn", attempt.Account.Name, "鉴权token预热失败: 账号=%s 商品=%s err=%v", attempt.Account.Name, attempt.Product.CommodityName, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
createdAt := time.Now()
|
||||||
|
// CSRF 已足够支撑 T0 首发请求,先入缓存;角色信息只做展示补充。
|
||||||
|
s.setWarmup(warmupKey(attempt), types.WarmupData{
|
||||||
|
CSRF: csrf,
|
||||||
|
Qualified: true,
|
||||||
|
CreatedAt: createdAt,
|
||||||
|
})
|
||||||
roleName, err := cli.RoleName(attempt.Account.Cookie)
|
roleName, err := cli.RoleName(attempt.Account.Cookie)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.LogAccountEvent("预热", "warn", attempt.Account.Name, "角色预热失败: 账号=%s err=%v", attempt.Account.Name, err)
|
logger.LogAccountEvent("预热", "warn", attempt.Account.Name, "角色预热失败: 账号=%s err=%v", attempt.Account.Name, err)
|
||||||
|
logger.LogAccountEvent("预热", "success", attempt.Account.Name,
|
||||||
|
"鉴权预热完成(角色信息未获取): 账号=%s 商品=%s 耗时=%s",
|
||||||
|
attempt.Account.Name, attempt.Product.CommodityName,
|
||||||
|
utils.RoundDuration(time.Since(start)))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
s.setWarmup(warmupKey(attempt), types.WarmupData{
|
s.setWarmup(warmupKey(attempt), types.WarmupData{
|
||||||
CSRF: csrf,
|
CSRF: csrf,
|
||||||
RoleName: roleName,
|
RoleName: roleName,
|
||||||
Qualified: true,
|
Qualified: true,
|
||||||
CreatedAt: time.Now(),
|
CreatedAt: createdAt,
|
||||||
})
|
})
|
||||||
logger.LogAccountEvent("预热", "success", attempt.Account.Name, "预热完成: 账号=%s 商品=%s 耗时=%s", attempt.Account.Name, attempt.Product.CommodityName, utils.RoundDuration(time.Since(start)))
|
logger.LogAccountEvent("预热", "success", attempt.Account.Name, "预热完成: 账号=%s 商品=%s 耗时=%s", attempt.Account.Name, attempt.Product.CommodityName, utils.RoundDuration(time.Since(start)))
|
||||||
}(attempt)
|
}(attempt)
|
||||||
|
|||||||
@@ -0,0 +1,75 @@
|
|||||||
|
package seckill
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"douyu_go/internal/activity"
|
||||||
|
"douyu_go/internal/client"
|
||||||
|
"douyu_go/internal/trace"
|
||||||
|
"douyu_go/internal/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestWarmupCachesCSRFBeforeRoleLookupCompletes(t *testing.T) {
|
||||||
|
roleStarted := make(chan struct{}, 1)
|
||||||
|
roleRelease := make(chan struct{})
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.URL.Path != "/bind" {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case roleStarted <- struct{}{}:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
<-roleRelease
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
fmt.Fprint(w, `{"error":0,"data":{"gameRole":{"roleName":"测试角色"}}}`)
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
act := *activity.Get("peace")
|
||||||
|
act.BindInfoURL = server.URL + "/bind"
|
||||||
|
base := client.New(&act, server.URL+"/time", &trace.Recorder{}, "")
|
||||||
|
scheduler := New(base, types.AppPaths{}, nil, 1, 1, nil, types.SeckillModeAggressive)
|
||||||
|
attempt := types.ExchangeAttempt{
|
||||||
|
Account: types.Account{Name: "a", Cookie: "cvl_csrf_token=cached-token"},
|
||||||
|
Product: types.Product{CommodityID: "goods-1", CommodityName: "测试商品"},
|
||||||
|
}
|
||||||
|
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
scheduler.warmupAttempts([]types.ExchangeAttempt{attempt})
|
||||||
|
close(done)
|
||||||
|
}()
|
||||||
|
select {
|
||||||
|
case <-roleStarted:
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Fatal("role lookup did not start")
|
||||||
|
}
|
||||||
|
|
||||||
|
warmup, ok := scheduler.warmupFor(attempt)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("CSRF should be cached before role lookup completes")
|
||||||
|
}
|
||||||
|
if warmup.CSRF != "cached-token" {
|
||||||
|
t.Fatalf("cached CSRF=%q, want cached-token", warmup.CSRF)
|
||||||
|
}
|
||||||
|
if warmup.RoleName != "" {
|
||||||
|
t.Fatalf("role name should not be cached before role lookup completes: %q", warmup.RoleName)
|
||||||
|
}
|
||||||
|
|
||||||
|
close(roleRelease)
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Fatal("warmup did not finish after role lookup")
|
||||||
|
}
|
||||||
|
warmup, ok = scheduler.warmupFor(attempt)
|
||||||
|
if !ok || warmup.RoleName != "测试角色" {
|
||||||
|
t.Fatalf("warmup role was not updated: %+v", warmup)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user