feat: breadth attempt order (#486)
* feat: breadth attempt order * fix: remove stray console log from debugging
This commit is contained in:
parent
6746442254
commit
1ef09d470b
|
@ -36,6 +36,7 @@
|
|||
* `stripColors` (defaults to `true`) for protocols that strips colors: unreal2, savage2, quake3, nadeo, gamespy2, doom3, armagetron.
|
||||
* `requestRulesRequired` (defaults to `false`) Valve games only. `requestRules` is always required to have a response or the query will timeout.
|
||||
* `requestPlayersRequired` (defaults to `false`) Valve games only. Querying players is always required to have a response or the query will timeout. Some [games](GAMES_LIST.md) may not provide a players response.
|
||||
* `noBreadthOrder` (defaults to `false`). If multiple attempts are to be made, disable doing one of each type until reaching the retry count.
|
||||
* Now documented: `address` (defaults to `undefined`) Override the IP address of the server skipping DNS resolution. When set, host will not be resolved, instead address will be connected to. However, some protocols still use host for other reasons e.g. as part of the query.
|
||||
* `maxAttempts` has been renamed to `maxRetries`.
|
||||
|
||||
|
|
|
@ -52,6 +52,7 @@ Confused on how this works, or you want to see more? Checkout the [examples](/ex
|
|||
| **requestPlayersRequired** | boolean | false | Valve games only. Querying players is always required to have a response or the query will timeout. Some [games](GAMES_LIST.md) may not provide a players response. |
|
||||
| **stripColors** | boolean | true | Enables stripping colors for protocols: unreal2, savage2, quake3, nadeo, gamespy2, doom3, armagetron. |
|
||||
| **portCache** | boolean | true | After you queried a server, the second time you query that exact server (identified by specified ip and port), first add an attempt to query with the last successful port. |
|
||||
| **noBreadthOrder** | boolean | false | Enable the behaviour of retrying an attempt X times followed by the next attempt X times, otherwise try attempt A, then B, then A, then B until reaching the X retry count of each. |
|
||||
|
||||
## Query Response
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ import Minimist from 'minimist'
|
|||
import { GameDig } from './../lib/index.js'
|
||||
|
||||
const argv = Minimist(process.argv.slice(2), {
|
||||
boolean: ['pretty', 'debug', 'givenPortOnly', 'requestRules', 'requestRulesRequired', 'requestPlayersRequired', 'stripColors', 'portCache'],
|
||||
boolean: ['pretty', 'debug', 'givenPortOnly', 'requestRules', 'requestRulesRequired', 'requestPlayersRequired', 'stripColors', 'portCache', 'noBreadthOrder'],
|
||||
string: ['guildId', 'listenUdpPort', 'ipFamily'],
|
||||
default: {
|
||||
stripColors: true,
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue