Repository navigation
feat: add round-robin connection balancing - #45
stepanenkoxx wants to merge 2 commits into
Conversation
| } | ||
|
|
||
| // the window may shrink between health checks, so wrap the stored position into it | ||
| const index = this.balancingCursor % eligible.length; |
There was a problem hiding this comment.
#1 [P2] Preserve the round-robin position when eligible hosts change
AI generated
The cursor indexes the filtered list, so the same index can point to different hosts after a health check. With one call after each check and eligible proxies alternating between [A, B, C] and [B, C], the picks are A, C, C, C, … even though B stays healthy throughout. Store the cursor in the original connection order and skip ineligible hosts as it advances.
There was a problem hiding this comment.
Each change to the eligible set causes at most one repeated or skipped pick, and the rotation evens out again after that. That only adds up when there's about one request per health check (every 5 s by default), and at that traffic balancing doesn't matter.
I could add something like this
// position in `this.connections` of the connection last returned by round-robin
private lastRoundRobinPosition = -1;
private selectRoundRobin(candidates: PDConnection[], latencyToleranceMs?: number) {
let eligible = candidates;
if (latencyToleranceMs !== undefined) {
const minLatency = fastestConnection(candidates).latency;
eligible = candidates.filter((c) => c.latency <= minLatency + latencyToleranceMs);
}
// `eligible` keeps the order of `this.connections`, so continuing after the last returned
// position does not repeat or skip hosts when hosts join or leave between health checks
const next =
eligible.find((c) => this.connections.indexOf(c) > this.lastRoundRobinPosition) ??
eligible[0];
this.lastRoundRobinPosition = this.connections.indexOf(next);
return next;
}
it works, but it adds complexity for no real gain i think
I suggest keeping current approach
| * is within this value of the lowest one are rotated (0 — only the fastest ones). | ||
| * When omitted, all healthy connections are rotated | ||
| */ | ||
| latencyTolerance?: number; |
There was a problem hiding this comment.
I'd prefer latencyToleranceMs so the unit is clear at the call site.
Make the unit clear at the call site.
8a68da1 to
8d07dd8
Compare
Changes
Testing