Skip to content

Commit 7904c59

Browse files
authored
fix: replace time.Sleep with context-aware select for health check and event loop intervals
1 parent 48fd7e1 commit 7904c59

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

internal/common.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,13 @@ func (c *Common) healthCheck() error {
468468
}
469469
c.tunnelPool.Flush()
470470
c.tunnelPool.ResetError()
471-
time.Sleep(reportInterval) // 等待连接池刷新完成
471+
472+
select {
473+
case <-c.ctx.Done():
474+
return c.ctx.Err()
475+
case <-time.After(reportInterval):
476+
}
477+
472478
c.logger.Debug("Tunnel pool reset: %v active connections", c.tunnelPool.Active())
473479
}
474480

@@ -481,7 +487,11 @@ func (c *Common) healthCheck() error {
481487
}
482488

483489
c.mu.Unlock()
484-
time.Sleep(reportInterval)
490+
select {
491+
case <-c.ctx.Done():
492+
return c.ctx.Err()
493+
case <-time.After(reportInterval):
494+
}
485495
}
486496
}
487497
}
@@ -743,7 +753,13 @@ func (c *Common) commonOnce() error {
743753
go func() {
744754
c.tunnelPool.Flush()
745755
c.tunnelPool.ResetError()
746-
time.Sleep(reportInterval) // 等待连接池刷新完成
756+
757+
select {
758+
case <-c.ctx.Done():
759+
return
760+
case <-time.After(reportInterval):
761+
}
762+
747763
c.logger.Debug("Tunnel pool reset: %v active connections", c.tunnelPool.Active())
748764
}()
749765
case "1": // TCP
@@ -996,7 +1012,11 @@ func (c *Common) singleEventLoop() error {
9961012
atomic.LoadUint64(&c.UDPRX), atomic.LoadUint64(&c.UDPTX))
9971013

9981014
// 等待下一个报告间隔
999-
time.Sleep(reportInterval)
1015+
select {
1016+
case <-c.ctx.Done():
1017+
return c.ctx.Err()
1018+
case <-time.After(reportInterval):
1019+
}
10001020
}
10011021
}
10021022
}

0 commit comments

Comments
 (0)