File tree Expand file tree Collapse file tree 3 files changed +8
-7
lines changed Expand file tree Collapse file tree 3 files changed +8
-7
lines changed Original file line number Diff line number Diff line change @@ -12,14 +12,13 @@ def __init__(self, partitions):
1212 """
1313 self .partitions = partitions
1414
15- def partition (self , key , partitions ):
15+ def partition (self , key , partitions = None ):
1616 """
1717 Takes a string key and num_partitions as argument and returns
1818 a partition to be used for the message
1919
2020 Arguments:
21- partitions: The list of partitions is passed in every call. This
22- may look like an overhead, but it will be useful
23- (in future) when we handle cases like rebalancing
21+ key: the key to use for partitioning
22+ partitions: (optional) a list of partitions.
2423 """
2524 raise NotImplementedError ('partition function has to be implemented' )
Original file line number Diff line number Diff line change @@ -5,7 +5,9 @@ class HashedPartitioner(Partitioner):
55 Implements a partitioner which selects the target partition based on
66 the hash of the key
77 """
8- def partition (self , key , partitions ):
8+ def partition (self , key , partitions = None ):
9+ if not partitions :
10+ partitions = self .partitions
911 size = len (partitions )
1012 idx = hash (key ) % size
1113
Original file line number Diff line number Diff line change @@ -15,9 +15,9 @@ def _set_partitions(self, partitions):
1515 self .partitions = partitions
1616 self .iterpart = cycle (partitions )
1717
18- def partition (self , key , partitions ):
18+ def partition (self , key , partitions = None ):
1919 # Refresh the partition list if necessary
20- if self .partitions != partitions :
20+ if partitions and self .partitions != partitions :
2121 self ._set_partitions (partitions )
2222
2323 return next (self .iterpart )
You can’t perform that action at this time.
0 commit comments