Skip to content

Commit 8aa6f77

Browse files
authored
Add t:GenStage.PartitionDispatcher.hash_function/0 (#290)
1 parent 9366a08 commit 8aa6f77

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

lib/gen_stage/dispatchers/partition_dispatcher.ex

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,24 @@ defmodule GenStage.PartitionDispatcher do
3333
* `:partitions` - the number of partitions to dispatch to. It may be
3434
an integer with a total number of partitions, where each partition
3535
is named from 0 up to `integer - 1`. For example, `partitions: 4`
36-
will contain 4 partitions named 0, 1, 2 and 3.
36+
will contain four partitions named `0`, `1`, `2` and `3`.
3737
38-
It may also be an enumerable that specifies the name of every partition.
38+
It may also be an *enumerable* that specifies the name of every partition.
3939
For instance, `partitions: [:odd, :even]` will build two partitions,
4040
named `:odd` and `:even`.
4141
42-
* `:hash` - the hashing algorithm, which receives the event and returns
43-
a tuple with two elements, the event to be dispatched as first argument
44-
and the partition as second. The partition must be one of the partitions
45-
specified in `:partitions` above. The default uses
46-
`fn event -> {event, :erlang.phash2(event, Enum.count(partitions))} end`
47-
on the event to select the partition. If it returns `:none`, the event
48-
is discarded.
42+
* `:hash` - the hashing algorithm. It's a function of type
43+
`t:hash_function/0`, which receives the event and returns a tuple with two
44+
elements, the event to be dispatched as first argument and the partition
45+
as second. The function can also return `:none`, in which case the event
46+
is discarded. The partition must be one of the partitions specified in
47+
`:partitions` above. The default uses:
48+
49+
fn event -> {event, :erlang.phash2(event, Enum.count(partitions))} end
4950
5051
### Examples
5152
52-
To start a producer with four partitions named 0, 1, 2 and 3:
53+
To start a producer with four partitions named `0`, `1`, `2`, and `3`:
5354
5455
{:producer, state, dispatcher: {GenStage.PartitionDispatcher, partitions: 0..3}}
5556
@@ -77,6 +78,12 @@ defmodule GenStage.PartitionDispatcher do
7778
7879
"""
7980

81+
@typedoc """
82+
The type used for the function passed to the `:hash` option.
83+
"""
84+
@typedoc since: "1.2.0"
85+
@type hash_function :: (event :: any -> {event :: any, partition :: any} | :none)
86+
8087
@behaviour GenStage.Dispatcher
8188
@init {nil, nil, 0}
8289

0 commit comments

Comments
 (0)