controller/ttl: Improve goroutine mgmt

Make sure all threads are terminated when Run returns.
This commit is contained in:
Ondra Kupka
2025-10-27 14:37:48 +01:00
parent 3ee8c53e53
commit 7240649e4f

View File

@@ -122,19 +122,26 @@ var (
// Run begins watching and syncing.
func (ttlc *Controller) Run(ctx context.Context, workers int) {
defer utilruntime.HandleCrash()
defer ttlc.queue.ShutDown()
logger := klog.FromContext(ctx)
logger.Info("Starting TTL controller")
defer logger.Info("Shutting down TTL controller")
var wg sync.WaitGroup
defer func() {
logger.Info("Shutting down TTL controller")
ttlc.queue.ShutDown()
wg.Wait()
}()
if !cache.WaitForNamedCacheSyncWithContext(ctx, ttlc.hasSynced) {
return
}
for i := 0; i < workers; i++ {
go wait.UntilWithContext(ctx, ttlc.worker, time.Second)
wg.Go(func() {
wait.UntilWithContext(ctx, ttlc.worker, time.Second)
})
}
<-ctx.Done()
}