Skip to content

Commit 01db2bc

Browse files
authored
Merge pull request #2979 from murgatroid99/grpc-js_round_robin_random_start
grpc-js: round_robin: Start connecting to endpoints from a random index
2 parents d9c8ac3 + 869e515 commit 01db2bc

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

packages/grpc-js/src/load-balancer-round-robin.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ class RoundRobinPicker implements Picker {
9090
}
9191
}
9292

93+
function rotateArray<T>(list: T[], startIndex: number) {
94+
return [...list.slice(startIndex), ...list.slice(0, startIndex)];
95+
}
96+
9397
export class RoundRobinLoadBalancer implements LoadBalancer {
9498
private children: LeafLoadBalancer[] = [];
9599

@@ -228,7 +232,8 @@ export class RoundRobinLoadBalancer implements LoadBalancer {
228232
}
229233
return true;
230234
}
231-
const endpointList = maybeEndpointList.value;
235+
const startIndex = (Math.random() * maybeEndpointList.value.length) | 0;
236+
const endpointList = rotateArray(maybeEndpointList.value, startIndex);
232237
this.resetSubchannelList();
233238
if (endpointList.length === 0) {
234239
const errorMessage = `No addresses resolved. Resolution note: ${resolutionNote}`;

0 commit comments

Comments
 (0)