github.com/chaowen112/go-lib@v0.0.0-20231018124935-124cd26d7cbe/timeutils/timeutils.go (about)

     1  package timeutils
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/chaowen112/go-lib/randutils"
     7  )
     8  
     9  func UnixMilli() float64 {
    10  	return float64(time.Now().UnixNano()/1000) / 1000000
    11  }
    12  
    13  // RandomDuration returns a random duration between [a, b) for distributing API pressures
    14  //
    15  //	for {
    16  //	    time.Sleep(timeutils.RandomDuration(5*time.Minute, 10*time.Minute)
    17  //	    client.Do(arg1, arg2, arg3)
    18  //	}
    19  //
    20  // above mechanism can avoid DDoS to the API server.
    21  func RandomDuration(a, b time.Duration) time.Duration {
    22  	return a + time.Duration(randutils.Uint32n(uint32((b-a)/time.Millisecond)))*time.Millisecond
    23  }