Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(499)

Unified Diff: chrome/browser/resources/google_now/utility.js

Issue 14743007: Retries with exponential backoff for dismissals (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cosmetics Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/resources/google_now/manifest.json ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/resources/google_now/utility.js
diff --git a/chrome/browser/resources/google_now/utility.js b/chrome/browser/resources/google_now/utility.js
index d0153bcc4c816bf22eccdda08914766e26d2efff..95f10612bc0c3be9aff98a000d8eb120091c8dca 100644
--- a/chrome/browser/resources/google_now/utility.js
+++ b/chrome/browser/resources/google_now/utility.js
@@ -221,3 +221,99 @@ function buildTaskManager(areConflicting) {
wrapCallback: wrapCallback
};
}
+
+var storage = chrome.storage.local;
+
+/**
+ * Builds an object to manage retrying activities with exponential backoff.
+ * @param {string} name Name of this attempt manager.
+ * @param {function()} attempt Activity that the manager retries till it
+ * calls 'stop' method.
+ * @param {number} initialDelaySeconds Default first delay until first retry.
+ * @param {number} maximumDelaySeconds Maximum delay between retries.
+ * @return {Object} Attempt manager interface.
+ */
+function buildAttemptManager(
+ name, attempt, initialDelaySeconds, maximumDelaySeconds) {
+ var alarmName = name + '-scheduler';
+ var currentDelayStorageKey = name + '-current-delay';
+
+ /**
+ * Creates an alarm for the next attempt. The alarm is repeating for the case
+ * when the next attempt crashes before registering next alarm.
+ * @param {number} delaySeconds Delay until next retry.
+ */
+ function createAlarm(delaySeconds) {
+ var alarmInfo = {
+ delayInMinutes: delaySeconds / 60,
+ periodInMinutes: maximumDelaySeconds / 60
+ };
+ chrome.alarms.create(alarmName, alarmInfo);
+ }
+
+ /**
+ * Schedules next attempt.
+ * @param {number=} optionalPreviousDelaySeconds Previous delay in a sequence
+ * of retry attemps, if specified. Not specified for scheduling first
+ * retry in the exponential sequence.
+ */
+ function scheduleNextAttempt(optionalPreviousDelaySeconds) {
+ var base = optionalPreviousDelaySeconds ? optionalPreviousDelaySeconds * 2 :
+ initialDelaySeconds;
+ var newRetryDelaySeconds =
+ Math.min(base * (1 + 0.2 * Math.random()), maximumDelaySeconds);
+
+ createAlarm(newRetryDelaySeconds);
+
+ var items = {};
+ items[currentDelayStorageKey] = newRetryDelaySeconds;
+ storage.set(items);
skare_ 2013/05/11 00:56:23 this is just syntax but either var items = {cDSK:
vadimt 2013/05/11 01:55:15 This would create a field 'currentDelayStorageKey'
skare_ 2013/05/11 02:07:19 [ok] oops, thought this was a literal. Might want
+ }
+
+ /**
+ * Starts repeated attempts.
+ * @param {number=} optionalFirstDelaySeconds Time until the first attempt, if
+ * specified. Otherwise, initialDelaySeconds will be used for the first
+ * attempt.
+ */
+ function start(optionalFirstDelaySeconds) {
skare_ 2013/05/11 00:56:23 opt_FirstDelaySeconds (s/optional/opt_ unless some
skare_ 2013/05/11 01:03:05 typo -- opt_firstDelaySeconds actually.
vadimt 2013/05/11 01:55:15 Done.
+ if (optionalFirstDelaySeconds) {
+ createAlarm(optionalFirstDelaySeconds);
+ storage.remove(currentDelayStorageKey);
+ } else {
+ scheduleNextAttempt();
+ }
+ }
+
+ /**
+ * Stops repeated attempts.
+ */
+ function stop() {
+ chrome.alarms.clear(alarmName);
+ storage.remove(currentDelayStorageKey);
+ }
+
+ /**
+ * Plans for the next attempt.
+ * @param {function()} callback Completion callback.
+ */
+ function planForNext(callback) {
+ tasks.debugSetStepName('planForNext-get-storage');
+ storage.get(currentDelayStorageKey, function(items) {
+ console.log('planForNext-get-storage ' + JSON.stringify(items));
+ scheduleNextAttempt(items[currentDelayStorageKey]);
+ callback();
+ });
+ }
+
+ chrome.alarms.onAlarm.addListener(function(alarm) {
+ if (alarm.name == alarmName)
+ attempt();
+ });
+
+ return {
+ start: start,
+ planForNext: planForNext,
+ stop: stop
+ };
+}
« no previous file with comments | « chrome/browser/resources/google_now/manifest.json ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698