-
Notifications
You must be signed in to change notification settings - Fork 5
/
counter.go
181 lines (148 loc) · 3.5 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package concurrentcount
import (
"math"
"sync"
"sync/atomic"
)
// Counter is the interface of all counter implementations. Prometheus always
// uses float64 as values, so we are doing so here, although a pure integer
// counter would be sufficient (or even more suitable) in most situations.
type Counter interface {
Get() float64
Inc()
Add(float64)
}
type MutexCounter struct {
mtx sync.Mutex
value float64
}
func (c *MutexCounter) Get() float64 {
c.mtx.Lock()
defer c.mtx.Unlock()
return c.value
}
func (c *MutexCounter) Inc() {
c.mtx.Lock()
defer c.mtx.Unlock()
c.value++
}
func (c *MutexCounter) Add(delta float64) {
c.mtx.Lock()
defer c.mtx.Unlock()
c.value += delta
}
type RWMutexCounter struct {
mtx sync.RWMutex
value float64
}
func (c *RWMutexCounter) Get() float64 {
c.mtx.RLock()
defer c.mtx.RUnlock()
return c.value
}
func (c *RWMutexCounter) Inc() {
c.mtx.Lock()
defer c.mtx.Unlock()
c.value++
}
func (c *RWMutexCounter) Add(delta float64) {
c.mtx.Lock()
defer c.mtx.Unlock()
c.value += delta
}
// AtomicIntCounter uses an int64 internally.
type AtomicIntCounter int64
func (c *AtomicIntCounter) Get() float64 {
return float64(atomic.LoadInt64((*int64)(c)))
}
func (c *AtomicIntCounter) Inc() {
atomic.AddInt64((*int64)(c), 1)
}
// Add ignores the non-integer part of delta.
func (c *AtomicIntCounter) Add(delta float64) {
atomic.AddInt64((*int64)(c), int64(delta))
}
// NaiveCounter does not apply any locking.
type NaiveCounter float64
func (c *NaiveCounter) Get() float64 {
return float64(*c)
}
func (c *NaiveCounter) Inc() {
(*c)++
}
// Add ignores the non-integer part of delta.
func (c *NaiveCounter) Add(delta float64) {
*c += NaiveCounter(delta)
}
// NaiveIntCounter uses an int64 internally and does not apply any locking.
type NaiveIntCounter int64
func (c *NaiveIntCounter) Get() float64 {
return float64(*c)
}
func (c *NaiveIntCounter) Inc() {
(*c)++
}
// Add ignores the non-integer part of delta.
func (c *NaiveIntCounter) Add(delta float64) {
*c += NaiveIntCounter(delta)
}
// AtomicCounter uses an uint64 internally and still deals with real
// float64's. However, it has to use CompareAndSwapUint64 for that, which might
// fail during contention. It might need to try several times, spinning...
type AtomicSpinningCounter uint64
func (c *AtomicSpinningCounter) Get() float64 {
return math.Float64frombits(atomic.LoadUint64((*uint64)(c)))
}
func (c *AtomicSpinningCounter) Inc() {
c.Add(1)
}
func (c *AtomicSpinningCounter) Add(delta float64) {
for {
oldBits := atomic.LoadUint64((*uint64)(c))
newBits := math.Float64bits(math.Float64frombits(oldBits) + delta)
if atomic.CompareAndSwapUint64((*uint64)(c), oldBits, newBits) {
return
}
}
}
type ChannelCounter struct {
queue chan float64
value chan float64
}
func NewChannelCounter(bufsize int) Counter {
c := &ChannelCounter{
make(chan float64, bufsize),
make(chan float64),
}
go c.loop()
return c
}
func (c *ChannelCounter) Get() float64 {
return <-c.value
}
func (c *ChannelCounter) Inc() {
c.queue <- 1
}
func (c *ChannelCounter) Add(delta float64) {
c.queue <- delta
}
func (c *ChannelCounter) loop() {
// In a real-life counter, there needed to be a way to shut it down
// again.
var value float64
for {
// Outer select: First process all the waiting samples.
// This is not ideal as it might let Get block forever.
select {
case v := <-c.queue:
value += v
default:
select {
case v := <-c.queue:
value += v
case c.value <- value:
// Do nothing.
}
}
}
}