Skip to content

Commit 9be2928

Browse files
authored
refactor: replace time.After with time.Ticker for periodic tasks in healthCheck and startPeriodicTasks
1 parent c307a2f commit 9be2928

File tree

2 files changed

+79
-80
lines changed

2 files changed

+79
-80
lines changed

internal/common.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,9 @@ func (c *Common) commonQueue() error {
675675

676676
// healthCheck 共用健康度检查
677677
func (c *Common) healthCheck() error {
678+
ticker := time.NewTicker(reportInterval)
679+
defer ticker.Stop()
680+
678681
for {
679682
if c.ctx.Err() != nil {
680683
return fmt.Errorf("healthCheck: context error: %w", c.ctx.Err())
@@ -716,7 +719,7 @@ func (c *Common) healthCheck() error {
716719
select {
717720
case <-c.ctx.Done():
718721
return fmt.Errorf("healthCheck: context error: %w", c.ctx.Err())
719-
case <-time.After(reportInterval):
722+
case <-ticker.C:
720723
}
721724

722725
c.logger.Debug("Tunnel pool flushed: %v active connections", c.tunnelPool.Active())
@@ -736,7 +739,7 @@ func (c *Common) healthCheck() error {
736739
select {
737740
case <-c.ctx.Done():
738741
return fmt.Errorf("healthCheck: context error: %w", c.ctx.Err())
739-
case <-time.After(reportInterval):
742+
case <-ticker.C:
740743
}
741744
}
742745
}
@@ -1339,6 +1342,9 @@ func (c *Common) singleControl() error {
13391342

13401343
// singleEventLoop 单端转发事件循环
13411344
func (c *Common) singleEventLoop() error {
1345+
ticker := time.NewTicker(reportInterval)
1346+
defer ticker.Stop()
1347+
13421348
for {
13431349
if c.ctx.Err() != nil {
13441350
return fmt.Errorf("singleEventLoop: context error: %w", c.ctx.Err())
@@ -1363,7 +1369,7 @@ func (c *Common) singleEventLoop() error {
13631369
select {
13641370
case <-c.ctx.Done():
13651371
return fmt.Errorf("singleEventLoop: context error: %w", c.ctx.Err())
1366-
case <-time.After(reportInterval):
1372+
case <-ticker.C:
13671373
}
13681374
}
13691375
}

internal/master.go

Lines changed: 70 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -569,102 +569,95 @@ func (m *Master) Shutdown(ctx context.Context) error {
569569

570570
// startPeriodicTasks 启动所有定期任务
571571
func (m *Master) startPeriodicTasks() {
572-
go m.startPeriodicBackup()
573-
go m.startPeriodicCleanup()
574-
go m.startPeriodicRestart()
575-
}
572+
ticker := time.NewTicker(ReloadInterval)
573+
defer ticker.Stop()
576574

577-
// startPeriodicBackup 启动定期备份
578-
func (m *Master) startPeriodicBackup() {
579575
for {
580576
select {
581-
case <-time.After(ReloadInterval):
582-
// 固定备份文件名
583-
backupPath := fmt.Sprintf("%s.backup", m.statePath)
584-
585-
if err := m.saveStateToPath(backupPath); err != nil {
586-
m.logger.Error("startPeriodicBackup: backup state failed: %v", err)
587-
} else {
588-
m.logger.Info("State backup saved: %v", backupPath)
589-
}
577+
case <-ticker.C:
578+
// 执行定期备份
579+
m.performPeriodicBackup()
580+
// 执行定期清理
581+
m.performPeriodicCleanup()
582+
// 执行定期重启
583+
m.performPeriodicRestart()
590584
case <-m.periodicDone:
585+
ticker.Stop()
591586
return
592587
}
593588
}
594589
}
595590

596-
// startPeriodicCleanup 启动定期清理重复ID的实例
597-
func (m *Master) startPeriodicCleanup() {
598-
for {
599-
select {
600-
case <-time.After(ReloadInterval):
601-
// 收集实例并按ID分组
602-
idInstances := make(map[string][]*Instance)
603-
m.instances.Range(func(key, value any) bool {
604-
if id := key.(string); id != apiKeyID {
605-
idInstances[id] = append(idInstances[id], value.(*Instance))
606-
}
607-
return true
608-
})
591+
// performPeriodicBackup 定期备份任务
592+
func (m *Master) performPeriodicBackup() {
593+
// 固定备份文件名
594+
backupPath := fmt.Sprintf("%s.backup", m.statePath)
609595

610-
// 清理重复实例
611-
for _, instances := range idInstances {
612-
if len(instances) <= 1 {
613-
continue
614-
}
596+
if err := m.saveStateToPath(backupPath); err != nil {
597+
m.logger.Error("performPeriodicBackup: backup state failed: %v", err)
598+
} else {
599+
m.logger.Info("State backup saved: %v", backupPath)
600+
}
601+
}
615602

616-
// 选择保留实例
617-
keepIdx := 0
618-
for i, inst := range instances {
619-
if inst.Status == "running" && instances[keepIdx].Status != "running" {
620-
keepIdx = i
621-
}
622-
}
603+
// performPeriodicCleanup 定期清理重复ID的实例
604+
func (m *Master) performPeriodicCleanup() {
605+
// 收集实例并按ID分组
606+
idInstances := make(map[string][]*Instance)
607+
m.instances.Range(func(key, value any) bool {
608+
if id := key.(string); id != apiKeyID {
609+
idInstances[id] = append(idInstances[id], value.(*Instance))
610+
}
611+
return true
612+
})
623613

624-
// 清理多余实例
625-
for i, inst := range instances {
626-
if i == keepIdx {
627-
continue
628-
}
629-
inst.deleted = true
630-
if inst.Status != "stopped" {
631-
m.stopInstance(inst)
632-
}
633-
m.instances.Delete(inst.ID)
634-
}
614+
// 清理重复实例
615+
for _, instances := range idInstances {
616+
if len(instances) <= 1 {
617+
continue
618+
}
619+
620+
// 选择保留实例
621+
keepIdx := 0
622+
for i, inst := range instances {
623+
if inst.Status == "running" && instances[keepIdx].Status != "running" {
624+
keepIdx = i
635625
}
636-
case <-m.periodicDone:
637-
return
626+
}
627+
628+
// 清理多余实例
629+
for i, inst := range instances {
630+
if i == keepIdx {
631+
continue
632+
}
633+
inst.deleted = true
634+
if inst.Status != "stopped" {
635+
m.stopInstance(inst)
636+
}
637+
m.instances.Delete(inst.ID)
638638
}
639639
}
640640
}
641641

642-
// startPeriodicRestart 启动定期错误实例重启
643-
func (m *Master) startPeriodicRestart() {
644-
for {
645-
select {
646-
case <-time.After(ReloadInterval):
647-
// 收集所有error状态的实例
648-
var errorInstances []*Instance
649-
m.instances.Range(func(key, value any) bool {
650-
if id := key.(string); id != apiKeyID {
651-
instance := value.(*Instance)
652-
if instance.Status == "error" && !instance.deleted {
653-
errorInstances = append(errorInstances, instance)
654-
}
655-
}
656-
return true
657-
})
658-
659-
// 重启所有error状态的实例
660-
for _, instance := range errorInstances {
661-
m.stopInstance(instance)
662-
time.Sleep(baseDuration)
663-
m.startInstance(instance)
642+
// performPeriodicRestart 定期错误实例重启
643+
func (m *Master) performPeriodicRestart() {
644+
// 收集所有error状态的实例
645+
var errorInstances []*Instance
646+
m.instances.Range(func(key, value any) bool {
647+
if id := key.(string); id != apiKeyID {
648+
instance := value.(*Instance)
649+
if instance.Status == "error" && !instance.deleted {
650+
errorInstances = append(errorInstances, instance)
664651
}
665-
case <-m.periodicDone:
666-
return
667652
}
653+
return true
654+
})
655+
656+
// 重启所有error状态的实例
657+
for _, instance := range errorInstances {
658+
m.stopInstance(instance)
659+
time.Sleep(baseDuration)
660+
m.startInstance(instance)
668661
}
669662
}
670663

0 commit comments

Comments
 (0)