From 801f69da381278b76ad52d7edc388da5a5cf54ac Mon Sep 17 00:00:00 2001 From: Alexei Ledenev Date: Thu, 4 Apr 2024 11:38:36 +0300 Subject: [PATCH] Updated main.go to introduce a non-blocking delay with loopTicker for better CPU usage efficiency. --- cmd/main.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cmd/main.go b/cmd/main.go index 98e4b96..10a5a31 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -28,6 +28,7 @@ const ( unassignTimeout = 5 * time.Minute kubeipLockName = "kubeip-lock" defaultLeaseDuration = 5 + loopInterval = 100 * 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 err = <-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 } } }