refactor(sniper): 对齐 dygou_go 调度架构,增加双时间日志并解耦预热
- 抢购调度重构:参考 dygou_go 将 sniper 拆分为独立子模块(runner/run_control/retry/exchange/classify/config/summary/category/helpers);移除全局自适应降速与轮次上限,采用账号独立循环、统一账号级退避、单请求并发闸门与首发错峰 - 预热与建连优化:默认关闭预热(避免单账号超时连坐整机开抢),开启时转为后台非阻塞协程;WebSocket 握手超时从 10s 降至 1500ms - 双时间日志:logger 增加 SetServerOffset,校准后日志前缀自动渲染「本地时间(H虎牙估算时间)」,实现跨机器绝对毫秒对齐 - 默认配置调优:版本提升至 v0.5.0,默认线程调为 2(缓解多实例并发限流),前端自动平滑迁移 - 验证方式:go test ./... -count=1 全通,npm run build 构建正常,单测覆盖双时间与预热配置
This commit is contained in:
@@ -139,7 +139,7 @@ func TestRunTargetsDirectExchangeSkipsListAndStopsPerAccount(t *testing.T) {
|
||||
|
||||
err := runner.RunTargets(context.Background(), accounts, []Target{{PID: 12889}}, Config{
|
||||
SID: 2203,
|
||||
Loops: 5,
|
||||
maxAttempts: 5,
|
||||
Delay: time.Millisecond,
|
||||
Concurrency: 2,
|
||||
StopAccountAfterSuccess: true,
|
||||
@@ -165,9 +165,9 @@ func TestRunTargetsTerminalNotEnoughStopsAccountTarget(t *testing.T) {
|
||||
runner := Runner{Client: client}
|
||||
|
||||
err := runner.RunTargets(context.Background(), []Account{{UID: 1, Cookie: "yyuid=1"}}, []Target{{PID: 12889}}, Config{
|
||||
SID: 2203,
|
||||
Loops: 4,
|
||||
Delay: time.Millisecond,
|
||||
SID: 2203,
|
||||
maxAttempts: 4,
|
||||
Delay: time.Millisecond,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -178,7 +178,7 @@ func TestRunTargetsTerminalNotEnoughStopsAccountTarget(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunTargetsTooFrequentBackoffSkipsNextLoop(t *testing.T) {
|
||||
func TestRunTargetsTooFrequentBackoffDelaysActualAttempts(t *testing.T) {
|
||||
client := &fakeActivityClient{
|
||||
exchangeFn: func(uid int64, sid, pid int32) (*huya.ScoreExchangePrizeResp, error) {
|
||||
return &huya.ScoreExchangePrizeResp{Status: 500, Msg: "操作频繁,请稍后再试"}, nil
|
||||
@@ -186,9 +186,10 @@ func TestRunTargetsTooFrequentBackoffSkipsNextLoop(t *testing.T) {
|
||||
}
|
||||
runner := Runner{Client: client}
|
||||
|
||||
started := time.Now()
|
||||
err := runner.RunTargets(context.Background(), []Account{{UID: 1, Cookie: "yyuid=1"}}, []Target{{PID: 12889}}, Config{
|
||||
SID: 2203,
|
||||
Loops: 2,
|
||||
maxAttempts: 2,
|
||||
Delay: time.Millisecond,
|
||||
TooFrequentBackoff: 50 * time.Millisecond,
|
||||
}, nil)
|
||||
@@ -196,8 +197,11 @@ func TestRunTargetsTooFrequentBackoffSkipsNextLoop(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, exchangeCount, _ := client.counts()
|
||||
if exchangeCount != 1 {
|
||||
t.Fatalf("exchangeCount=%d, want 1", exchangeCount)
|
||||
if exchangeCount != 2 {
|
||||
t.Fatalf("exchangeCount=%d, want 2(maxAttempts 应统计真实尝试)", exchangeCount)
|
||||
}
|
||||
if elapsed := time.Since(started); elapsed < 45*time.Millisecond {
|
||||
t.Fatalf("退避没有生效,耗时=%v", elapsed)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -214,7 +218,7 @@ func TestRunTargetsSharedListForNameTargets(t *testing.T) {
|
||||
|
||||
err := runner.RunTargets(context.Background(), accounts, []Target{{Name: "小黄鸭"}}, Config{
|
||||
SID: 2203,
|
||||
Loops: 1,
|
||||
maxAttempts: 1,
|
||||
Concurrency: 2,
|
||||
StopAccountAfterSuccess: true,
|
||||
ListBeforeExchange: true,
|
||||
@@ -251,7 +255,7 @@ func TestRunTargetsAutoSelectPrefersPrizeWithLeft(t *testing.T) {
|
||||
runner := Runner{Client: client}
|
||||
err := runner.RunTargets(context.Background(), []Account{{UID: 1, Cookie: "yyuid=1"}}, []Target{{Auto: true}}, Config{
|
||||
SID: 2203,
|
||||
Loops: 1,
|
||||
maxAttempts: 1,
|
||||
StopAccountAfterSuccess: true,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
@@ -287,7 +291,7 @@ func TestRunTargetsCategoryFilter(t *testing.T) {
|
||||
err := runner.RunTargets(context.Background(), []Account{{UID: 1, Cookie: "yyuid=1"}}, []Target{{Auto: true}}, Config{
|
||||
SID: 2277,
|
||||
Category: "限量返场",
|
||||
Loops: 1,
|
||||
maxAttempts: 1,
|
||||
StopAccountAfterSuccess: true,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
@@ -313,7 +317,7 @@ func TestRunTargetsCategoryFilterMisses(t *testing.T) {
|
||||
err := runner.RunTargets(context.Background(), []Account{{UID: 1, Cookie: "yyuid=1"}}, []Target{{Name: "雪山"}}, Config{
|
||||
SID: 2277,
|
||||
Category: "限量返场",
|
||||
Loops: 1,
|
||||
maxAttempts: 1,
|
||||
ListBeforeExchange: true,
|
||||
}, func(result Result) {
|
||||
results = append(results, result)
|
||||
@@ -340,7 +344,7 @@ func TestRunTargetsRefreshAfterSuccessEmitsRefreshResults(t *testing.T) {
|
||||
|
||||
err := runner.RunTargets(context.Background(), []Account{{UID: 1, Cookie: "yyuid=1"}}, []Target{{PID: 12889}}, Config{
|
||||
SID: 2203,
|
||||
Loops: 1,
|
||||
maxAttempts: 1,
|
||||
StopAccountAfterSuccess: true,
|
||||
RefreshAfterSuccess: true,
|
||||
}, func(result Result) {
|
||||
@@ -377,7 +381,7 @@ func TestRunTargetsConditionGateKeepsRetrying(t *testing.T) {
|
||||
|
||||
err := runner.RunTargets(context.Background(), []Account{{UID: 1, Cookie: "yyuid=1"}}, []Target{{PID: 12889}}, Config{
|
||||
SID: 2203,
|
||||
Loops: 3,
|
||||
maxAttempts: 3,
|
||||
Delay: 10 * time.Millisecond,
|
||||
ErrorBackoff: time.Millisecond,
|
||||
}, nil)
|
||||
@@ -414,7 +418,7 @@ func TestRunTargetsExchangesPrizeOnceWhenTargetsDuplicated(t *testing.T) {
|
||||
{PID: 13409},
|
||||
}, Config{
|
||||
SID: 2277,
|
||||
Loops: 3,
|
||||
maxAttempts: 3,
|
||||
Delay: time.Millisecond,
|
||||
StopAccountAfterSuccess: true,
|
||||
}, nil)
|
||||
@@ -435,7 +439,7 @@ func TestRunTargetsWaitsForRefreshBeforeReturn(t *testing.T) {
|
||||
seen := map[string]bool{}
|
||||
err := runner.RunTargets(context.Background(), []Account{{UID: 1, Cookie: "yyuid=1"}}, []Target{{PID: 13409}}, Config{
|
||||
SID: 2277,
|
||||
Loops: 1,
|
||||
maxAttempts: 1,
|
||||
StopAccountAfterSuccess: true,
|
||||
RefreshAfterSuccess: true,
|
||||
}, func(result Result) {
|
||||
@@ -469,7 +473,7 @@ func TestRunTargetsDoesNotExchangeSamePrizeTwiceViaNameMatch(t *testing.T) {
|
||||
{Name: "礼包"},
|
||||
}, Config{
|
||||
SID: 2277,
|
||||
Loops: 2,
|
||||
maxAttempts: 2,
|
||||
Delay: time.Millisecond,
|
||||
DirectExchange: false,
|
||||
StopAccountAfterSuccess: true,
|
||||
@@ -504,7 +508,7 @@ func TestRunTargetsSoldOutKeepsProbingUntilSuccess(t *testing.T) {
|
||||
|
||||
err := runner.RunTargets(context.Background(), []Account{{UID: 1, Cookie: "yyuid=1"}}, []Target{{PID: 12889}}, Config{
|
||||
SID: 2203,
|
||||
Loops: 6,
|
||||
maxAttempts: 6,
|
||||
Delay: time.Millisecond,
|
||||
ErrorBackoff: time.Millisecond,
|
||||
StopAccountAfterSuccess: true,
|
||||
@@ -529,7 +533,7 @@ func TestRunTargetsStopOnSoldOutTerminatesTarget(t *testing.T) {
|
||||
|
||||
err := runner.RunTargets(context.Background(), []Account{{UID: 1, Cookie: "yyuid=1"}}, []Target{{PID: 12889}}, Config{
|
||||
SID: 2203,
|
||||
Loops: 5,
|
||||
maxAttempts: 5,
|
||||
Delay: time.Millisecond,
|
||||
ErrorBackoff: time.Millisecond,
|
||||
StopOnSoldOut: true,
|
||||
@@ -554,7 +558,7 @@ func TestRunTargetsOffShelfTerminatesTarget(t *testing.T) {
|
||||
|
||||
err := runner.RunTargets(context.Background(), []Account{{UID: 1, Cookie: "yyuid=1"}}, []Target{{PID: 12889}}, Config{
|
||||
SID: 2203,
|
||||
Loops: 4,
|
||||
maxAttempts: 4,
|
||||
Delay: time.Millisecond,
|
||||
ErrorBackoff: time.Millisecond,
|
||||
}, nil)
|
||||
@@ -593,7 +597,7 @@ func TestRunTargetsConcurrencyLimitsInFlightRequests(t *testing.T) {
|
||||
|
||||
err := runner.RunTargets(context.Background(), accounts, []Target{{PID: 12889}}, Config{
|
||||
SID: 2203,
|
||||
Loops: 1,
|
||||
maxAttempts: 1,
|
||||
Delay: time.Millisecond,
|
||||
Concurrency: 2,
|
||||
TooFrequentBackoff: 10 * time.Millisecond,
|
||||
@@ -619,7 +623,7 @@ func TestRunTargetsEmitsSummary(t *testing.T) {
|
||||
// 异步刷新只在有 emit 回调时才跑(没地方上报就不发请求),这里给个空回调。
|
||||
err := runner.RunTargets(context.Background(), accounts, []Target{{PID: 12889}}, Config{
|
||||
SID: 2203,
|
||||
Loops: 3,
|
||||
maxAttempts: 3,
|
||||
Delay: time.Millisecond,
|
||||
Concurrency: 2,
|
||||
StopAccountAfterSuccess: true,
|
||||
|
||||
Reference in New Issue
Block a user