From d05455ab05a4a4b87495908fb60e5561b5595204 Mon Sep 17 00:00:00 2001 From: carlory Date: Mon, 20 Apr 2026 18:28:05 +0800 Subject: [PATCH] controller/resourcepoolstatusrequest: Improve goroutine mgmt Signed-off-by: carlory --- .../resourcepoolstatusrequest/controller.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/pkg/controller/resourcepoolstatusrequest/controller.go b/pkg/controller/resourcepoolstatusrequest/controller.go index 752e991ccf2..138fd81ea7e 100644 --- a/pkg/controller/resourcepoolstatusrequest/controller.go +++ b/pkg/controller/resourcepoolstatusrequest/controller.go @@ -20,6 +20,7 @@ import ( "context" "fmt" "sort" + "sync" "time" resourcev1alpha3 "k8s.io/api/resource/v1alpha3" @@ -128,11 +129,17 @@ func NewController( // Run starts the controller workers. func (c *Controller) Run(ctx context.Context, workers int) { defer utilruntime.HandleCrash() - defer c.workqueue.ShutDown() logger := klog.FromContext(ctx) logger.Info("Starting ResourcePoolStatusRequest controller") + var wg sync.WaitGroup + defer func() { + logger.Info("Shutting down ResourcePoolStatusRequest controller") + c.workqueue.ShutDown() + wg.Wait() + }() + // Wait for the caches to be synced before starting workers logger.Info("Waiting for informer caches to sync") if !cache.WaitForCacheSync(ctx.Done(), c.requestSynced, c.sliceSynced, c.claimSynced) { @@ -142,14 +149,17 @@ func (c *Controller) Run(ctx context.Context, workers int) { logger.Info("Starting workers", "count", workers) for range workers { - go wait.UntilWithContext(ctx, c.runWorker, time.Second) + wg.Go(func() { + wait.UntilWithContext(ctx, c.runWorker, time.Second) + }) } // Start the cleanup goroutine for TTL-based garbage collection - go wait.UntilWithContext(ctx, c.cleanupExpiredRequests, cleanupPollingInterval) + wg.Go(func() { + wait.UntilWithContext(ctx, c.cleanupExpiredRequests, cleanupPollingInterval) + }) <-ctx.Done() - logger.Info("Shutting down ResourcePoolStatusRequest controller") } // runWorker is a long-running function that will continually call the