shirou_gopsutil/internal/common/sleep.go

19 lines
314 B
Go
Raw Normal View History

package common
import (
"context"
"time"
)
// Sleep awaits for provided interval.
// Can be interrupted by context cancelation.
func Sleep(ctx context.Context, interval time.Duration) error {
2021-12-23 05:54:41 +08:00
timer := time.NewTimer(interval)
select {
case <-ctx.Done():
return ctx.Err()
case <-timer.C:
return nil
}
}