github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/timingwheel/utils.go (about)

     1  package timingwheel
     2  
     3  import (
     4  	"sync"
     5  	"time"
     6  )
     7  
     8  // truncate returns the result of rounding x toward zero to a multiple of m.
     9  // If m <= 0, Truncate returns x unchanged.
    10  func truncate(x, m int64) int64 {
    11  	if m <= 0 {
    12  		return x
    13  	}
    14  	return x - x%m
    15  }
    16  
    17  // timeToMs returns an integer number, which represents t in milliseconds.
    18  func timeToMs(t time.Time) int64 {
    19  	return t.UnixNano() / int64(time.Millisecond)
    20  }
    21  
    22  // msToTime returns the UTC time corresponding to the given Unix time,
    23  // t milliseconds since January 1, 1970 UTC.
    24  func msToTime(t int64) time.Time {
    25  	return time.Unix(0, t*int64(time.Millisecond)).UTC()
    26  }
    27  
    28  type waitGroupWrapper struct {
    29  	sync.WaitGroup
    30  }
    31  
    32  func (w *waitGroupWrapper) Wrap(cb func()) {
    33  	w.Add(1)
    34  	go func() {
    35  		cb()
    36  		w.Done()
    37  	}()
    38  }