Smarter pinging. Don't waster resources #3240

This commit is contained in:
Nicolas Gotchac 2016-11-17 19:20:58 +01:00
parent 868fdd68ab
commit 5fd66d2c97
2 changed files with 41 additions and 3 deletions

View File

@ -84,7 +84,7 @@ export default class Ws extends JsonRpcBase {
this._connecting = false; this._connecting = false;
if (this._autoConnect) { if (this._autoConnect) {
this._connect(); setTimeout(() => this._connect(), 500);
} }
} }

View File

@ -25,6 +25,8 @@ export default class Status {
this._pingable = false; this._pingable = false;
this._apiStatus = {}; this._apiStatus = {};
this._status = {}; this._status = {};
this._pollPingTimeoutId = null;
} }
start () { start () {
@ -64,14 +66,43 @@ export default class Status {
}); });
} }
/**
* Pinging should be smart. It should only
* be used when the UI is connecting or the
* Node is deconnected.
*
* @see src/views/Connection/connection.js
*/
_shouldPing = () => {
const { isConnected, isConnecting } = this._apiStatus;
return isConnecting || !isConnected;
}
_stopPollPing = () => {
if (!this._pollPingTimeoutId) {
return;
}
clearTimeout(this._pollPingTimeoutId);
this._pollPingTimeoutId = null;
}
_pollPing = () => { _pollPing = () => {
const dispatch = (pingable, timeout = 500) => { // Already pinging, don't try again
if (this._pollPingTimeoutId) {
return;
}
const dispatch = (pingable, timeout = 1000) => {
if (pingable !== this._pingable) { if (pingable !== this._pingable) {
this._pingable = pingable; this._pingable = pingable;
this._store.dispatch(statusCollection({ isPingable: pingable })); this._store.dispatch(statusCollection({ isPingable: pingable }));
} }
setTimeout(this._pollPing, timeout); this._pollPingTimeoutId = setTimeout(() => {
this._stopPollPing();
this._pollPing();
}, timeout);
}; };
fetch('/', { method: 'HEAD' }) fetch('/', { method: 'HEAD' })
@ -108,6 +139,13 @@ export default class Status {
this._apiStatus = apiStatus; this._apiStatus = apiStatus;
} }
// Ping if necessary, otherwise stop pinging
if (this._shouldPing()) {
this._pollPing();
} else {
this._stopPollPing();
}
if (!isConnected) { if (!isConnected) {
nextTimeout(250); nextTimeout(250);
return; return;