mirror of
https://github.com/tribufu/node-gamedig
synced 2026-06-01 09:42:41 +00:00
feat: port caching (#478)
* feat: port caching * feat: dont store in cache if disabled
This commit is contained in:
parent
1f0563f7d2
commit
b48a4398cd
4 changed files with 24 additions and 7 deletions
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue