diff options
Diffstat (limited to 'internal/ctxutil/sleep.go')
| -rw-r--r-- | internal/ctxutil/sleep.go | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/internal/ctxutil/sleep.go b/internal/ctxutil/sleep.go new file mode 100644 index 0000000..6965e8d --- /dev/null +++ b/internal/ctxutil/sleep.go @@ -0,0 +1,29 @@ +package ctxutil + +import ( + "context" + "time" +) + +// Sleep waits for the delay or exits early when the context is canceled. +// It returns true when the full delay elapsed. +func Sleep(ctx context.Context, delay time.Duration) bool { + if delay <= 0 { + select { + case <-ctx.Done(): + return false + default: + return true + } + } + + timer := time.NewTimer(delay) + defer timer.Stop() + + select { + case <-ctx.Done(): + return false + case <-timer.C: + return true + } +} |
