add config.performance.unfurlSrvCacheTtl option for caching resolveSrv calls

This commit is contained in:
Nick Krecklow
2020-05-08 16:53:48 -05:00
parent f6780e7a9b
commit 11d3031b6c
5 changed files with 74 additions and 27 deletions

View File

@ -1,5 +1,3 @@
const dns = require('dns')
const minecraftJavaPing = require('mc-ping-updated')
const minecraftBedrockPing = require('mcpe-ping-fixed')
@ -8,10 +6,10 @@ const MessageOf = require('./message')
const config = require('../config')
function ping (host, port, type, timeout, callback, version) {
switch (type) {
function ping (serverRegistration, timeout, callback, version) {
switch (serverRegistration.data.type) {
case 'PC':
unfurlSrv(host, port, (host, port) => {
serverRegistration.unfurlSrv((host, port) => {
minecraftJavaPing(host, port || 25565, (err, res) => {
if (err) {
callback(err)
@ -35,13 +33,13 @@ function ping (host, port, type, timeout, callback, version) {
break
case 'PE':
minecraftBedrockPing(host, port || 19132, (err, res) => {
minecraftBedrockPing(serverRegistration.data.ip, serverRegistration.data.port || 19132, (err, res) => {
if (err) {
callback(err)
} else {
callback(null, {
players: {
online: capPlayerCount(host, parseInt(res.currentPlayers))
online: capPlayerCount(serverRegistration.data.ip, parseInt(res.currentPlayers))
}
})
}
@ -49,25 +47,10 @@ function ping (host, port, type, timeout, callback, version) {
break
default:
throw new Error('Unsupported type: ' + type)
throw new Error('Unsupported type: ' + serverRegistration.data.type)
}
}
function unfurlSrv (hostname, port, callback) {
if (config.performance && config.performance.skipUnfurlSrv) {
callback(hostname, port)
return
}
dns.resolveSrv('_minecraft._tcp.' + hostname, (_, records) => {
if (!records || records.length < 1) {
callback(hostname, port)
} else {
callback(records[0].name, records[0].port)
}
})
}
// player count can be up to 1^32-1, which is a massive scale and destroys browser performance when rendering graphs
// Artificially cap and warn to prevent propogating garbage
function capPlayerCount (host, playerCount) {
@ -136,7 +119,7 @@ class PingController {
for (const serverRegistration of this._app.serverRegistrations) {
const version = serverRegistration.getNextProtocolVersion()
ping(serverRegistration.data.ip, serverRegistration.data.port, serverRegistration.data.type, config.rates.connectTimeout, (err, resp) => {
ping(serverRegistration, config.rates.connectTimeout, (err, resp) => {
if (err) {
logger.log('error', 'Failed to ping %s: %s', serverRegistration.data.ip, err.message)
}