func (this *SaramaPartitioner) Partition(key sarama.Encoder, numPartitions int32) (int32, error) { keyBytes, err := key.Encode() if err != nil { return -1, err } return this.partitioner.Partition(keyBytes, numPartitions) }
// Partition takes the key and partition count and chooses a partition. IntPartitioner should ONLY receive Int32Encoder keys. // Passing it a Int32Encoder(2) key means it should assign the incoming message partition = 2 (assuming this partition exists, otherwise there's no guarantee which partition will be picked). func (this *IntPartitioner) Partition(key sarama.Encoder, numPartitions int32) (int32, error) { if key == nil { panic("IntPartitioner does not work without keys.") } b, err := key.Encode() if err != nil { return -1, err } buf := bytes.NewBuffer(b) partition, err := binary.ReadUvarint(buf) if err != nil { return -1, err } return int32(partition) % numPartitions, nil }