feat: port caching (#478)

* feat: port caching

* feat: dont store in cache if disabled
This commit is contained in:
CosminPerRam 2024-01-17 23:23:20 +02:00 committed by GitHub
parent 1f0563f7d2
commit b48a4398cd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 24 additions and 7 deletions

View file

@ -7,6 +7,7 @@ const defaultOptions = {
attemptTimeout: 10000,
maxAttempts: 1,
stripColors: true,
portCache: true,
ipFamily: 0
}
@ -15,6 +16,7 @@ export default class QueryRunner {
this.udpSocket = new GlobalUdpSocket({
port: runnerOpts.listenUdpPort
})
this.portCache = {}
}
async run (userOptions) {
@ -50,11 +52,18 @@ export default class QueryRunner {
gameQueryPortOffset ? portOffsetArray = [gameQueryPortOffset] : portOffsetArray = [0]
}
const cachedPort = this.portCache[`${userOptions.address}:${userOptions.port}`]
if (cachedPort && optionsCollection.portCache) {
addAttemptWithPort(cachedPort)
}
if (userOptions.port) {
if (!userOptions.givenPortOnly) {
portOffsetArray.forEach((portOffset) => { addAttemptWithPort(userOptions.port + portOffset) })
if (userOptions.port === gameOptions.port && gameQueryPort) { addAttemptWithPort(gameQueryPort) }
}
attempts.push(optionsCollection)
} else if (gameQueryPort) {
addAttemptWithPort(gameQueryPort)
@ -74,7 +83,11 @@ export default class QueryRunner {
attemptNum++
try {
return await this._attempt(attempt)
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)