Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CustomHasher #841

Merged
merged 3 commits into from
Mar 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions partitioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,14 @@ type hashPartitioner struct {
hasher hash.Hash32
}

// NewCustomHashPartitioner is a wrapper around NewHashPartitioner,
// allowing the use of custom hasher
func NewCustomHashPartitioner(hasher hash.Hash32) PartitionerConstructor {
// NewCustomHashPartitioner is a wrapper around NewHashPartitioner, allowing the use of custom hasher.
// The argument is a function providing the instance, implementing the hash.Hash32 interface. This is to ensure that
// each partition dispatcher gets its own hasher, to avoid concurrency issues by sharing an instance.
func NewCustomHashPartitioner(hasher func() hash.Hash32) PartitionerConstructor {
return func(topic string) Partitioner {
p := new(hashPartitioner)
p.random = NewRandomPartitioner(topic)
p.hasher = hasher
p.hasher = hasher()
return p
}
}
Expand Down
4 changes: 2 additions & 2 deletions partitioner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestRoundRobinPartitioner(t *testing.T) {

func TestNewHashPartitionerWithHasher(t *testing.T) {
// use the current default hasher fnv.New32a()
partitioner := NewCustomHashPartitioner(fnv.New32a())("mytopic")
partitioner := NewCustomHashPartitioner(fnv.New32a)("mytopic")

choice, err := partitioner.Partition(&ProducerMessage{}, 1)
if err != nil {
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestNewHashPartitionerWithHasher(t *testing.T) {

func TestHashPartitionerWithHasherMinInt32(t *testing.T) {
// use the current default hasher fnv.New32a()
partitioner := NewCustomHashPartitioner(fnv.New32a())("mytopic")
partitioner := NewCustomHashPartitioner(fnv.New32a)("mytopic")

msg := ProducerMessage{}
// "1468509572224" generates 2147483648 (uint32) result from Sum32 function
Expand Down