Skip to content

Commit

Permalink
chore: use other module for default partitioner
Browse files Browse the repository at this point in the history
  • Loading branch information
oliveigah committed May 27, 2024
1 parent 6aa86b6 commit 6b03ba2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
11 changes: 11 additions & 0 deletions lib/klife/default_partitioner.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule Klife.DefaultPartitioner do
@behaviour Klife.Partitioner
alias Klife.Record

@impl Klife.Partitioner
def get_partition(%Record{key: nil}, max_partition),
do: Enum.random(0..max_partition)

def get_partition(%Record{key: key}, max_partition),
do: :erlang.phash2(key, max_partition + 1)
end
12 changes: 1 addition & 11 deletions lib/klife/partitioner.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
defmodule Klife.Partitioner do
alias Klife.Record

@callback get_partition(record :: %Record{}, max_partition :: integer) :: integer

@behaviour __MODULE__

@impl __MODULE__
def get_partition(%Record{key: nil}, max_partition),
do: Enum.random(0..max_partition)

def get_partition(%Record{key: key}, max_partition), do: :erlang.phash2(key, max_partition + 1)
@callback get_partition(record :: %Klife.Record{}, max_partition :: integer) :: integer
end
2 changes: 1 addition & 1 deletion lib/klife/producer/controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ defmodule Klife.Producer.Controller do

data = %{
max_partition: max_partition,
default_partitioner: config_topic[:partitioner] || Klife.Partitioner
default_partitioner: config_topic[:partitioner] || Klife.DefaultPartitioner
}

case :ets.lookup(table_name, topic.name) do
Expand Down

0 comments on commit 6b03ba2

Please sign in to comment.