-
Notifications
You must be signed in to change notification settings - Fork 23
/
counter.go
56 lines (49 loc) · 1.34 KB
/
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
56
package shuttle
import (
"sync"
"time"
)
// Counter is used to track 2 values for a given metric. The first item is the
// "all time" metric counterand the second is the last value since the metric was
// ReadAndReset. Counters are safe for concurrent use.
type Counter struct {
value int
allTimeValue int
lastRAR time.Time
sync.Mutex
}
// NewCounter returns a new Counter initialized to the initial value
func NewCounter(initial int) *Counter {
return &Counter{value: initial, allTimeValue: initial, lastRAR: time.Now()}
}
// Read returns the current value of the Counter
func (c *Counter) Read() int {
c.Mutex.Lock()
defer c.Mutex.Unlock()
return c.value
}
// AllTime returns the current alltime value of the Counter
func (c *Counter) AllTime() int {
c.Mutex.Lock()
defer c.Mutex.Unlock()
return c.allTimeValue
}
// ReadAndReset returns the current value and the last time it was reset, then
// resets the value and the last reset time to time.Now()
func (c *Counter) ReadAndReset() (int, time.Time) {
c.Mutex.Lock()
defer c.Mutex.Unlock()
defer func() {
c.value = 0
c.lastRAR = time.Now()
}()
return c.value, c.lastRAR
}
// Add increments the counter (alltime and current), returning the new value
func (c *Counter) Add(u int) int {
c.Mutex.Lock()
defer c.Mutex.Unlock()
c.allTimeValue += u
c.value += u
return c.value
}