Skip to content

Commit

Permalink
Handle Ajax Request timeouts issue #78
Browse files Browse the repository at this point in the history
  • Loading branch information
swilly22 authored and joeyparrish committed May 22, 2015
1 parent d0192a1 commit 28c017b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ Sanborn Hilland <[email protected]>
Timothy Drews <[email protected]>
Vasanth Polipelli <[email protected]>
Vignesh Venkatasubramanian <[email protected]>
Roi Lipman <[email protected]>
33 changes: 33 additions & 0 deletions lib/util/ajax_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ shaka.util.AjaxRequest.prototype.cleanupRequest_ = function() {
this.xhr_.onload = null;
this.xhr_.onreadystatechange = null;
this.xhr_.onerror = null;
this.xhr_.ontimeout = null;
}
this.xhr_ = null;
};
Expand Down Expand Up @@ -254,6 +255,7 @@ shaka.util.AjaxRequest.prototype.sendInternal = function() {
this.xhr_.onreadystatechange = this.onReadyStateChange_.bind(this);
}
this.xhr_.onerror = this.onError_.bind(this);
this.xhr_.ontimeout = this.onTimeout_.bind(this);

for (var k in this.parameters.requestHeaders) {
this.xhr_.setRequestHeader(k, this.parameters.requestHeaders[k]);
Expand Down Expand Up @@ -480,3 +482,34 @@ shaka.util.AjaxRequest.prototype.onError_ = function(event) {
this.destroy_();
};

/**
* Handles a "timeout" event.
*
* @param {!ProgressEvent} event The ProgressEvent from this.xhr_.
*
* @private
*/
shaka.util.AjaxRequest.prototype.onTimeout_ = function(event) {
if (this.attempts_ < this.parameters.maxAttempts) {
this.cleanupRequest_();

var sendAgain = this.sendInternal.bind(this);

// Fuzz the delay to avoid tons of clients hitting the server at once
// after it recovers from whatever is causing it to fail.
var negToPosOne = (Math.random() * 2.0) - 1.0;
var negToPosFuzzFactor = negToPosOne * this.parameters.retryFuzzFactor;
var fuzzedDelay = this.retryDelayMs_ * (1.0 + negToPosFuzzFactor);
window.setTimeout(sendAgain, fuzzedDelay);

// Store the fuzzed delay to make testing retries feasible.
this.lastDelayMs_ = fuzzedDelay;

// Back off the next delay.
this.retryDelayMs_ *= this.parameters.retryBackoffFactor;
} else {
var error = this.createError_('Request timmedout.', 'net');
this.promise_.reject(error);
this.destroy_();
}
}

0 comments on commit 28c017b

Please sign in to comment.