forked from uber/tchannel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter.go
109 lines (94 loc) · 3.12 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
// Copyright (c) 2015 Uber Technologies, Inc.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package testutils
import (
"sync"
"github.com/uber-go/atomic"
)
// Decrement is the interface returned by Decrementor.
type Decrement interface {
// Single returns whether any more tokens are remaining.
Single() bool
// Multiple tries to get n tokens. It returns the actual amount of tokens
// available to use. If this is 0, it means there are no tokens left.
Multiple(n int) int
}
type decrementor struct {
n atomic.Int64
}
func (d *decrementor) Single() bool {
return d.n.Dec() >= 0
}
func (d *decrementor) Multiple(n int) int {
decBy := -1 * int64(n)
decremented := d.n.Add(decBy)
if decremented <= decBy {
// Already out of tokens before this decrement.
return 0
} else if decremented < 0 {
// Not enough tokens, return how many tokens we actually could decrement.
return n + int(decremented)
}
return n
}
// Decrementor returns a function that can be called from multiple goroutines and ensures
// it will only return true n times.
func Decrementor(n int) Decrement {
return &decrementor{
n: *atomic.NewInt64(int64(n)),
}
}
// Batch returns a slice with n broken into batches of size batchSize.
func Batch(n, batchSize int) []int {
fullBatches := n / batchSize
batches := make([]int, 0, fullBatches+1)
for i := 0; i < fullBatches; i++ {
batches = append(batches, batchSize)
}
if remaining := n % batchSize; remaining > 0 {
batches = append(batches, remaining)
}
return batches
}
// Buckets splits n over the specified number of buckets.
func Buckets(n int, numBuckets int) []int {
perBucket := n / numBuckets
buckets := make([]int, numBuckets)
for i := range buckets {
buckets[i] = perBucket
if i == 0 {
buckets[i] += n % numBuckets
}
}
return buckets
}
// RunN runs the given f n times (and passes the run's index) and waits till they complete.
// It starts n-1 goroutines, and runs one instance in the current goroutine.
func RunN(n int, f func(i int)) {
var wg sync.WaitGroup
for i := 0; i < n-1; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
f(i)
}(i)
}
f(n - 1)
wg.Wait()
}