feat: breadth attempt order (#486)

* feat: breadth attempt order

* fix: remove stray console log from debugging
This commit is contained in:
CosminPerRam 2024-01-18 23:11:03 +02:00 committed by GitHub
parent 6746442254
commit 1ef09d470b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 23 additions and 13 deletions

View file

@ -8,6 +8,7 @@ const defaultOptions = {
maxRetries: 1,
stripColors: true,
portCache: true,
noBreadthOrder: false,
ipFamily: 0
}
@ -76,22 +77,29 @@ export default class QueryRunner {
const numRetries = userOptions.maxRetries || gameOptions.maxRetries || defaultOptions.maxRetries
const retries = Array.from({ length: numRetries }, (x, i) => i)
const attemptOrder = []
if (optionsCollection.noBreadthOrder) {
attempts.forEach(attempt => retries.forEach(retry => attemptOrder.push({ attempt, retry })))
} else {
retries.forEach(retry => attempts.forEach(attempt => attemptOrder.push({ attempt, retry })))
}
let attemptNum = 0
const errors = []
for (const attempt of attempts) {
for (let retry = 0; retry < numRetries; retry++) {
attemptNum++
for (const { attempt, retry } of attemptOrder) {
attemptNum++
try {
const response = await this._attempt(attempt)
if (attempt.portCache) {
this.portCache[`${userOptions.address}:${userOptions.port}`] = attempt.port
}
return response
} catch (e) {
e.stack = 'Attempt #' + attemptNum + ' - Port=' + attempt.port + ' Retry=' + (retry) + ':\n' + e.stack
errors.push(e)
try {
const response = await this._attempt(attempt)
if (attempt.portCache) {
this.portCache[`${userOptions.address}:${userOptions.port}`] = attempt.port
}
return response
} catch (e) {
e.stack = 'Attempt #' + attemptNum + ' - Port=' + attempt.port + ' Retry=' + (retry) + ':\n' + e.stack
errors.push(e)
}
}