/
rollingcounter.go
59 lines (47 loc) · 1.29 KB
/
rollingcounter.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package rollingcounter
import (
"math/rand"
"sync/atomic"
"github.com/hyperledger/fabric-sdk-go/pkg/common/logging"
)
var logger = logging.NewLogger("fabsdk/util")
// Counter is a rolling counter that increments an index up to a maximum value. If the counter reaches
// the maximum then the counter resets to 0. A single counter instance may be used by multiple Go routines.
type Counter struct {
index int32
}
// New returns a new rolling counter
func New() *Counter {
return &Counter{index: -1}
}
// Next increments the counter. If the counter reaches n then
// the counter is reset to 0. Note: n must be greater than 0 or else
// a panic will result.
func (c *Counter) Next(n int) int {
if n <= 0 {
panic("n must be greater than 0")
}
for {
current := atomic.LoadInt32(&c.index)
logger.Debugf("Current index: %d", current)
i := int(current)
if i == -1 {
// Choose a random index the first time
i = rand.Intn(n)
} else {
i++
if i >= n {
i = 0
}
}
if atomic.CompareAndSwapInt32(&c.index, current, int32(i)) {
logger.Debugf("Set the counter to %d", i)
return i
}
logger.Debugf("Another thread has already incremented the counter. Trying again...")
}
}