Add socketTimeout and attemptTimeout

This commit is contained in:
mmorrison 2018-01-31 04:03:13 -06:00
parent 3a96e00508
commit b5f49fe343
3 changed files with 25 additions and 17 deletions

View file

@ -45,13 +45,22 @@ function(e,state) {
> npm install sonicsnes/node-gamedig > npm install sonicsnes/node-gamedig
> ``` > ```
### Input Parameters ### Query Options
**Typical**
* **type**: One of the game IDs listed in the game list below * **type**: One of the game IDs listed in the game list below
* **host** * **host**: Hostname or IP of the game server
* **port**: (optional) Uses the protocol default if not set * **port**: (optional) Uses the protocol default if not set
* **notes**: (optional) Passed through to output
* **maxAttempts**: (optional) Number of attempts to query server in case of failure (default 1) **Advanced**
* **notes**: (optional) An object passed through in the return value.
* **maxAttempts**: (optional) Number of attempts to query server in case of failure. (default 1)
* **socketTimeout**: (optional) Milliseconds to wait for a single packet. Beware that increasing this
will cause many queries to take longer even if the server is online. (default 1000)
* **attemptTimeout**: (optional) Milliseconds allowed for an entire query attempt. This timeout is not commonly hit,
as the socketTimeout typically fires first. (default 10000)
### Return Value ### Return Value

View file

@ -9,8 +9,8 @@ class Core extends EventEmitter {
constructor() { constructor() {
super(); super();
this.options = { this.options = {
tcpTimeout: 1000, socketTimeout: 1000,
udpTimeout: 1000, attemptTimeout: 10000,
maxAttempts: 1 maxAttempts: 1
}; };
this.attempt = 1; this.attempt = 1;
@ -19,10 +19,7 @@ class Core extends EventEmitter {
this.byteorder = 'le'; this.byteorder = 'le';
this.delimiter = '\0'; this.delimiter = '\0';
this.srvRecord = null; this.srvRecord = null;
this.attemptTimeoutTimer = null;
this.globalTimeoutTimer = setTimeout(() => {
this.fatal('timeout');
},10000);
} }
fatal(err,noretry) { fatal(err,noretry) {
@ -58,7 +55,6 @@ class Core extends EventEmitter {
done(state) { done(state) {
if(this.finished) return; if(this.finished) return;
clearTimeout(this.globalTimeoutTimer);
if(this.options.notes) if(this.options.notes)
state.notes = this.options.notes; state.notes = this.options.notes;
@ -71,6 +67,7 @@ class Core extends EventEmitter {
state.query.type = this.type; state.query.type = this.type;
if('pretty' in this) state.query.pretty = this.pretty; if('pretty' in this) state.query.pretty = this.pretty;
state.query.duration = Date.now() - this.startMillis; state.query.duration = Date.now() - this.startMillis;
state.query.attempts = this.attempt;
this.reset(); this.reset();
this.finished = true; this.finished = true;
@ -79,6 +76,7 @@ class Core extends EventEmitter {
} }
reset() { reset() {
clearTimeout(this.attemptTimeoutTimer);
if(this.timers) { if(this.timers) {
for (const timer of this.timers) { for (const timer of this.timers) {
clearTimeout(timer); clearTimeout(timer);
@ -101,6 +99,10 @@ class Core extends EventEmitter {
this.startMillis = Date.now(); this.startMillis = Date.now();
this.attemptTimeoutTimer = setTimeout(() => {
this.fatal('timeout');
},this.options.attemptTimeout);
async.series([ async.series([
(c) => { (c) => {
// resolve host names // resolve host names
@ -211,7 +213,6 @@ class Core extends EventEmitter {
connected = true; connected = true;
c(socket); c(socket);
}); });
socket.setTimeout(10000);
socket.setNoDelay(true); socket.setNoDelay(true);
if(this.debug) console.log(address+':'+port+" TCPCONNECT"); if(this.debug) console.log(address+':'+port+" TCPCONNECT");
@ -255,13 +256,11 @@ class Core extends EventEmitter {
this.tcpTimeoutTimer = this.setTimeout(() => { this.tcpTimeoutTimer = this.setTimeout(() => {
this.tcpCallback = false; this.tcpCallback = false;
this.fatal('TCP Watchdog Timeout'); this.fatal('TCP Watchdog Timeout');
},this.options.tcpTimeout); },this.options.socketTimeout);
this.tcpCallback = ondata; this.tcpCallback = ondata;
}); });
} }
udpSend(buffer,onpacket,ontimeout) { udpSend(buffer,onpacket,ontimeout) {
process.nextTick(() => { process.nextTick(() => {
if(this.udpCallback) return this.fatal('Attempted to send UDP packet while still waiting on a managed response'); if(this.udpCallback) return this.fatal('Attempted to send UDP packet while still waiting on a managed response');
@ -273,7 +272,7 @@ class Core extends EventEmitter {
let timeout = false; let timeout = false;
if(!ontimeout || ontimeout() !== true) timeout = true; if(!ontimeout || ontimeout() !== true) timeout = true;
if(timeout) this.fatal('UDP Watchdog Timeout'); if(timeout) this.fatal('UDP Watchdog Timeout');
},this.options.udpTimeout); },this.options.socketTimeout);
this.udpCallback = onpacket; this.udpCallback = onpacket;
}); });
} }

View file

@ -1,7 +1,7 @@
class Mumble extends require('./core') { class Mumble extends require('./core') {
constructor() { constructor() {
super(); super();
this.options.tcpTimeout = 5000; this.options.socketTimeout = 5000;
} }
run(state) { run(state) {