Skip to content

Commit

Permalink
Merge pull request #223 from dkropachev/dk/fix-idgenerator-getstream
Browse files Browse the repository at this point in the history
Simplify IDGenerator and reduce collission rate
  • Loading branch information
dkropachev authored Aug 8, 2024
2 parents 009034a + a0de29d commit 77db780
Showing 1 changed file with 2 additions and 7 deletions.
9 changes: 2 additions & 7 deletions internal/streams/streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,8 @@ func streamFromBucket(bucket, streamInBucket int) int {
}

func (s *IDGenerator) GetStream() (int, bool) {
// based closely on the java-driver stream ID generator
// avoid false sharing subsequent requests.
offset := atomic.LoadUint32(&s.offset)
for !atomic.CompareAndSwapUint32(&s.offset, offset, (offset+1)%s.numBuckets) {
offset = atomic.LoadUint32(&s.offset)
}
offset = (offset + 1) % s.numBuckets
// Reduce collisions by offsetting the starting point
offset := atomic.AddUint32(&s.offset, 1)

for i := uint32(0); i < s.numBuckets; i++ {
pos := int((i + offset) % s.numBuckets)
Expand Down

0 comments on commit 77db780

Please sign in to comment.