Browse Source

Handle when client.Partitions returns empty slice

This can happen as documented in #49, when all the partitions for a slice
undergo leader election simultaneously.

In this case, return LeaderNotAvailable, which is true even if we haven't chosen
a partition yet :)
Evan Huus 10 years ago
parent
commit
4b02374ea7
1 changed files with 4 additions and 0 deletions
  1. 4 0
      producer.go

+ 4 - 0
producer.go

@@ -439,6 +439,10 @@ func (p *Producer) choosePartition(topic string, key Encoder) (int32, error) {
 
 	numPartitions := int32(len(partitions))
 
+	if numPartitions == 0 {
+		return -1, LeaderNotAvailable
+	}
+
 	choice := p.config.Partitioner.Partition(key, numPartitions)
 
 	if choice < 0 || choice >= numPartitions {