Skip to content

Commit

Permalink
Refactor loopTicker to use time.Sleep instead to prevent high CPU usage
Browse files Browse the repository at this point in the history
  • Loading branch information
alexei-led committed Apr 4, 2024
1 parent 2bc8c1b commit 2ec20ce
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const (
unassignTimeout = 5 * time.Minute
kubeipLockName = "kubeip-lock"
defaultLeaseDuration = 5
loopInterval = 250 * time.Millisecond
pauseInterval = 3 * time.Second
)

var (
Expand Down Expand Up @@ -179,10 +179,6 @@ 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:
Expand All @@ -208,9 +204,8 @@ 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
default:
time.Sleep(pauseInterval)
}
}
}
Expand Down

0 comments on commit 2ec20ce

Please sign in to comment.