Use time.After instead of tickers.

This commit is contained in:
jholdstock 2022-03-28 14:35:15 +01:00 committed by Jamie Holdstock
parent 9fe6eb8e52
commit c91ec5c697
3 changed files with 5 additions and 11 deletions

View File

@ -379,14 +379,12 @@ func Start(shutdownCtx context.Context, wg *sync.WaitGroup, vdb *database.VspDat
// Run voting wallet consistency check periodically. // Run voting wallet consistency check periodically.
wg.Add(1) wg.Add(1)
go func() { go func() {
ticker := time.NewTicker(consistencyInterval)
consistencyLoop: consistencyLoop:
for { for {
select { select {
case <-shutdownCtx.Done(): case <-shutdownCtx.Done():
ticker.Stop()
break consistencyLoop break consistencyLoop
case <-ticker.C: case <-time.After(consistencyInterval):
checkWalletConsistency() checkWalletConsistency()
} }
} }

View File

@ -202,19 +202,17 @@ func Open(ctx context.Context, shutdownWg *sync.WaitGroup, dbFile string, backup
return nil, fmt.Errorf("upgrade failed: %w", err) return nil, fmt.Errorf("upgrade failed: %w", err)
} }
// Start a ticker to update the backup file at the specified interval. // Periodically update the database backup file.
shutdownWg.Add(1) shutdownWg.Add(1)
go func() { go func() {
ticker := time.NewTicker(backupInterval)
for { for {
select { select {
case <-ticker.C: case <-time.After(backupInterval):
err := writeHotBackupFile(db) err := writeHotBackupFile(db)
if err != nil { if err != nil {
log.Errorf("Failed to write database backup: %v", err) log.Errorf("Failed to write database backup: %v", err)
} }
case <-ctx.Done(): case <-ctx.Done():
ticker.Stop()
shutdownWg.Done() shutdownWg.Done()
return return
} }

View File

@ -159,7 +159,7 @@ func Start(ctx context.Context, requestShutdown func(), shutdownWg *sync.WaitGro
} }
}() }()
// Use a ticker to update cached VSP stats. // Periodically update cached VSP stats.
var refresh time.Duration var refresh time.Duration
if s.cfg.Debug { if s.cfg.Debug {
refresh = 1 * time.Second refresh = 1 * time.Second
@ -168,14 +168,12 @@ func Start(ctx context.Context, requestShutdown func(), shutdownWg *sync.WaitGro
} }
shutdownWg.Add(1) shutdownWg.Add(1)
go func() { go func() {
ticker := time.NewTicker(refresh)
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
ticker.Stop()
shutdownWg.Done() shutdownWg.Done()
return return
case <-ticker.C: case <-time.After(refresh):
err := updateCache(ctx, vdb, dcrd, config.NetParams, wallets) err := updateCache(ctx, vdb, dcrd, config.NetParams, wallets)
if err != nil { if err != nil {
log.Errorf("Failed to update cached VSP stats: %v", err) log.Errorf("Failed to update cached VSP stats: %v", err)