From df9e6c7c0ee6af27bbc97e32d77f81a9e09ee32f Mon Sep 17 00:00:00 2001 From: Jonathan Barrow Date: Fri, 6 Feb 2026 15:15:21 -0500 Subject: [PATCH] fix: only run Promise.race if healthCheckTargets has data fixes #321 --- src/models/server.ts | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/models/server.ts b/src/models/server.ts index 65f14ea..1a19b4d 100644 --- a/src/models/server.ts +++ b/src/models/server.ts @@ -98,19 +98,24 @@ ServerSchema.method('getServerConnectInfo', async function (): Promise healthCheck(target))) - ]); + // * If the server only has 1 IP, healthCheckTargets will be empty and Promise.race + // * will never resolve. In those cases, just skip this step entirely since we'd + // * have to use that singular IP regardless + if (healthCheckTargets.length !== 0) { + // * Check the random IP and start the race at the same time, preferring + // * the result of the random IP should it succeed. Worst case scenario + // * this takes 2 seconds to complete + const [randomResult, raceResult] = await Promise.allSettled([ + healthCheck({ host: randomIP, port: this.health_check_port! }), + Promise.race(healthCheckTargets.map(target => healthCheck(target))) + ]); - 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}`); + 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}`); + } } }