// averageLoop calculates averages for the stats in the background
averageLoop calculates averages for the stats in the background
// averageLoop calculates averages for the stats in the background
func (acc *Account) averageLoop() {
	tick := time.NewTicker(time.Second)
	var period float64
	defer tick.Stop()
	for {
		select {
		case now := <-tick.C:
			acc.statmu.Lock()
			elapsed := now.Sub(acc.lpTime).Seconds()
			avg := float64(acc.lpBytes) / elapsed
			if period < averagePeriod {
				period++
			}
			acc.avg = (avg + (period-1)*acc.avg) / period
			acc.lpBytes = 0
			acc.lpTime = now
			acc.statmu.Unlock()
		case <-acc.exit:
			return
		}
	}
}
func ( acc * Account ) averageLoop ( ) { tick := time . NewTicker ( time . Second ) var period float64 defer tick . Stop ( ) for { select { case now := <- tick . C : acc . statmu . Lock ( ) elapsed := now . Sub ( acc . lpTime ) . Seconds ( ) avg := float64 ( acc . lpBytes ) / elapsed if period < averagePeriod { period ++ } acc . avg = ( avg + ( period - 1 ) * acc . avg ) / period acc . lpBytes = 0 acc . lpTime = now acc . statmu . Unlock ( ) case <- acc . exit : return } } }
averageLoop calculates averages for the stats in the background
averageLoop calculates averages for the stats in the background
// Add average of last second. // Soft start the moving average // Unlock stats