From 7d978ca131d6a0bf6109bdee6de749b11b2404b2 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sat, 13 May 2017 23:12:55 -0700 Subject: [PATCH] Prevent callbacks from many polling requests being queued for the same host --- static/js/utils.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/static/js/utils.js b/static/js/utils.js index b20e927..31f1daa 100644 --- a/static/js/utils.js +++ b/static/js/utils.js @@ -52,6 +52,8 @@ function NvHTTP(address, clientUid, userEnteredAddress = '') { this.numofapps = 0; this.hostname = address; this.externalIP = ''; + this._pollCompletionCallbacks = []; + _self = this; }; @@ -104,6 +106,16 @@ NvHTTP.prototype = { // called every few seconds to poll the server for updated info pollServer: function(onComplete) { + // Pend this callback on completion + this._pollCompletionCallbacks.push(onComplete); + + // Check if a poll was already in progress + if (this._pollCompletionCallbacks.length > 1) { + // Don't start another. The one in progress will + // alert our caller too. + return; + } + this.selectServerAddress(function(successfulAddress) { // Successfully determined server address. Update base URL. this.address = successfulAddress; @@ -120,13 +132,21 @@ NvHTTP.prototype = { this._consecutivePollFailures = 0; this.online = true; - onComplete(this); + // Call all pending completion callbacks + var completion; + while ((completion = this._pollCompletionCallbacks.pop())) { + completion(this); + } }.bind(this), function() { if (++this._consecutivePollFailures >= 3) { this.online = false; } - onComplete(this); + // Call all pending completion callbacks + var completion; + while ((completion = this._pollCompletionCallbacks.pop())) { + completion(this); + } }.bind(this)); },