Skip to content

Commit

Permalink
Fixed a data race in the Hash balancer when using custom hasher (segm…
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve van Loben Sels authored Aug 9, 2019
1 parent 11670d5 commit 433ef07
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ var (
type Hash struct {
rr RoundRobin
Hasher hash.Hash32

// lock protects Hasher while calculating the hash code. It is assumed that
// the Hasher field is read-only once the Balancer is created, so as a
// performance optimization, reads of the field are not protected.
lock sync.Mutex
}

func (h *Hash) Balance(msg Message, partitions ...int) (partition int) {
Expand All @@ -142,7 +147,10 @@ func (h *Hash) Balance(msg Message, partitions ...int) (partition int) {
}

hasher := h.Hasher
if hasher == nil {
if hasher != nil {
h.lock.Lock()
defer h.lock.Unlock()
} else {
hasher = fnv1aPool.Get().(hash.Hash32)
defer fnv1aPool.Put(hasher)
}
Expand Down

0 comments on commit 433ef07

Please sign in to comment.