context_sleep.go 500 B

123456789101112131415161718192021222324
  1. package aws
  2. import (
  3. "time"
  4. )
  5. // SleepWithContext will wait for the timer duration to expire, or the context
  6. // is canceled. Which ever happens first. If the context is canceled the Context's
  7. // error will be returned.
  8. //
  9. // Expects Context to always return a non-nil error if the Done channel is closed.
  10. func SleepWithContext(ctx Context, dur time.Duration) error {
  11. t := time.NewTimer(dur)
  12. defer t.Stop()
  13. select {
  14. case <-t.C:
  15. break
  16. case <-ctx.Done():
  17. return ctx.Err()
  18. }
  19. return nil
  20. }