Merge pull request #2979 from murgatroid99/grpc-js_round_robin_random_start

grpc-js: round_robin: Start connecting to endpoints from a random index
This commit is contained in:
Michael Lumish 2025-07-18 14:07:12 -07:00 committed by GitHub
commit 01db2bc620
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 6 additions and 1 deletions

View File

@ -90,6 +90,10 @@ class RoundRobinPicker implements Picker {
}
}
function rotateArray<T>(list: T[], startIndex: number) {
return [...list.slice(startIndex), ...list.slice(0, startIndex)];
}
export class RoundRobinLoadBalancer implements LoadBalancer {
private children: LeafLoadBalancer[] = [];
@ -228,7 +232,8 @@ export class RoundRobinLoadBalancer implements LoadBalancer {
}
return true;
}
const endpointList = maybeEndpointList.value;
const startIndex = (Math.random() * maybeEndpointList.value.length) | 0;
const endpointList = rotateArray(maybeEndpointList.value, startIndex);
this.resetSubchannelList();
if (endpointList.length === 0) {
const errorMessage = `No addresses resolved. Resolution note: ${resolutionNote}`;