Esempio n. 1
0
func NewIntLinearCounter(
	id string, value int64, minVal int64, maxVal int64,
	cntRate float64, inactiveTTL int64) *IntLinearCounter {

	pqm := IntLinearCounter{
		Id:               id,
		Value:            value,
		MinValue:         minVal,
		MaxValue:         maxVal,
		CountRate:        cntRate,
		LastUpdateNanoTs: common.UnixNanoTs(),
		InactiveTTL:      inactiveTTL,
		CounterType:      CNT_INT_COUNTER,
	}
	return &pqm
}
Esempio n. 2
0
func (c *IntLinearCounter) Update() {
	curTs := common.UnixNanoTs()
	tsDelta := float64(curTs-c.LastUpdateNanoTs) / 1000000000.0

	// If clocks moved back, just do nothing. Lets just remember the last time stamp.
	if tsDelta < 0 {
		c.LastUpdateNanoTs = curTs
		return
	}

	valDelta := c.CountRate * tsDelta
	if valDelta >= 1 || valDelta <= -1 {
		c.Value += int64(valDelta)
		leftover := valDelta - math.Trunc(valDelta)
		// Overflow will decrease a current TS to take into account overflow value.
		c.LastUpdateNanoTs = curTs - int64(math.Abs(tsDelta*leftover))
	}
	c.correctLimits()
}