优化一些功能
This commit is contained in:
@@ -71,6 +71,8 @@ type Scheduler struct {
|
||||
|
||||
runMu sync.Mutex
|
||||
running bool
|
||||
// pendingClient 保存运行中收到的客户端更新,当前任务结束后自动应用。
|
||||
pendingClient *client.DouyuClient
|
||||
|
||||
scheduleMu sync.Mutex
|
||||
schedule *time.Timer
|
||||
@@ -133,15 +135,24 @@ func (s *Scheduler) clientSnapshot() *client.DouyuClient {
|
||||
}
|
||||
|
||||
// UpdateClient 热替换底层 HTTP 客户端(用于活动切换)。
|
||||
// 若有抢购任务正在运行则拒绝替换,返回 false。
|
||||
// 若有抢购任务正在运行则暂存新客户端,当前任务结束后自动应用,并返回 false。
|
||||
func (s *Scheduler) UpdateClient(newClient *client.DouyuClient) bool {
|
||||
s.runMu.Lock()
|
||||
if s.running {
|
||||
if s.pendingClient != nil && s.pendingClient.HTTPClient != nil {
|
||||
s.pendingClient.HTTPClient.CloseIdleConnections()
|
||||
}
|
||||
s.pendingClient = newClient
|
||||
s.runMu.Unlock()
|
||||
return false
|
||||
}
|
||||
s.runMu.Unlock()
|
||||
|
||||
s.replaceClient(newClient)
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *Scheduler) replaceClient(newClient *client.DouyuClient) {
|
||||
s.clientMu.Lock()
|
||||
defer s.clientMu.Unlock()
|
||||
old := s.client
|
||||
@@ -149,7 +160,6 @@ func (s *Scheduler) UpdateClient(newClient *client.DouyuClient) bool {
|
||||
if old != nil && old.HTTPClient != nil {
|
||||
old.HTTPClient.CloseIdleConnections()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// UpdateProducts 更新商品缓存,抢购成功图片会优先使用中文商品名。
|
||||
@@ -289,8 +299,15 @@ func (s *Scheduler) TryStartRun() bool {
|
||||
|
||||
func (s *Scheduler) finishRun() {
|
||||
s.runMu.Lock()
|
||||
defer s.runMu.Unlock()
|
||||
s.running = false
|
||||
pendingClient := s.pendingClient
|
||||
s.pendingClient = nil
|
||||
s.runMu.Unlock()
|
||||
|
||||
if pendingClient != nil {
|
||||
s.replaceClient(pendingClient)
|
||||
logger.LogEvent("配置", "success", "抢购任务结束,已应用等待中的客户端配置")
|
||||
}
|
||||
}
|
||||
|
||||
// RunNow 立即执行抢购任务,持续循环直到超时或全部成功。
|
||||
|
||||
Reference in New Issue
Block a user