/
counter.go
47 lines (36 loc) · 916 Bytes
/
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
package metrics
import "sync/atomic"
// Counters hold an int64 value that can be incremented and decremented.
type Counter interface {
// Clear the counter: set it to zero.
Clear()
// Return the current count.
Count() int64
// Decrement the counter by the given amount.
Dec(amount int64) Counter
// Increment the counter by the given amount.
Inc(amount int64) Counter
}
// The standard implementation of a Counter uses the sync/atomic package
// to manage a single int64 value.
type counter struct {
count int64
}
// Create a new counter.
func NewCounter() Counter {
return &counter{count: 0}
}
func (c *counter) Clear() {
atomic.StoreInt64(&c.count, 0)
}
func (c *counter) Count() int64 {
return atomic.LoadInt64(&c.count)
}
func (c *counter) Dec(i int64) Counter {
atomic.AddInt64(&c.count, -i)
return c
}
func (c *counter) Inc(i int64) Counter {
atomic.AddInt64(&c.count, i)
return c
}