File tree 1 file changed +8
-4
lines changed
1 file changed +8
-4
lines changed Original file line number Diff line number Diff line change 7
7
"math/rand"
8
8
"sort"
9
9
"sync"
10
- "sync/atomic"
11
10
)
12
11
13
12
// The Balancer interface provides an abstraction of the message distribution
@@ -42,8 +41,10 @@ func (f BalancerFunc) Balance(msg Message, partitions ...int) int {
42
41
type RoundRobin struct {
43
42
ChunkSize int
44
43
// Use a 32 bits integer so RoundRobin values don't need to be aligned to
45
- // apply atomic increments.
44
+ // apply increments.
46
45
counter uint32
46
+
47
+ mutex sync.Mutex
47
48
}
48
49
49
50
// Balance satisfies the Balancer interface.
@@ -52,14 +53,17 @@ func (rr *RoundRobin) Balance(msg Message, partitions ...int) int {
52
53
}
53
54
54
55
func (rr * RoundRobin ) balance (partitions []int ) int {
56
+ rr .mutex .Lock ()
57
+ defer rr .mutex .Unlock ()
58
+
55
59
if rr .ChunkSize < 1 {
56
60
rr .ChunkSize = 1
57
61
}
58
62
59
63
length := len (partitions )
60
- counterNow := atomic . LoadUint32 ( & rr .counter )
64
+ counterNow := rr .counter
61
65
offset := int (counterNow / uint32 (rr .ChunkSize ))
62
- atomic . AddUint32 ( & rr .counter , 1 )
66
+ rr .counter ++
63
67
return partitions [offset % length ]
64
68
}
65
69
You can’t perform that action at this time.
0 commit comments