Documentation ¶
Overview ¶
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).
// We're recording marks-per-1second counter := ratecounter.NewRateCounter(1 * time.Second) // Record an event happening counter.Mark() // get the current requests-per-second counter.Rate()
To record an average over a longer period, you can:
// Record requests-per-minute counter := ratecounter.NewRateCounter(60 * time.Second) // Calculate the average requests-per-second for the last minute counter.Rate() / 60
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AvgRateCounter ¶ added in v0.2.0
type AvgRateCounter struct {
// contains filtered or unexported fields
}
An AvgRateCounter is a thread-safe counter which returns the ratio between the number of calls 'Incr' and the counter value in the last interval
func NewAvgRateCounter ¶ added in v0.2.0
func NewAvgRateCounter(intrvl time.Duration) *AvgRateCounter
NewAvgRateCounter constructs a new AvgRateCounter, for the interval provided
func (*AvgRateCounter) Hits ¶ added in v0.2.0
func (a *AvgRateCounter) Hits() int64
Hits returns the number of calling method Incr during specified interval
func (*AvgRateCounter) Incr ¶ added in v0.2.0
func (a *AvgRateCounter) Incr(val int64)
Incr Adds an event into the AvgRateCounter
func (*AvgRateCounter) Rate ¶ added in v0.2.0
func (a *AvgRateCounter) Rate() float64
Rate Returns the current ratio between the events count and its values during the last interval
func (*AvgRateCounter) String ¶ added in v0.2.0
func (a *AvgRateCounter) String() string
String returns counter's rate formatted to string
func (*AvgRateCounter) WithResolution ¶ added in v0.2.0
func (a *AvgRateCounter) WithResolution(resolution int) *AvgRateCounter
WithResolution determines the minimum resolution of this counter
type Counter ¶
type Counter int64
A Counter is a thread-safe counter implementation
type RateCounter ¶
type RateCounter struct {
// contains filtered or unexported fields
}
A RateCounter is a thread-safe counter which returns the number of times 'Incr' has been called in the last interval
func NewRateCounter ¶
func NewRateCounter(intrvl time.Duration) *RateCounter
NewRateCounter Constructs a new RateCounter, for the interval provided
func (*RateCounter) Incr ¶
func (r *RateCounter) Incr(val int64)
Incr Add an event into the RateCounter
func (*RateCounter) Rate ¶
func (r *RateCounter) Rate() int64
Rate Return the current number of events in the last interval
func (*RateCounter) String ¶
func (r *RateCounter) String() string
func (*RateCounter) WithResolution ¶ added in v0.2.0
func (r *RateCounter) WithResolution(resolution int) *RateCounter
WithResolution determines the minimum resolution of this counter, default is 20