summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authoraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-12-16 09:20:26 +0000
committeraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-12-16 09:20:26 +0000
commitd5428e97a7a6f52931f495f62bc80879e8dd6590 (patch)
tree8779913dde6b72a68865edf7300b9bad4ca0be2d /chrome
parent07619be239c31bc153b13db47594188b25f07fad (diff)
downloadchromium_src-d5428e97a7a6f52931f495f62bc80879e8dd6590.zip
chromium_src-d5428e97a7a6f52931f495f62bc80879e8dd6590.tar.gz
chromium_src-d5428e97a7a6f52931f495f62bc80879e8dd6590.tar.bz2
Merge 33946 - Add exponential backoff and a random factor to request scheduling in the Google Mail checker sample extension.
BUG=29540 Review URL: http://codereview.chromium.org/465092 TBR=aa@chromium.org Review URL: http://codereview.chromium.org/501052 git-svn-id: svn://svn.chromium.org/chrome/branches/249/src@34698 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/common/extensions/docs/examples/extensions/gmail/background.html54
1 files changed, 36 insertions, 18 deletions
diff --git a/chrome/common/extensions/docs/examples/extensions/gmail/background.html b/chrome/common/extensions/docs/examples/extensions/gmail/background.html
index d40e926..9b188e3 100644
--- a/chrome/common/extensions/docs/examples/extensions/gmail/background.html
+++ b/chrome/common/extensions/docs/examples/extensions/gmail/background.html
@@ -9,7 +9,9 @@ var gmail = "https://mail.google.com/";
var gmailAtomRef = "https://mail.google.com/mail/feed/atom";
var gmailRe = /https?\:\/\/mail.google.com\/(?!a\/)/;
var loggedInImage;
-var pollInterval = 1000 * 60; // 1 minute
+var pollIntervalMin = 1000 * 60; // 1 minute
+var pollIntervalMax = 1000 * 60 * 60; // 1 hour
+var requestFailureCount = 0; // used for exponential backoff
var requestTimeout = 1000 * 2; // 5 seconds
var rotation = 0;
var unreadCount = -1;
@@ -81,7 +83,18 @@ function init() {
}
function scheduleRequest() {
- window.setTimeout(startRequest, pollInterval);
+ var randomness = Math.random() * 2;
+ var exponent = Math.pow(2, requestFailureCount);
+ var delay = Math.min(randomness * pollIntervalMin * exponent,
+ pollIntervalMax);
+ delay = Math.round(delay);
+
+ console.log("Scheduling request...");
+ console.log(" exponent: ", exponent);
+ console.log(" randomness: ", randomness);
+ console.log(" delay: ", delay);
+
+ window.setTimeout(startRequest, delay);
}
// ajax stuff
@@ -108,12 +121,14 @@ function getInboxCount(onSuccess, onError) {
}, requestTimeout);
function handleSuccess(count) {
+ requestFailureCount = 0;
window.clearTimeout(abortTimerId);
if (onSuccess)
onSuccess(count);
}
function handleError() {
+ ++requestFailureCount;
window.clearTimeout(abortTimerId);
if (onError)
onError();
@@ -123,26 +138,29 @@ function getInboxCount(onSuccess, onError) {
console.log("request..");
xhr.onreadystatechange = function(){
console.log("readystate: " + xhr.readyState);
- if (xhr.readyState == 4) {
- console.log("responseText: " + xhr.responseText.substring(0, 200) +
+ if (xhr.readyState != 4)
+ return;
+
+ console.log("responseText: " + xhr.responseText.substring(0, 200) +
"...");
- if (xhr.responseXML) {
- var xmlDoc = xhr.responseXML;
- var fullCountSet = xmlDoc.evaluate("/gmail:feed/gmail:fullcount",
- xmlDoc, gmailNSResolver, XPathResult.ANY_TYPE, null);
- var fullCountNode = fullCountSet.iterateNext();
- if (fullCountNode) {
- handleSuccess(fullCountNode.textContent);
- } else {
- console.log("fullcount not found!");
- console.error("Error: feed retrieved, but no <fullcount> node " +
- "found");
- handleError();
- }
+ if (xhr.responseXML) {
+ var xmlDoc = xhr.responseXML;
+ var fullCountSet = xmlDoc.evaluate("/gmail:feed/gmail:fullcount",
+ xmlDoc, gmailNSResolver, XPathResult.ANY_TYPE, null);
+ var fullCountNode = fullCountSet.iterateNext();
+ if (fullCountNode) {
+ handleSuccess(fullCountNode.textContent);
+ return;
} else {
- console.log("No responseXML!");
+ console.log("fullcount not found!");
+ console.error("Error: feed retrieved, but no <fullcount> node " +
+ "found");
}
+ } else {
+ console.log("No responseXML!");
}
+
+ handleError();
}
xhr.onerror = function(error) {