fix(GODT-2327): Better sleep (with context)

This commit is contained in:
James Houlahan
2023-02-07 12:29:04 +01:00
committed by Jakub
parent 20d83dd476
commit 48f2c56caa
3 changed files with 14 additions and 7 deletions

View File

@ -185,12 +185,12 @@ func New(
// When triggered, sync the user and then begin streaming API events.
user.goSync = user.tasks.Trigger(func(ctx context.Context) {
user.log.Debug("Sync triggered")
user.log.Info("Sync triggered")
// Sync the user.
user.syncAbort.Do(ctx, func(ctx context.Context) {
if user.vault.SyncStatus().IsComplete() {
user.log.Debug("Sync already complete, skipping")
user.log.Info("Sync already complete, skipping")
return
}
@ -200,7 +200,7 @@ func New(
return
} else if err := user.doSync(ctx); err != nil {
user.log.WithError(err).Error("Failed to sync, will retry later")
time.Sleep(SyncRetryCooldown)
sleepCtx(ctx, SyncRetryCooldown)
} else {
user.log.Info("Sync complete, starting API event stream")
return
@ -685,3 +685,11 @@ func b32(b bool) uint32 {
return 0
}
// sleepCtx sleeps for the given duration, or until the context is canceled.
func sleepCtx(ctx context.Context, d time.Duration) {
select {
case <-ctx.Done():
case <-time.After(d):
}
}