use mcping-js, prevent ping loops while the last is still running

This commit is contained in:
Nick Krecklow 2020-05-09 23:28:37 -05:00
parent f9ce280a2b
commit 10bd23cfa4
No known key found for this signature in database
GPG Key ID: 5F149FDE156FFA94
4 changed files with 25 additions and 16 deletions

@ -1,4 +1,4 @@
const minecraftJavaPing = require('mc-ping-updated') const minecraftJavaPing = require('mcping-js')
const minecraftBedrockPing = require('mcpe-ping-fixed') const minecraftBedrockPing = require('mcpe-ping-fixed')
const logger = require('./logger') const logger = require('./logger')
@ -10,7 +10,9 @@ function ping (serverRegistration, timeout, callback, version) {
switch (serverRegistration.data.type) { switch (serverRegistration.data.type) {
case 'PC': case 'PC':
serverRegistration.unfurlSrv((host, port) => { serverRegistration.unfurlSrv((host, port) => {
minecraftJavaPing(host, port || 25565, (err, res) => { const server = new minecraftJavaPing.MinecraftServer(host, port || 25565)
server.ping(timeout, version, (err, res) => {
if (err) { if (err) {
callback(err) callback(err)
} else { } else {
@ -28,7 +30,7 @@ function ping (serverRegistration, timeout, callback, version) {
callback(null, payload) callback(null, payload)
} }
}, timeout, version) })
}) })
break break
@ -71,6 +73,7 @@ function capPlayerCount (host, playerCount) {
class PingController { class PingController {
constructor (app) { constructor (app) {
this._app = app this._app = app
this._isRunningTasks = false
} }
schedule () { schedule () {
@ -113,8 +116,15 @@ class PingController {
} }
startPingTasks = (callback) => { startPingTasks = (callback) => {
if (this._isRunningTasks) {
logger.log('warn', 'Started re-pinging servers before the last loop has finished! You may need to increase "rates.pingAll" in config.json')
return
}
this._isRunningTasks = true
const results = [] const results = []
let remainingTasks = this._app.serverRegistrations.length
for (const serverRegistration of this._app.serverRegistrations) { for (const serverRegistration of this._app.serverRegistrations) {
const version = serverRegistration.getNextProtocolVersion() const version = serverRegistration.getNextProtocolVersion()
@ -130,7 +140,10 @@ class PingController {
version version
} }
if (--remainingTasks === 0) { if (Object.keys(results).length === this._app.serverRegistrations.length) {
// Loop has completed, release the locking flag
this._isRunningTasks = false
callback(results) callback(results)
} }
}, version.protocolId) }, version.protocolId)

@ -223,11 +223,7 @@ class ServerRegistration {
// Generate an updated hash // Generate an updated hash
// This is used by #getFaviconUrl // This is used by #getFaviconUrl
if (!this._faviconHasher) { this.faviconHash = crypto.createHash('md5').update(favicon).digest('hex').toString()
this._faviconHasher = crypto.createHash('md5')
}
this.faviconHash = this._faviconHasher.update(favicon).digest('hex').toString()
return true return true
} }

10
package-lock.json generated

@ -5495,11 +5495,6 @@
"object-visit": "^1.0.0" "object-visit": "^1.0.0"
} }
}, },
"mc-ping-updated": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/mc-ping-updated/-/mc-ping-updated-0.1.1.tgz",
"integrity": "sha512-wYX4B41Jog+RvLFgYPytNvbw+b9mjjsKKqMLJ57a7aH3VgJCNzk6qeWFCTz7FLCWTEk8F81RBqh8kMPCjUVdNw=="
},
"mcpe-ping-fixed": { "mcpe-ping-fixed": {
"version": "0.0.3", "version": "0.0.3",
"resolved": "https://registry.npmjs.org/mcpe-ping-fixed/-/mcpe-ping-fixed-0.0.3.tgz", "resolved": "https://registry.npmjs.org/mcpe-ping-fixed/-/mcpe-ping-fixed-0.0.3.tgz",
@ -5509,6 +5504,11 @@
"portfinder": "^0.4.0" "portfinder": "^0.4.0"
} }
}, },
"mcping-js": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/mcping-js/-/mcping-js-1.2.1.tgz",
"integrity": "sha512-s0MRsyDnaxhG0lt1JqJl6tBewZkRj0fzeq/oU/aPSF74PrFW/ZWyzNOgh+NO5hY84U2XnRnscAZ0A0MIIEvC4A=="
},
"md5.js": { "md5.js": {
"version": "1.3.5", "version": "1.3.5",
"resolved": "https://registry.npmjs.org/md5.js/-/md5.js-1.3.5.tgz", "resolved": "https://registry.npmjs.org/md5.js/-/md5.js-1.3.5.tgz",

@ -5,8 +5,8 @@
"main": "main.js", "main": "main.js",
"dependencies": { "dependencies": {
"finalhandler": "^1.1.2", "finalhandler": "^1.1.2",
"mc-ping-updated": "0.1.1",
"mcpe-ping-fixed": "0.0.3", "mcpe-ping-fixed": "0.0.3",
"mcping-js": "^1.2.1",
"request": "2.88.2", "request": "2.88.2",
"serve-static": "^1.14.1", "serve-static": "^1.14.1",
"sqlite3": "4.1.1", "sqlite3": "4.1.1",