-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_counter.go
55 lines (47 loc) · 1.04 KB
/
simple_counter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package metric
import (
"time"
)
func newSimpleCounter(windowDur, bucketDur time.Duration) *simpleCounter {
return &simpleCounter{
bucketDur: int64(bucketDur),
buckets: make([]buckets, int(windowDur/bucketDur)),
}
}
type buckets struct {
end int64
count int64
}
// simpleCounter is a none thread-safe implementation of
// bucket-window counter and only records count
type simpleCounter struct {
bucketDur int64 // bucket duration
buckets []buckets
index int
}
// incr incrases the count by 1
func (c *simpleCounter) incr() {
now := timeNow()
cur := &c.buckets[c.index]
if now < cur.end {
cur.count++
return
}
// cylically initilaize next bucket
c.index = (c.index + 1) % len(c.buckets)
cur = &c.buckets[c.index]
cur.end = now - now%c.bucketDur + c.bucketDur
cur.count = 1
}
// get returns total count of all buckets
func (c *simpleCounter) get() int64 {
now := timeNow()
sum := int64(0)
w := int64(len(c.buckets)) * c.bucketDur
for _, b := range c.buckets {
if b.end+w > now {
sum += b.count
}
}
return sum
}