@platformatic/kafka
Version:
Modern and performant client for Apache Kafka
24 lines (23 loc) • 951 B
JavaScript
export function roundRobinAssigner(_current, members, topics, metadata) {
const membersSize = members.size;
const assignments = [];
// Flat the list of members and subscribed topics
for (const memberId of members.keys()) {
assignments.push({ memberId, assignments: new Map() });
}
// Assign topic-partitions in round robin
let currentMember = 0;
for (const topic of topics) {
const partitionsCount = metadata.topics.get(topic).partitionsCount;
for (let i = 0; i < partitionsCount; i++) {
const member = assignments[currentMember++ % membersSize];
let topicAssignments = member.assignments.get(topic);
if (!topicAssignments) {
topicAssignments = { topic, partitions: [] };
member.assignments.set(topic, topicAssignments);
}
topicAssignments?.partitions.push(i);
}
}
return assignments;
}