Merge branch 'master' of https://github.com/Cryptkeeper/Minetrack into uplot

This commit is contained in:
Nick Krecklow
2020-05-14 20:50:51 -05:00
9 changed files with 97 additions and 76 deletions

View File

@ -1,9 +1,9 @@
const crypto = require('crypto')
const dns = require('dns')
const { GRAPH_UPDATE_TIME_GAP, TimeTracker } = require('./time')
const DNSResolver = require('./dns')
const Server = require('./server')
const { GRAPH_UPDATE_TIME_GAP, TimeTracker } = require('./time')
const { getPlayerCountOrNull } = require('./util')
const config = require('../config')
@ -21,6 +21,7 @@ class ServerRegistration {
this.serverId = serverId
this.data = data
this._pingHistory = []
this.dnsResolver = new DNSResolver(this.data.ip, this.data.port)
}
handlePing (timestamp, resp, err, version, updateHistoryGraph) {
@ -254,62 +255,6 @@ class ServerRegistration {
color: this.data.color
}
}
unfurlSrv (callback) {
// Skip unfurling SRV, instantly return pre-configured data
if (config.performance && config.performance.skipUnfurlSrv) {
callback(this.data.ip, this.data.port)
return
}
const timestamp = new Date().getTime()
// If a cached copy exists and is within its TTL, instantly return
if (this._lastUnfurlSrv && timestamp - this._lastUnfurlSrv.timestamp <= config.performance.unfurlSrvCacheTtl) {
callback(this._lastUnfurlSrv.ip, this._lastUnfurlSrv.port)
return
}
// Group callbacks into an array
// Once resolved, fire callbacks sequentially
// This avoids callbacks possibly executing out of order
if (!this._unfurlSrvCallbackQueue) {
this._unfurlSrvCallbackQueue = []
}
this._unfurlSrvCallbackQueue.push(callback)
// Prevent multiple #resolveSrv calls per ServerRegistration
if (!this._isUnfurlingSrv) {
this._isUnfurlingSrv = true
dns.resolveSrv('_minecraft._tcp' + this.data.ip, (_, records) => {
this._lastUnfurlSrv = {
timestamp
}
if (records && records.length > 0) {
this._lastUnfurlSrv.ip = records[0].name
this._lastUnfurlSrv.port = records[0].port
} else {
// Provide fallback to pre-configured data
this._lastUnfurlSrv.ip = this.data.ip
this._lastUnfurlSrv.port = this.data.port
}
// Fire the waiting callbacks in queue
// Release blocking flag to allow new #resolveSrv calls
this._isUnfurlingSrv = false
for (const callback of this._unfurlSrvCallbackQueue) {
callback(this._lastUnfurlSrv.ip, this._lastUnfurlSrv.port)
}
// Reset the callback queue
this._unfurlSrvCallbackQueue = []
})
}
}
}
module.exports = ServerRegistration