mirror of
https://github.com/hibiken/asynq.git
synced 2025-07-07 05:43:40 +08:00
20 lines
302 B
Go
20 lines
302 B
Go
package timeutil
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
// Sleep will wait for the specified duration or return on context
|
|
// expiration.
|
|
func Sleep(ctx context.Context, d time.Duration) error {
|
|
t := time.NewTimer(d)
|
|
select {
|
|
case <-ctx.Done():
|
|
t.Stop()
|
|
return ctx.Err()
|
|
case <-t.C:
|
|
return nil
|
|
}
|
|
}
|