From 25e70770bc924546d910fe951761764aab692744 Mon Sep 17 00:00:00 2001 From: Alexei Ledenev Date: Thu, 4 Apr 2024 12:44:27 +0300 Subject: [PATCH] Refactor loop interval to use loopTicker for non-blocking delay in main.go --- cmd/main.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cmd/main.go b/cmd/main.go index 56bc246..8e906d6 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -28,6 +28,7 @@ const ( unassignTimeout = 5 * time.Minute kubeipLockName = "kubeip-lock" defaultLeaseDuration = 5 + loopInterval = 250 * time.Millisecond ) var ( @@ -177,6 +178,10 @@ func run(c context.Context, log *logrus.Entry, cfg *config.Config) error { } }() + // Create a ticker for non-blocking delay + loopTicker := time.NewTicker(loopInterval) + defer loopTicker.Stop() + for { select { case assignErr, ok := <-errorCh: @@ -202,6 +207,9 @@ func run(c context.Context, log *logrus.Entry, cfg *config.Config) error { log.Infof("static public IP address released") } return nil + case <-loopTicker.C: + // Wait for the next tick before continuing the loop + // This case prevents high CPU usage by introducing a non-blocking delay } } }