fix(seckill): 代理失败时快速回退直连,发布 0.5.3

This commit is contained in:
yml
2026-11-20 13:37:00 +08:00
parent f4b3cfa486
commit 79b503d1a6
5 changed files with 224 additions and 21 deletions
+1 -1
View File
@@ -3,7 +3,7 @@ package constants
import "time" import "time"
// Version 应用版本号,与 wails.json version 同步。 // Version 应用版本号,与 wails.json version 同步。
const Version = "0.5.2" const Version = "0.5.3"
// BuildTime 构建时间,由编译时 ldflags 注入。 // BuildTime 构建时间,由编译时 ldflags 注入。
var BuildTime = "unknown" var BuildTime = "unknown"
+121 -18
View File
@@ -26,6 +26,9 @@ const (
proxyValidationTimeout = 3 * time.Second proxyValidationTimeout = 3 * time.Second
proxyValidationWorkers = 3 proxyValidationWorkers = 3
proxyValidateAttempts = 1 proxyValidateAttempts = 1
proxyImmediateTimeout = 5 * time.Second
proxyScheduledTimeout = 20 * time.Second
proxyRefillTimeout = 20 * time.Second
) )
type proxySettingsSnapshot struct { type proxySettingsSnapshot struct {
@@ -131,6 +134,7 @@ func (s *Scheduler) UpdateProxySettings(cfg types.Config) {
} }
s.clientMu.Lock() s.clientMu.Lock()
s.accountClientsDirty = true s.accountClientsDirty = true
s.proxyFallbackActive = false
s.clientMu.Unlock() s.clientMu.Unlock()
} }
@@ -182,6 +186,7 @@ func (s *Scheduler) closeAccountClients() {
s.proxyStandbyTarget = 0 s.proxyStandbyTarget = 0
s.proxyPoolGeneration++ s.proxyPoolGeneration++
s.proxyRefillInFlight = false s.proxyRefillInFlight = false
s.proxyFallbackActive = false
s.clientMu.Unlock() s.clientMu.Unlock()
closeDouyuClientList(oldClients) closeDouyuClientList(oldClients)
} }
@@ -212,6 +217,7 @@ func (s *Scheduler) replaceAccountClients(pool preparedProxyPool) {
s.proxyStandbyTarget = pool.standbyTarget s.proxyStandbyTarget = pool.standbyTarget
s.proxyPoolGeneration++ s.proxyPoolGeneration++
s.proxyRefillInFlight = false s.proxyRefillInFlight = false
s.proxyFallbackActive = false
s.clientMu.Unlock() s.clientMu.Unlock()
closeDouyuClientList(oldClients) closeDouyuClientList(oldClients)
} }
@@ -272,6 +278,9 @@ func (s *Scheduler) accountClientsCover(accountNames []string) bool {
func (s *Scheduler) calibrationClient() *client.DouyuClient { func (s *Scheduler) calibrationClient() *client.DouyuClient {
s.clientMu.RLock() s.clientMu.RLock()
defer s.clientMu.RUnlock() defer s.clientMu.RUnlock()
if s.proxyFallbackActive && s.directClient != nil {
return s.directClient
}
if len(s.accountClientOrder) > 0 && s.accountClientOrder[0] != nil { if len(s.accountClientOrder) > 0 && s.accountClientOrder[0] != nil {
return s.accountClientOrder[0] return s.accountClientOrder[0]
} }
@@ -287,6 +296,12 @@ func (s *Scheduler) allClients() []*client.DouyuClient {
seen[s.client] = struct{}{} seen[s.client] = struct{}{}
out = append(out, s.client) out = append(out, s.client)
} }
if (s.proxyFallbackActive || s.client == nil) && s.directClient != nil {
if _, ok := seen[s.directClient]; !ok {
seen[s.directClient] = struct{}{}
out = append(out, s.directClient)
}
}
for _, cli := range s.accountClientOrder { for _, cli := range s.accountClientOrder {
if cli == nil { if cli == nil {
continue continue
@@ -315,6 +330,10 @@ func (s *Scheduler) prepareProxyGroups(accountProducts map[string]string, forceR
s.closeAccountClients() s.closeAccountClients()
return nil return nil
} }
if !forceRefresh && s.proxyFallbackSnapshot() {
logger.LogEvent("代理", "warn", "代理此前准备失败,本次抢购直接使用直连,避免再次等待")
return nil
}
if !forceRefresh && s.accountClientsCover(names) { if !forceRefresh && s.accountClientsCover(names) {
return nil return nil
} }
@@ -324,16 +343,23 @@ func (s *Scheduler) prepareProxyGroups(accountProducts map[string]string, forceR
standbyTarget := proxyStandbyCount(activeCount, cfg.sparePercent) standbyTarget := proxyStandbyCount(activeCount, cfg.sparePercent)
base := s.clientSnapshot() base := s.clientSnapshot()
if base == nil { if base == nil {
return errors.New("基础客户端未初始化") s.activateProxyDirectFallback(errors.New("基础客户端未初始化"))
return nil
} }
poolStartedAt := time.Now() poolStartedAt := time.Now()
proxies, err := s.buildProxyPool(base, cfg, activeCount, activeCount+standbyTarget, nil) timeout := proxyImmediateTimeout
if forceRefresh {
timeout = proxyScheduledTimeout
}
proxies, err := s.buildProxyPool(base, cfg, activeCount, activeCount+standbyTarget, nil, timeout)
if err != nil { if err != nil {
return err s.activateProxyDirectFallback(err)
return nil
} }
if len(proxies) < activeCount { if len(proxies) < activeCount {
return fmt.Errorf("代理数量不足: 需要%d个活动代理,实际%d个", activeCount, len(proxies)) s.activateProxyDirectFallback(fmt.Errorf("代理数量不足: 需要%d个活动代理,实际%d个", activeCount, len(proxies)))
return nil
} }
if len(proxies) < activeCount+standbyTarget { if len(proxies) < activeCount+standbyTarget {
logger.LogEvent("代理", "warn", "备用代理不足: 目标%d个,实际%d个;活动代理不受影响", logger.LogEvent("代理", "warn", "备用代理不足: 目标%d个,实际%d个;活动代理不受影响",
@@ -395,6 +421,25 @@ func (s *Scheduler) prepareProxyGroups(accountProducts map[string]string, forceR
return nil return nil
} }
func (s *Scheduler) proxyFallbackSnapshot() bool {
s.clientMu.RLock()
defer s.clientMu.RUnlock()
return s.proxyFallbackActive
}
func (s *Scheduler) activateProxyDirectFallback(cause error) {
s.closeAccountClients()
s.clientMu.Lock()
active := s.directClient != nil
s.proxyFallbackActive = active
s.clientMu.Unlock()
if active {
logger.LogEvent("代理", "warn", "代理准备失败,已回退直连以保证抢购继续: %v", cause)
return
}
logger.LogEvent("代理", "error", "代理准备失败且直连客户端不可用: %v", cause)
}
func effectiveProxyStandbyTarget(cfg proxySettingsSnapshot, target, actual int) int { func effectiveProxyStandbyTarget(cfg proxySettingsSnapshot, target, actual int) int {
if cfg.proxyAPI == "" { if cfg.proxyAPI == "" {
return actual return actual
@@ -455,15 +500,16 @@ func proxyStandbyCount(activeCount, sparePercent int) int {
return count return count
} }
func (s *Scheduler) buildProxyPool(base *client.DouyuClient, cfg proxySettingsSnapshot, required, desired int, excludedURLs map[string]struct{}) ([]validatedProxy, error) { func (s *Scheduler) buildProxyPool(base *client.DouyuClient, cfg proxySettingsSnapshot, required, desired int, excludedURLs map[string]struct{}, timeout time.Duration) ([]validatedProxy, error) {
if desired < required { if desired < required {
desired = required desired = required
} }
if required <= 0 { if required <= 0 {
return nil, nil return nil, nil
} }
deadline := time.Now().Add(timeout)
if cfg.proxyAPI != "" { if cfg.proxyAPI != "" {
proxies, err := s.fetchAndValidateProxyPool(base, cfg, desired, excludedURLs) proxies, err := s.fetchAndValidateProxyPool(base, cfg, desired, excludedURLs, timeout)
if len(proxies) >= required { if len(proxies) >= required {
if len(proxies) < desired { if len(proxies) < desired {
logger.LogEvent("代理", "warn", "动态代理备用数量不足: 目标%d个,实际%d个", desired, len(proxies)) logger.LogEvent("代理", "warn", "动态代理备用数量不足: 目标%d个,实际%d个", desired, len(proxies))
@@ -490,7 +536,11 @@ func (s *Scheduler) buildProxyPool(base *client.DouyuClient, cfg proxySettingsSn
} }
templates := make([]validatedProxy, 0, validateCount) templates := make([]validatedProxy, 0, validateCount)
for _, rawURL := range staticURLs[:validateCount] { for _, rawURL := range staticURLs[:validateCount] {
proxy, err := s.validateProxy(base, rawURL, "静态") if time.Now().After(deadline) {
closeValidatedProxies(templates)
return nil, errors.New("静态代理校验超时")
}
proxy, err := s.validateProxy(base, rawURL, "静态", time.Until(deadline))
if err != nil { if err != nil {
closeValidatedProxies(templates) closeValidatedProxies(templates)
return nil, fmt.Errorf("静态代理不可用: %w", err) return nil, fmt.Errorf("静态代理不可用: %w", err)
@@ -570,7 +620,7 @@ func splitProxyURLs(raw string) []string {
return out return out
} }
func (s *Scheduler) fetchAndValidateProxyPool(base *client.DouyuClient, cfg proxySettingsSnapshot, count int, excludedURLs map[string]struct{}) ([]validatedProxy, error) { func (s *Scheduler) fetchAndValidateProxyPool(base *client.DouyuClient, cfg proxySettingsSnapshot, count int, excludedURLs map[string]struct{}, timeout time.Duration) ([]validatedProxy, error) {
service := s.proxyService service := s.proxyService
if service == nil { if service == nil {
service = proxyprovider.NewService() service = proxyprovider.NewService()
@@ -583,6 +633,7 @@ func (s *Scheduler) fetchAndValidateProxyPool(base *client.DouyuClient, cfg prox
seen[rawURL] = struct{}{} seen[rawURL] = struct{}{}
} }
var lastErr error var lastErr error
deadline := time.Now().Add(timeout)
rounds := proxyFetchRounds rounds := proxyFetchRounds
if count > rounds { if count > rounds {
@@ -592,12 +643,23 @@ func (s *Scheduler) fetchAndValidateProxyPool(base *client.DouyuClient, cfg prox
rounds = proxyFetchMaxRounds rounds = proxyFetchMaxRounds
} }
for round := 0; round < rounds; round++ { for round := 0; round < rounds; round++ {
remaining := time.Until(deadline)
if remaining <= 0 {
if lastErr == nil {
lastErr = errors.New("代理准备总时长超时")
}
break
}
need := count - len(proxies) need := count - len(proxies)
if need <= 0 { if need <= 0 {
break break
} }
ctx, cancel := context.WithTimeout(context.Background(), proxyRequestTimeout) requestTimeout := proxyRequestTimeout
if remaining < requestTimeout {
requestTimeout = remaining
}
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
endpoints, err := service.ExtractWithWhitelist(ctx, cfg.proxyAPI, need, proxyprovider.WhitelistConfig{ endpoints, err := service.ExtractWithWhitelist(ctx, cfg.proxyAPI, need, proxyprovider.WhitelistConfig{
APIURL: cfg.proxyAPI, APIURL: cfg.proxyAPI,
LoginPassword: cfg.whitelistPassword, LoginPassword: cfg.whitelistPassword,
@@ -612,7 +674,7 @@ func (s *Scheduler) fetchAndValidateProxyPool(base *client.DouyuClient, cfg prox
break break
} }
} else { } else {
validated := s.validateProxyBatch(base, endpoints) validated := s.validateProxyBatch(base, endpoints, deadline)
for _, item := range validated { for _, item := range validated {
if item.err != nil { if item.err != nil {
lastErr = item.err lastErr = item.err
@@ -641,7 +703,13 @@ func (s *Scheduler) fetchAndValidateProxyPool(base *client.DouyuClient, cfg prox
} }
if round < rounds-1 { if round < rounds-1 {
time.Sleep(proxyFetchRetryWait) wait := proxyFetchRetryWait
if remaining := time.Until(deadline); remaining < wait {
wait = remaining
}
if wait > 0 {
time.Sleep(wait)
}
} }
} }
@@ -685,6 +753,10 @@ func (s *Scheduler) recordProxyResult(proxyClient *client.DouyuClient, err error
} }
event, expiredClients, switched := s.switchGroupToStandbyLocked(proxyClient, group) event, expiredClients, switched := s.switchGroupToStandbyLocked(proxyClient, group)
if !switched && s.directClient != nil {
event = s.switchGroupToDirectLocked(proxyClient, group)
switched = true
}
needRefill := false needRefill := false
failedAccounts := 0 failedAccounts := 0
failedURL := "" failedURL := ""
@@ -743,6 +815,22 @@ func (s *Scheduler) switchGroupToStandbyLocked(oldClient *client.DouyuClient, gr
return proxySwitchEvent{}, expiredClients, false return proxySwitchEvent{}, expiredClients, false
} }
func (s *Scheduler) switchGroupToDirectLocked(oldClient *client.DouyuClient, group *proxyRuntimeGroup) proxySwitchEvent {
event := proxySwitchEvent{
accountNames: append([]string(nil), group.accountNames...),
fromURL: group.activeURL,
}
group.activeURL = ""
group.consecutiveFailures = 0
group.failed = false
for _, accountName := range group.accountNames {
s.accountClients[accountName] = s.directClient
}
delete(s.proxyGroups, oldClient)
replaceClientInOrder(s.accountClientOrder, oldClient, s.directClient)
return event
}
func replaceClientInOrder(order []*client.DouyuClient, oldClient, newClient *client.DouyuClient) { func replaceClientInOrder(order []*client.DouyuClient, oldClient, newClient *client.DouyuClient) {
for i, cli := range order { for i, cli := range order {
if cli == oldClient { if cli == oldClient {
@@ -777,7 +865,7 @@ func (s *Scheduler) startProxyRefill() {
} }
func (s *Scheduler) refillProxyStandbys(base *client.DouyuClient, cfg proxySettingsSnapshot, generation uint64, need int, excludedURLs map[string]struct{}) { func (s *Scheduler) refillProxyStandbys(base *client.DouyuClient, cfg proxySettingsSnapshot, generation uint64, need int, excludedURLs map[string]struct{}) {
proxies, err := s.fetchAndValidateProxyPool(base, cfg, need, excludedURLs) proxies, err := s.fetchAndValidateProxyPool(base, cfg, need, excludedURLs, proxyRefillTimeout)
if err != nil { if err != nil {
logger.LogEvent("代理", "warn", "备用代理补提取未完全成功: %v", err) logger.LogEvent("代理", "warn", "备用代理补提取未完全成功: %v", err)
} }
@@ -877,10 +965,17 @@ func copyStringSet(source map[string]struct{}) map[string]struct{} {
func logProxySwitch(label string, event proxySwitchEvent) { func logProxySwitch(label string, event proxySwitchEvent) {
logger.LogEvent("代理", "warn", "%s: 账号=%d %s -> %s IP=%s 归属=%s RTT=%s 来源=%s", logger.LogEvent("代理", "warn", "%s: 账号=%d %s -> %s IP=%s 归属=%s RTT=%s 来源=%s",
label, len(event.accountNames), client.MaskProxyURL(event.fromURL), client.MaskProxyURL(event.toURL), label, len(event.accountNames), proxyDisplay(event.fromURL), proxyDisplay(event.toURL),
event.toIP, event.toRegion, event.toRTT.Round(time.Millisecond), event.toSource) event.toIP, event.toRegion, event.toRTT.Round(time.Millisecond), event.toSource)
} }
func proxyDisplay(raw string) string {
if strings.TrimSpace(raw) == "" {
return "直连"
}
return client.MaskProxyURL(raw)
}
func proxyPoolExpiresAt(apiURL string, now time.Time) time.Time { func proxyPoolExpiresAt(apiURL string, now time.Time) time.Time {
minutes := 10 minutes := 10
if u, err := url.Parse(strings.TrimSpace(apiURL)); err == nil { if u, err := url.Parse(strings.TrimSpace(apiURL)); err == nil {
@@ -896,13 +991,16 @@ func proxyPoolExpiresAt(apiURL string, now time.Time) time.Time {
return now.Add(time.Duration(minutes) * time.Minute) return now.Add(time.Duration(minutes) * time.Minute)
} }
func (s *Scheduler) validateProxy(base *client.DouyuClient, proxyURL, source string) (validatedProxy, error) { func (s *Scheduler) validateProxy(base *client.DouyuClient, proxyURL, source string, timeout time.Duration) (validatedProxy, error) {
cli, err := client.NewWithProxy(base.Activity, base.TimeAPIURL, base.Trace, proxyURL) cli, err := client.NewWithProxy(base.Activity, base.TimeAPIURL, base.Trace, proxyURL)
if err != nil { if err != nil {
return validatedProxy{}, err return validatedProxy{}, err
} }
if cli.HTTPClient != nil && cli.HTTPClient.Timeout > proxyValidationTimeout { if timeout <= 0 || timeout > proxyValidationTimeout {
cli.HTTPClient.Timeout = proxyValidationTimeout timeout = proxyValidationTimeout
}
if cli.HTTPClient != nil {
cli.HTTPClient.Timeout = timeout
} }
var lastErr error var lastErr error
for attempt := 0; attempt < proxyValidateAttempts; attempt++ { for attempt := 0; attempt < proxyValidateAttempts; attempt++ {
@@ -929,7 +1027,7 @@ type proxyValidationResult struct {
err error err error
} }
func (s *Scheduler) validateProxyBatch(base *client.DouyuClient, endpoints []proxyprovider.Endpoint) []proxyValidationResult { func (s *Scheduler) validateProxyBatch(base *client.DouyuClient, endpoints []proxyprovider.Endpoint, deadline time.Time) []proxyValidationResult {
if len(endpoints) == 0 { if len(endpoints) == 0 {
return nil return nil
} }
@@ -945,7 +1043,12 @@ func (s *Scheduler) validateProxyBatch(base *client.DouyuClient, endpoints []pro
go func() { go func() {
defer wg.Done() defer wg.Done()
for endpoint := range jobs { for endpoint := range jobs {
proxy, err := s.validateProxy(base, endpoint.URL, "API/"+endpoint.Provider) remaining := time.Until(deadline)
if remaining <= 0 {
results <- proxyValidationResult{endpoint: endpoint, err: errors.New("代理校验总时长超时")}
continue
}
proxy, err := s.validateProxy(base, endpoint.URL, "API/"+endpoint.Provider, remaining)
results <- proxyValidationResult{endpoint: endpoint, proxy: proxy, err: err} results <- proxyValidationResult{endpoint: endpoint, proxy: proxy, err: err}
} }
}() }()
+65
View File
@@ -153,6 +153,71 @@ func TestPrepareProxyGroupsRoutesAccountsToDistinctClients(t *testing.T) {
} }
} }
func TestPrepareProxyGroupsFallsBackDirectAndSkipsRepeatedWait(t *testing.T) {
var requests int32
apiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt32(&requests, 1)
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, `{"code":-1,"status":200,"message":"secret密匙错误"}`)
}))
defer apiServer.Close()
act := *activity.Get("peace")
base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "http://127.0.0.1:1")
scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive)
scheduler.UpdateProxySettings(types.Config{
ProxyEnabled: true,
ProxyAPI: apiServer.URL,
AccountsPerProxy: 5,
ProxySparePercent: 25,
})
accountProducts := map[string]string{"a": "goods-1"}
if err := scheduler.prepareProxyGroups(accountProducts, false); err != nil {
t.Fatalf("prepareProxyGroups() error = %v", err)
}
if !scheduler.proxyFallbackSnapshot() {
t.Fatal("代理失败后应启用直连回退")
}
if scheduler.clientForAccount("a") != scheduler.directClient {
t.Fatal("代理失败后账号应路由到真正的直连客户端")
}
firstRequests := atomic.LoadInt32(&requests)
if firstRequests == 0 {
t.Fatal("首次代理准备应尝试提取")
}
if err := scheduler.prepareProxyGroups(accountProducts, false); err != nil {
t.Fatalf("second prepareProxyGroups() error = %v", err)
}
if got := atomic.LoadInt32(&requests); got != firstRequests {
t.Fatalf("直连回退后不应重复等待提取,requests=%d want %d", got, firstRequests)
}
}
func TestProxyGroupSwitchesDirectWhenStandbyUnavailable(t *testing.T) {
act := *activity.Get("peace")
base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "")
scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive)
groupClient := &client.DouyuClient{}
group := &proxyRuntimeGroup{
accountNames: []string{"a"},
activeURL: "http://user:pass@127.0.0.1:9000",
}
scheduler.replaceAccountClients(preparedProxyPool{
accountClients: map[string]*client.DouyuClient{"a": groupClient},
activeClients: []*client.DouyuClient{groupClient},
groups: map[*client.DouyuClient]*proxyRuntimeGroup{groupClient: group},
allClients: []*client.DouyuClient{groupClient},
})
proxyErr := errors.New("read tcp: connection reset by peer")
scheduler.recordProxyResult(groupClient, proxyErr)
scheduler.recordProxyResult(groupClient, proxyErr)
if scheduler.clientForAccount("a") != scheduler.directClient {
t.Fatal("备用代理为空时,故障组应立即切换到直连")
}
}
func newTimeProxyServer(t *testing.T) *httptest.Server { func newTimeProxyServer(t *testing.T) *httptest.Server {
t.Helper() t.Helper()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+35
View File
@@ -61,6 +61,7 @@ type Scheduler struct {
clientMu sync.RWMutex clientMu sync.RWMutex
proxyPrepareMu sync.Mutex proxyPrepareMu sync.Mutex
client *client.DouyuClient client *client.DouyuClient
directClient *client.DouyuClient
// accountClients 按账号名路由到独立的代理客户端;未命中的账号使用 client。 // accountClients 按账号名路由到独立的代理客户端;未命中的账号使用 client。
accountClients map[string]*client.DouyuClient accountClients map[string]*client.DouyuClient
accountClientOrder []*client.DouyuClient accountClientOrder []*client.DouyuClient
@@ -73,6 +74,7 @@ type Scheduler struct {
proxyStandbyTarget int proxyStandbyTarget int
proxyPoolGeneration uint64 proxyPoolGeneration uint64
proxyRefillInFlight bool proxyRefillInFlight bool
proxyFallbackActive bool
paths types.AppPaths paths types.AppPaths
font *truetype.Font font *truetype.Font
@@ -162,8 +164,13 @@ func New(cli *client.DouyuClient, paths types.AppPaths, font *truetype.Font, con
if attempts <= 0 { if attempts <= 0 {
attempts = constants.DefaultAttempts attempts = constants.DefaultAttempts
} }
var directClient *client.DouyuClient
if cli != nil {
directClient, _ = client.NewWithProxy(cli.Activity, cli.TimeAPIURL, cli.Trace, "")
}
scheduler := &Scheduler{ scheduler := &Scheduler{
client: cli, client: cli,
directClient: directClient,
accountClients: make(map[string]*client.DouyuClient), accountClients: make(map[string]*client.DouyuClient),
paths: paths, paths: paths,
font: font, font: font,
@@ -194,6 +201,18 @@ func New(cli *client.DouyuClient, paths types.AppPaths, font *truetype.Font, con
// Close 关闭调度器的后台资源。 // Close 关闭调度器的后台资源。
func (s *Scheduler) Close() { func (s *Scheduler) Close() {
s.closeAccountClients() s.closeAccountClients()
s.clientMu.Lock()
directClient := s.directClient
s.directClient = nil
s.clientMu.Unlock()
if directClient != nil {
if directClient.HTTPClient != nil {
directClient.HTTPClient.CloseIdleConnections()
}
if directClient.TimeHTTPClient != nil {
directClient.TimeHTTPClient.CloseIdleConnections()
}
}
if s.successImages != nil { if s.successImages != nil {
s.successImages.Close() s.successImages.Close()
} }
@@ -214,6 +233,9 @@ func (s *Scheduler) clientSnapshot() *client.DouyuClient {
func (s *Scheduler) clientForAccount(accountName string) *client.DouyuClient { func (s *Scheduler) clientForAccount(accountName string) *client.DouyuClient {
s.clientMu.RLock() s.clientMu.RLock()
defer s.clientMu.RUnlock() defer s.clientMu.RUnlock()
if s.proxyFallbackActive && s.directClient != nil {
return s.directClient
}
if cli := s.accountClients[accountName]; cli != nil { if cli := s.accountClients[accountName]; cli != nil {
return cli return cli
} }
@@ -239,18 +261,31 @@ func (s *Scheduler) UpdateClient(newClient *client.DouyuClient) bool {
} }
func (s *Scheduler) replaceClient(newClient *client.DouyuClient) { func (s *Scheduler) replaceClient(newClient *client.DouyuClient) {
var newDirect *client.DouyuClient
if newClient != nil {
newDirect, _ = client.NewWithProxy(newClient.Activity, newClient.TimeAPIURL, newClient.Trace, "")
}
s.clientMu.Lock() s.clientMu.Lock()
old := s.client old := s.client
oldDirect := s.directClient
oldGroups := s.accountClients oldGroups := s.accountClients
s.client = newClient s.client = newClient
s.directClient = newDirect
s.accountClients = make(map[string]*client.DouyuClient) s.accountClients = make(map[string]*client.DouyuClient)
s.accountClientOrder = nil s.accountClientOrder = nil
s.accountClientsDirty = true s.accountClientsDirty = true
s.accountClientsExpireAt = time.Time{} s.accountClientsExpireAt = time.Time{}
s.proxyFallbackActive = false
s.clientMu.Unlock() s.clientMu.Unlock()
if old != nil && old.HTTPClient != nil { if old != nil && old.HTTPClient != nil {
old.HTTPClient.CloseIdleConnections() old.HTTPClient.CloseIdleConnections()
} }
if oldDirect != nil && oldDirect.HTTPClient != nil {
oldDirect.HTTPClient.CloseIdleConnections()
}
if oldDirect != nil && oldDirect.TimeHTTPClient != nil {
oldDirect.TimeHTTPClient.CloseIdleConnections()
}
closeDouyuClients(oldGroups) closeDouyuClients(oldGroups)
} }
+2 -2
View File
@@ -2,7 +2,7 @@
"$schema": "https://wails.io/schemas/config.v2.json", "$schema": "https://wails.io/schemas/config.v2.json",
"name": "douyu_go", "name": "douyu_go",
"outputfilename": "douyu_go", "outputfilename": "douyu_go",
"version": "0.5.2", "version": "0.5.3",
"frontend:dir": "frontend", "frontend:dir": "frontend",
"frontend:install": "npm install", "frontend:install": "npm install",
"frontend:build": "npm run build", "frontend:build": "npm run build",
@@ -15,6 +15,6 @@
"email": "yml@example.com" "email": "yml@example.com"
}, },
"info": { "info": {
"productVersion": "0.5.2" "productVersion": "0.5.3"
} }
} }