feat: prefer random IP during health check

This commit is contained in:
Jonathan Barrow 2026-01-12 12:53:46 -05:00
parent cd5755745c
commit 5b44d9f9aa
No known key found for this signature in database
GPG Key ID: 2A7DAA6DED5A77E5

View File

@ -88,25 +88,34 @@ ServerSchema.method('getServerConnectInfo', async function (): Promise<IServerCo
}; };
} }
const healthCheckTargets = ipList.map(ip => ({ // * Remove the random IP from the race pool to remove the duplicate health check
const healthCheckTargets = ipList.filter(ip => ip !== randomIP).map(ip => ({
host: ip, host: ip,
port: this.health_check_port! port: this.health_check_port!
})); }));
let target: string | undefined; // * Default to the random IP in case nothing responded in time
// * and just Hope For The Best:tm:
let target = randomIP;
try { // * Check the random IP and start the race at the same time, preferring
// * Pick the first address that wins the health check. If no address responds in 2 seconds // * the result of the random IP should it succeed. Worst case scenario
// * nothing is returned // * this takes 2 seconds to complete
target = await Promise.race(healthCheckTargets.map(target => healthCheck(target))); const [randomResult, raceResult] = await Promise.allSettled([
} catch { healthCheck({ host: randomIP, port: this.health_check_port! }),
// * Eat error for now, this means that no address responded in time Promise.race(healthCheckTargets.map(target => healthCheck(target)))
// TODO - Handle this ]);
LOG_WARN(`Server ${this.service_name} faield to find healthy NEX server. Falling back to random IP`);
if (randomResult.status === 'rejected') {
if (raceResult.status === 'fulfilled') {
target = raceResult.value;
} else {
LOG_WARN(`Server ${this.service_name} failed to find healthy NEX server. Using the randomly selected IP ${target}`);
}
} }
return { return {
ip: target || randomIP, // * Just use a random IP if nothing responded in time and Hope For The Best:tm: ip: target,
port: this.port port: this.port
}; };
}); });