-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax_counter.go
208 lines (166 loc) · 3.6 KB
/
max_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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package gounter
import (
"math"
"sync"
"sync/atomic"
)
// MaxCounter has a max number for counter.
// When counter to max number, it will stop and reject all other actions.
type MaxCounter struct {
noCopy noCopy
counter *Counter
done uint32
maxBits uint64
}
// maxCounterPool
var maxCounterPool = &sync.Pool{
New: func() any {
return &MaxCounter{}
},
}
// AcquireMaxCounter acquire a MaxCounter from pool.
func AcquireMaxCounter(max float64) *MaxCounter {
maxCounter := maxCounterPool.Get().(*MaxCounter)
maxCounter.SetMax(max)
maxCounter.counter = AcquireCounter()
return maxCounter
}
// ReleaseMaxCounter releases MaxCounter.
func ReleaseMaxCounter(c *MaxCounter) {
if c == nil {
return
}
c.reset()
ReleaseCounter(c.counter)
c.counter = nil
maxCounterPool.Put(c)
}
// reset MaxCounter
// And releases Counter
func (c *MaxCounter) reset() {
if c.counter != nil {
c.counter.Reset()
} else {
c.counter = AcquireCounter()
}
atomic.StoreUint64(&c.maxBits, 0)
atomic.StoreUint32(&c.done, 0)
}
// isDone say now is max?
func (c *MaxCounter) isDone() bool {
done := atomic.LoadUint32(&c.done)
return done != 0
}
// setDone set add done, now is max.
func (c *MaxCounter) setDone() {
atomic.StoreUint32(&c.done, 1)
}
// setUnDone set done value = 0.
func (c *MaxCounter) setUnDone() {
atomic.StoreUint32(&c.done, 0)
}
// Can use add?
func (c *MaxCounter) Can() bool {
return !c.isDone()
}
// GetMax gets a max number.
func (c *MaxCounter) GetMax() float64 {
bits := atomic.LoadUint64(&c.maxBits)
max := math.Float64frombits(bits)
return max
}
// SetMax set a max number.
func (c *MaxCounter) SetMax(max float64) {
for {
oldBits := atomic.LoadUint64(&c.maxBits)
newBits := math.Float64bits(max)
if atomic.CompareAndSwapUint64(&c.maxBits, oldBits, newBits) {
return
}
}
}
// Set sets the value of the counter to the given value,
// if it is less than or equal to the maximum value
func (c *MaxCounter) Set(value float64) bool {
max := c.GetMax()
if max < value {
return false
}
return c.counter.Set(value)
}
// Get a number.
func (c *MaxCounter) Get() float64 {
return c.counter.Get()
}
// Real get Counter Real().
func (c *MaxCounter) Real() float64 {
return c.counter.Real()
}
// Reset reset MaxCounter.
func (c *MaxCounter) Reset() {
c.reset()
}
// Add is same as Counter.Add().
func (c *MaxCounter) Add(delta float64) bool {
if c.isDone() && delta >= 0 {
return false
}
if c.isDone() && delta < 0 {
c.setUnDone()
}
max := c.GetMax()
realNum := c.Real()
if realNum >= max && delta > 0 {
c.setDone()
return false
}
if realNum <= 0 && delta < 0 {
return false
}
return c.counter.Add(delta)
}
// Sub is same as Counter.Sub().
func (c *MaxCounter) Sub(delta float64) bool {
return c.Add(delta * -1)
}
// Inc is same as Counter.Inc().
func (c *MaxCounter) Inc() bool {
return c.Add(1)
}
// Dec is same as Counter.Dec().
func (c *MaxCounter) Dec() bool {
return c.Add(-1)
}
// CopyTo copies number to dst.
func (c *MaxCounter) CopyTo(d interface{}) (ok bool, err error) {
dst, can := d.(*MaxCounter)
if !can {
err = ErrDifferentCounterType
return
}
if c == dst {
err = ErrSameCounterPointer
return
}
for {
oldMaxBits := atomic.LoadUint64(&dst.maxBits)
bits := atomic.LoadUint64(&c.maxBits)
oldCounter := dst.counter
counter := c.counter
if counter == nil {
counter = AcquireCounter()
}
if oldCounter == nil {
oldCounter = AcquireCounter()
}
ok, err = counter.CopyTo(oldCounter)
if !ok {
return
}
dst.counter = oldCounter
if atomic.CompareAndSwapUint64(&dst.maxBits, oldMaxBits, bits) {
ok = true
return
}
}
}