Skip to content

Commit

Permalink
refactor: periodic operations to use time.Ticker (#978)
Browse files Browse the repository at this point in the history
- Use `time.Ticker` instead of `time.After` for periodic operations in `autoSave` function
- Use `time.Ticker` instead of `time.After` for periodic operations in `monitor` function

Signed-off-by: appleboy <[email protected]>
  • Loading branch information
appleboy authored Jan 29, 2025
1 parent 975ac35 commit 5b3e0c8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion storage/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ func Initialize(cfg *storage.Config) error {

// autoSave automatically calls the Save function of the provider at every interval
func autoSave(ctx context.Context, store Store, interval time.Duration) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
logr.Info("[store.autoSave] Stopping active job")
return
case <-time.After(interval):
case <-ticker.C:
logr.Info("[store.autoSave] Saving")
if err := store.Save(); err != nil {
logr.Errorf("[store.autoSave] Save failed: %s", err.Error())
Expand Down
4 changes: 3 additions & 1 deletion watchdog/watchdog.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ func monitor(ep *endpoint.Endpoint, alertingConfig *alerting.Config, maintenance
// Run it immediately on start
execute(ep, alertingConfig, maintenanceConfig, connectivityConfig, disableMonitoringLock, enabledMetrics)
// Loop for the next executions
ticker := time.NewTicker(ep.Interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
logr.Warnf("[watchdog.monitor] Canceling current execution of group=%s; endpoint=%s; key=%s", ep.Group, ep.Name, ep.Key())
return
case <-time.After(ep.Interval):
case <-ticker.C:
execute(ep, alertingConfig, maintenanceConfig, connectivityConfig, disableMonitoringLock, enabledMetrics)
}
}
Expand Down

0 comments on commit 5b3e0c8

Please sign in to comment.