mirror of
https://github.com/certimate-go/certimate.git
synced 2026-06-30 21:05:12 +08:00
38 lines
617 B
Go
38 lines
617 B
Go
package wait
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
// 等待一段时间。
|
|
//
|
|
// 入参:
|
|
// - wait: 等待时间。
|
|
//
|
|
// 出参:
|
|
// - err: 错误。
|
|
func Delay(wait time.Duration) error {
|
|
return DelayWithContext(context.Background(), wait)
|
|
}
|
|
|
|
// 等待一段时间,或上下文被取消。
|
|
//
|
|
// 入参:
|
|
// - ctx: 上下文。
|
|
// - wait: 等待时间。
|
|
//
|
|
// 出参:
|
|
// - err: 错误。
|
|
func DelayWithContext(ctx context.Context, wait time.Duration) error {
|
|
ticker := time.NewTimer(wait)
|
|
defer ticker.Stop()
|
|
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case <-ticker.C:
|
|
return nil
|
|
}
|
|
}
|