feat: 完善代理池与抢购链路,发布 0.5.0
This commit is contained in:
+19
-12
@@ -164,17 +164,18 @@ func envInt(key string, fallback int) int {
|
||||
// defaultConfig 返回内置默认配置。
|
||||
func defaultConfig() types.Config {
|
||||
return types.Config{
|
||||
Activity: activity.DefaultActivityID,
|
||||
TimeAPIURL: constants.DefaultTimeAPI,
|
||||
TraceEnabled: false,
|
||||
TraceMaxBody: 0,
|
||||
LogLevel: "info",
|
||||
Concurrency: constants.DefaultConcurrency,
|
||||
Attempts: constants.DefaultAttempts,
|
||||
RetryStages: defaultRetryStages(),
|
||||
SeckillMode: types.SeckillModeAggressive,
|
||||
XiaodianPageSize: 200,
|
||||
AccountsPerProxy: constants.DefaultAccountsPerProxy,
|
||||
Activity: activity.DefaultActivityID,
|
||||
TimeAPIURL: constants.DefaultTimeAPI,
|
||||
TraceEnabled: false,
|
||||
TraceMaxBody: 0,
|
||||
LogLevel: "info",
|
||||
Concurrency: constants.DefaultConcurrency,
|
||||
Attempts: constants.DefaultAttempts,
|
||||
RetryStages: defaultRetryStages(),
|
||||
SeckillMode: types.SeckillModeAggressive,
|
||||
XiaodianPageSize: 200,
|
||||
AccountsPerProxy: constants.DefaultAccountsPerProxy,
|
||||
ProxySparePercent: constants.DefaultProxySparePercent,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -214,7 +215,7 @@ func LoadConfig(path string) types.Config {
|
||||
if cfg.Activity == "" {
|
||||
cfg.Activity = activity.DefaultActivityID
|
||||
}
|
||||
if cfg.TimeAPIURL == "" {
|
||||
if cfg.TimeAPIURL == "" || cfg.TimeAPIURL == constants.LegacyDefaultTimeAPI {
|
||||
cfg.TimeAPIURL = constants.DefaultTimeAPI
|
||||
}
|
||||
if cfg.Concurrency <= 0 {
|
||||
@@ -242,6 +243,12 @@ func normalizeProxyConfig(cfg *types.Config) {
|
||||
if cfg.AccountsPerProxy > constants.MaxAccountsPerProxy {
|
||||
cfg.AccountsPerProxy = constants.MaxAccountsPerProxy
|
||||
}
|
||||
if cfg.ProxySparePercent <= 0 {
|
||||
cfg.ProxySparePercent = constants.DefaultProxySparePercent
|
||||
}
|
||||
if cfg.ProxySparePercent > constants.MaxProxySparePercent {
|
||||
cfg.ProxySparePercent = constants.MaxProxySparePercent
|
||||
}
|
||||
}
|
||||
|
||||
// normalizeXiaodianConfig 约束和平小店分页参数,接口单页最大为 200。
|
||||
|
||||
@@ -128,3 +128,30 @@ func TestLoadConfigNormalizesAccountsPerProxy(t *testing.T) {
|
||||
t.Fatalf("AccountsPerProxy=%d, want %d", got, constants.MaxAccountsPerProxy)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadConfigNormalizesProxySparePercent(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "config.json")
|
||||
if err := os.WriteFile(path, []byte(`{"proxySparePercent":0}`), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := LoadConfig(path).ProxySparePercent; got != constants.DefaultProxySparePercent {
|
||||
t.Fatalf("ProxySparePercent=%d, want %d", got, constants.DefaultProxySparePercent)
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(`{"proxySparePercent":999}`), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := LoadConfig(path).ProxySparePercent; got != constants.MaxProxySparePercent {
|
||||
t.Fatalf("ProxySparePercent=%d, want %d", got, constants.MaxProxySparePercent)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadConfigMigratesLegacyTimeAPI(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "config.json")
|
||||
content := []byte(`{"timeApiUrl":"` + constants.LegacyDefaultTimeAPI + `"}`)
|
||||
if err := os.WriteFile(path, content, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := LoadConfig(path).TimeAPIURL; got != constants.DefaultTimeAPI {
|
||||
t.Fatalf("TimeAPIURL=%q, want %q", got, constants.DefaultTimeAPI)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user