feat: 自定义重试间隔策略 + 修复按钮hover样式
- 新增重试间隔阶段配置,支持自定义时间分界点和频率 - 默认配置: 0-8秒=130ms, 8-25秒=260ms, 25-75秒=900ms - 设置面板添加可增删的阶段配置 UI - 修复按钮 hover 时文字变白的问题
This commit is contained in:
@@ -81,6 +81,16 @@ func defaultConfig() types.Config {
|
||||
LogLevel: "info",
|
||||
Concurrency: constants.DefaultConcurrency,
|
||||
Attempts: constants.DefaultAttempts,
|
||||
RetryStages: defaultRetryStages(),
|
||||
}
|
||||
}
|
||||
|
||||
// defaultRetryStages 返回默认重试间隔阶段配置。
|
||||
func defaultRetryStages() []types.RetryStage {
|
||||
return []types.RetryStage{
|
||||
{MaxElapsed: 8, Interval: 130},
|
||||
{MaxElapsed: 25, Interval: 260},
|
||||
{MaxElapsed: 75, Interval: 900},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,6 +128,9 @@ func LoadConfig(path string) types.Config {
|
||||
if cfg.LogLevel == "" {
|
||||
cfg.LogLevel = "info"
|
||||
}
|
||||
if len(cfg.RetryStages) == 0 {
|
||||
cfg.RetryStages = defaultRetryStages()
|
||||
}
|
||||
logger.LogEvent("配置", "success", "配置加载完成: %s", path)
|
||||
return cfg
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
+13
-6
@@ -7,12 +7,19 @@ import (
|
||||
|
||||
// Config 应用配置。
|
||||
type Config struct {
|
||||
TimeAPIURL string `json:"timeApiUrl"`
|
||||
TraceEnabled bool `json:"traceEnabled"`
|
||||
TraceMaxBody int `json:"traceMaxBody"`
|
||||
LogLevel string `json:"logLevel"` // debug / info / warn / error
|
||||
Concurrency int `json:"concurrency"` // 抢购并发数
|
||||
Attempts int `json:"attempts"` // 单号重试次数
|
||||
TimeAPIURL string `json:"timeApiUrl"`
|
||||
TraceEnabled bool `json:"traceEnabled"`
|
||||
TraceMaxBody int `json:"traceMaxBody"`
|
||||
LogLevel string `json:"logLevel"` // debug / info / warn / error
|
||||
Concurrency int `json:"concurrency"` // 抢购并发数
|
||||
Attempts int `json:"attempts"` // 单号重试次数
|
||||
RetryStages []RetryStage `json:"retryStages"` // 重试间隔阶段配置
|
||||
}
|
||||
|
||||
// RetryStage 重试间隔阶段配置。
|
||||
type RetryStage struct {
|
||||
MaxElapsed int `json:"maxElapsed"` // 经过秒数上限
|
||||
Interval int `json:"interval"` // 重试间隔(ms)
|
||||
}
|
||||
|
||||
// AppPaths 运行时路径。
|
||||
|
||||
Reference in New Issue
Block a user