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

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: Typo. 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..150dd41a8d66886c77733d8e5442d9c755a3e3a6 100644
--- a/chrome/browser/resources/google_now/utility.js
+++ b/chrome/browser/resources/google_now/utility.js
@@ -221,3 +221,100 @@ 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 until 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(
arv (Not doing code reviews) 2013/05/15 17:25:36 In general build and create functions should be re
vadimt 2013/05/15 19:05:18 Ack
+ 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=} opt_previousDelaySeconds Previous delay in a sequence of
+ * retry attempts, if specified. Not specified for scheduling first retry
+ * in the exponential sequence.
+ */
+ function scheduleNextAttempt(opt_previousDelaySeconds) {
+ var base = opt_previousDelaySeconds ? opt_previousDelaySeconds * 2 :
+ initialDelaySeconds;
+ var newRetryDelaySeconds =
+ Math.min(base * (1 + 0.2 * Math.random()), maximumDelaySeconds);
+
+ createAlarm(newRetryDelaySeconds);
+
+ var items = {};
+ items[currentDelayStorageKey] = newRetryDelaySeconds;
+ storage.set(items);
+ }
+
+ /**
+ * Starts repeated attempts.
+ * @param {number=} opt_firstDelaySeconds Time until the first attempt, if
+ * specified. Otherwise, initialDelaySeconds will be used for the first
+ * attempt.
+ */
+ function start(opt_firstDelaySeconds) {
+ if (opt_firstDelaySeconds) {
+ createAlarm(opt_firstDelaySeconds);
+ 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. It will be invoked after
+ * the planning is done.
+ */
+ 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