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 2 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
6 changes: 4 additions & 2 deletions partitioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,20 @@ func (p *roundRobinPartitioner) RequiresConsistency() bool {
return false
}

type hasherFunc func() hash.Hash32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't even bother naming this type, especially since it's weird for a non-exported interface to be an argument to an exported method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, true.


type hashPartitioner struct {
random Partitioner
hasher hash.Hash32
}

// NewCustomHashPartitioner is a wrapper around NewHashPartitioner,
// allowing the use of custom hasher
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be worth documenting here that it expects the method reference not an instance of the hasher

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely. I might have gotten a bit in a hurry to open the PR, since I realised my mistake.

func NewCustomHashPartitioner(hasher hash.Hash32) PartitionerConstructor {
func NewCustomHashPartitioner(hasher hasherFunc) 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