bitbucket.org/ai69/amoy@v0.2.3/sleep.go (about)

     1  package amoy
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  )
     7  
     8  // SleepForMilliseconds pauses the current goroutine for at least the n milliseconds.
     9  func SleepForMilliseconds(n float64) {
    10  	time.Sleep(Milliseconds(n))
    11  }
    12  
    13  // SleepForSeconds pauses the current goroutine for at least the n seconds.
    14  func SleepForSeconds(n float64) {
    15  	time.Sleep(Seconds(n))
    16  }
    17  
    18  // SleepForMinutes pauses the current goroutine for at least the n minutes.
    19  func SleepForMinutes(n float64) {
    20  	time.Sleep(Minutes(n))
    21  }
    22  
    23  // SleepForHours pauses the current goroutine for at least the n hours.
    24  func SleepForHours(n float64) {
    25  	time.Sleep(Hours(n))
    26  }
    27  
    28  // SleepForDays pauses the current goroutine for at least the n days.
    29  func SleepForDays(n float64) {
    30  	time.Sleep(Days(n))
    31  }
    32  
    33  // SleepWithContext pauses the current goroutine for the duration d or shorter duration if the context is cancelled.
    34  // A negative or zero duration causes SleepWithContext to return immediately.
    35  func SleepWithContext(ctx context.Context, d time.Duration) error {
    36  	if d <= 0 {
    37  		return nil
    38  	}
    39  	t := time.NewTimer(d)
    40  	defer t.Stop()
    41  	select {
    42  	case <-t.C:
    43  		return nil
    44  	case <-ctx.Done():
    45  		return ctx.Err()
    46  	}
    47  }