ratecounter
A Thread-Safe RateCounter implementation in Golang
Usage
import "github.com/paulbellamy/ratecounter"
Package ratecounter provides a thread-safe rate-counter, for tracking
counts in an interval
Useful for implementing counters and stats of 'requests-per-second' (for
example):
counter := ratecounter.NewRateCounter(1 * time.Second)
counter.Incr(1)
counter.Rate()
To record an average over a longer period, you can:
counter := ratecounter.NewRateCounter(60 * time.Second)
counter.Rate() / 60
Also you can track average value of some metric in an interval.
Useful for implementing counters and stats of 'average-execution-time' (for
example):
counter := ratecounter.NewAvgRateCounter(60 * time.Second)
startTime := time.Now()
heavyOperation()
counter.Incr(time.Since(startTime).Nanoseconds())
counter.Rate()
Documentation
Check latest documentation on go doc.