ReferenceHash is a Balancer that uses the provided hash function to determine which partition to route messages to. This ensures that messages with the same key are routed to the same partition. The logic to calculate the partition is: (int32(hasher.Sum32()) & 0x7fffffff) % len(partitions) => pa
| 192 | // the Sarama NewReferenceHashPartitioner and ensures that messages produced by kafka-go will |
| 193 | // be delivered to the same topics that the Sarama producer would be delivered to. |
| 194 | type ReferenceHash struct { |
| 195 | rr randomBalancer |
| 196 | Hasher hash.Hash32 |
| 197 | |
| 198 | // lock protects Hasher while calculating the hash code. It is assumed that |
| 199 | // the Hasher field is read-only once the Balancer is created, so as a |
| 200 | // performance optimization, reads of the field are not protected. |
| 201 | lock sync.Mutex |
| 202 | } |
| 203 | |
| 204 | func (h *ReferenceHash) Balance(msg Message, partitions ...int) int { |
| 205 | if msg.Key == nil { |
nothing calls this directly
no outgoing calls
no test coverage detected