refactor: periodic operations to use time.Ticker (#978)

- 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 <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu
2025-01-29 11:23:16 +08:00
committed by GitHub
parent 975ac3592e
commit 5b3e0c8074
2 changed files with 6 additions and 2 deletions

View File

@ -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())