github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/timer/util.go (about)

     1  /* For license and copyright information please see the LEGAL file in the code repository */
     2  
     3  package timer
     4  
     5  import (
     6  	"github.com/GeniusesGroup/libgo/protocol"
     7  	"github.com/GeniusesGroup/libgo/time/monotonic"
     8  )
     9  
    10  // when is a helper function for setting the 'when' field of a Timer.
    11  // It returns what the time will be, in nanoseconds, Duration d in the future.
    12  // If d is negative, it is ignored. If the returned value would be less than
    13  // zero because of an overflow, MaxInt64 is returned.
    14  func when(d protocol.Duration) (t monotonic.Time) {
    15  	t.Now()
    16  	if d <= 0 {
    17  		return
    18  	}
    19  	t.Add(d)
    20  	// check for overflow.
    21  	if t < 0 {
    22  		// monotonic.Now() and d are always positive, so addition
    23  		// (including overflow) will never result in t == 0.
    24  		t = maxWhen
    25  	}
    26  	return
    27  }
    28  
    29  // badTimer is called if the timer data structures have been corrupted,
    30  // presumably due to racy use by the program. We panic here rather than
    31  // panicing due to invalid slice access while holding locks.
    32  // See issue #25686.
    33  func badTimer() {
    34  	panic("timer: data corruption")
    35  }