Documentation
¶
Overview ¶
Package ratecount provides thread-safe rate calculator and few helpers
Index ¶
Examples ¶
Constants ¶
const ( KB = 1e3 MB = 1e6 GB = 1e9 TB = 1e12 KiB = 1024 MiB = 1024 * 1024 GiB = 1024 * 1024 * 1024 TiB = 1024 * 1024 * 1024 * 1024 )
common constants used with *Counter.In()
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Counter ¶
type Counter struct {
// contains filtered or unexported fields
}
Counter implements a thread-safe rate counter, See NewAvgCounter for detail.
Example ¶
c := NewAvgCounter(100*time.Millisecond, 3)
for i := 0; i < 51; i++ {
c.Incr(1)
time.Sleep(10 * time.Millisecond)
}
// should be 9 or 10
fmt.Println(c.Rate() == 9 || c.Rate() == 10)
Output: true
func NewAvgCounter ¶
NewAvgCounter creates a rate counter computes average of last n time windows.
IT RETURNS nil IF res == 0 || n == 0
It calculates how many "tokens" (in most cases, bytes) is transfered in given time window res, caches last n windows, and calculates average transfer rate. As it calculates AVERAGE OF LAST N windows, you'll get lower rate within first n windows.
For example, NewAvgCounter(time.Second, 5) calculates transfer speed (token per second) for last 5 seconds. NewAvgCounter(10*time.Second, 5) calculates transfer speed (token per 10 seconds) for last 50 seconds.
To be more specific, say you have a precise ticker, which calls Incr(1) at t = 0.5 / 1.5 / 2.5 / ... (1 tick per second), so calling Rate() at t=6 returns average speed of t=1~5, and calling Rate() at t=3 returns result from t=-2~2, or (t1+t2) / 5.
func NewCounter ¶
NewCounter is shortcut of NewAvgCounter(res, 1)
type RatedReader ¶
type RatedReader struct {
// contains filtered or unexported fields
}
RatedReader computes transfer rate when you read from it
type RatedWriter ¶
type RatedWriter struct {
// contains filtered or unexported fields
}
RatedWriter computes transfer rate when you write to it