1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-24 13:48:56 +08:00

23 lines
394 B
Go
Raw Normal View History

// SPDX-License-Identifier: BSD-3-Clause
package common
import (
"context"
"time"
)
// Sleep awaits for provided interval.
// Can be interrupted by context cancellation.
func Sleep(ctx context.Context, interval time.Duration) error {
2021-12-22 21:54:41 +00:00
timer := time.NewTimer(interval)
select {
case <-ctx.Done():
2023-05-28 12:32:10 +00:00
if !timer.Stop() {
<-timer.C
}
return ctx.Err()
case <-timer.C:
return nil
}
}