github.com/unionj-cloud/go-doudou@v1.3.8-0.20221011095552-0088008e5b31/toolkit/internal/common/sleep.go (about)

     1  package common
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  )
     7  
     8  // Sleep awaits for provided interval.
     9  // Can be interrupted by context cancelation.
    10  func Sleep(ctx context.Context, interval time.Duration) error {
    11  	timer := time.NewTimer(interval)
    12  	select {
    13  	case <-ctx.Done():
    14  		return ctx.Err()
    15  	case <-timer.C:
    16  		return nil
    17  	}
    18  }