feat: 自定义重试间隔策略 + 修复按钮hover样式
- 新增重试间隔阶段配置,支持自定义时间分界点和频率 - 默认配置: 0-8秒=130ms, 8-25秒=260ms, 25-75秒=900ms - 设置面板添加可增删的阶段配置 UI - 修复按钮 hover 时文字变白的问题
This commit is contained in:
@@ -46,6 +46,8 @@ type Scheduler struct {
|
||||
concurrency int // 抢购并发数
|
||||
attempts int // 单号重试次数
|
||||
|
||||
retryStages []types.RetryStage // 重试间隔阶段配置
|
||||
|
||||
accountsMu sync.RWMutex
|
||||
accounts []types.Account
|
||||
|
||||
@@ -65,7 +67,7 @@ type Scheduler struct {
|
||||
}
|
||||
|
||||
// New 创建抢购调度器。
|
||||
func New(cli *client.DouyuClient, paths types.AppPaths, font *truetype.Font, concurrency, attempts int) *Scheduler {
|
||||
func New(cli *client.DouyuClient, paths types.AppPaths, font *truetype.Font, concurrency, attempts int, retryStages []types.RetryStage) *Scheduler {
|
||||
if concurrency <= 0 {
|
||||
concurrency = constants.DefaultConcurrency
|
||||
}
|
||||
@@ -78,18 +80,22 @@ func New(cli *client.DouyuClient, paths types.AppPaths, font *truetype.Font, con
|
||||
font: font,
|
||||
concurrency: concurrency,
|
||||
attempts: attempts,
|
||||
retryStages: retryStages,
|
||||
results: make(map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateSettings 运行时更新并发数和重试次数。
|
||||
func (s *Scheduler) UpdateSettings(concurrency, attempts int) {
|
||||
// UpdateSettings 运行时更新并发数、重试次数和重试间隔。
|
||||
func (s *Scheduler) UpdateSettings(concurrency, attempts int, retryStages []types.RetryStage) {
|
||||
if concurrency > 0 {
|
||||
s.concurrency = concurrency
|
||||
}
|
||||
if attempts > 0 {
|
||||
s.attempts = attempts
|
||||
}
|
||||
if len(retryStages) > 0 {
|
||||
s.retryStages = retryStages
|
||||
}
|
||||
}
|
||||
|
||||
// LoadAccounts 从账号文件加载账号。
|
||||
@@ -300,7 +306,7 @@ func (s *Scheduler) RunNow(accountProducts map[string]string) {
|
||||
break
|
||||
}
|
||||
|
||||
retryInterval := seckillRetryInterval(time.Since(startTime))
|
||||
retryInterval := s.getRetryInterval(time.Since(startTime))
|
||||
if retryInterval > 0 && time.Since(startTime)+retryInterval < seckillMaxDuration {
|
||||
time.Sleep(jitterDuration(retryInterval))
|
||||
}
|
||||
@@ -421,15 +427,19 @@ func (s *Scheduler) executeRound(attempts []types.ExchangeAttempt) roundOutcome
|
||||
return outcome
|
||||
}
|
||||
|
||||
func seckillRetryInterval(elapsed time.Duration) time.Duration {
|
||||
switch {
|
||||
case elapsed < 8*time.Second:
|
||||
return 180 * time.Millisecond
|
||||
case elapsed < 25*time.Second:
|
||||
return 350 * time.Millisecond
|
||||
default:
|
||||
return 900 * time.Millisecond
|
||||
// getRetryInterval 根据配置的重试阶段计算当前应该使用的重试间隔。
|
||||
func (s *Scheduler) getRetryInterval(elapsed time.Duration) time.Duration {
|
||||
elapsedSec := int(elapsed.Seconds())
|
||||
|
||||
// 使用配置的阶段
|
||||
for _, stage := range s.retryStages {
|
||||
if stage.MaxElapsed > 0 && elapsedSec < stage.MaxElapsed && stage.Interval > 0 {
|
||||
return time.Duration(stage.Interval) * time.Millisecond
|
||||
}
|
||||
}
|
||||
|
||||
// 默认值(如果没有配置或配置无效)
|
||||
return 900 * time.Millisecond
|
||||
}
|
||||
|
||||
func jitterDuration(base time.Duration) time.Duration {
|
||||
|
||||
Reference in New Issue
Block a user