github.com/gofiber/fiber/v2@v2.47.0/internal/gopsutil/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  	var timer = time.NewTimer(interval)
    12  	select {
    13  	case <-ctx.Done():
    14  		if !timer.Stop() {
    15  			<-timer.C
    16  		}
    17  		return ctx.Err()
    18  	case <-timer.C:
    19  		return nil
    20  	}
    21  }