fix(lifecycle): TokenRefreshService Stop() 防重复 close

使用 sync.Once 包裹 close(stopCh),避免多次调用 Stop() 时
触发 panic: close of closed channel。
This commit is contained in:
QTom
2026-03-30 10:32:59 +08:00
parent b65275235f
commit 61607990c8

View File

@@ -32,8 +32,9 @@ type TokenRefreshService struct {
privacyClientFactory PrivacyClientFactory
proxyRepo ProxyRepository
stopCh chan struct{}
wg sync.WaitGroup
stopCh chan struct{}
stopOnce sync.Once
wg sync.WaitGroup
}
// NewTokenRefreshService 创建token刷新服务
@@ -130,7 +131,9 @@ func (s *TokenRefreshService) Start() {
// Stop 停止刷新服务(可安全多次调用)
func (s *TokenRefreshService) Stop() {
close(s.stopCh)
s.stopOnce.Do(func() {
close(s.stopCh)
})
s.wg.Wait()
slog.Info("token_refresh.service_stopped")
}