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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/resources/google_now/manifest.json ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // 'use strict'; TODO(vadimt): Uncomment once crbug.com/237617 is fixed. 5 // 'use strict'; TODO(vadimt): Uncomment once crbug.com/237617 is fixed.
6 6
7 // TODO(vadimt): Remove alerts. 7 // TODO(vadimt): Remove alerts.
8 8
9 /** 9 /**
10 * @fileoverview Utility objects and functions for Google Now extension. 10 * @fileoverview Utility objects and functions for Google Now extension.
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 }); 214 });
215 215
216 return { 216 return {
217 add: add, 217 add: add,
218 // TODO(vadimt): Replace with instrumenting callbacks. 218 // TODO(vadimt): Replace with instrumenting callbacks.
219 debugSetStepName: debugSetStepName, 219 debugSetStepName: debugSetStepName,
220 instrumentApiFunction: instrumentApiFunction, 220 instrumentApiFunction: instrumentApiFunction,
221 wrapCallback: wrapCallback 221 wrapCallback: wrapCallback
222 }; 222 };
223 } 223 }
224
225 var storage = chrome.storage.local;
226
227 /**
228 * Builds an object to manage retrying activities with exponential backoff.
229 * @param {string} name Name of this attempt manager.
230 * @param {function()} attempt Activity that the manager retries until it
231 * calls 'stop' method.
232 * @param {number} initialDelaySeconds Default first delay until first retry.
233 * @param {number} maximumDelaySeconds Maximum delay between retries.
234 * @return {Object} Attempt manager interface.
235 */
236 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
237 name, attempt, initialDelaySeconds, maximumDelaySeconds) {
238 var alarmName = name + '-scheduler';
239 var currentDelayStorageKey = name + '-current-delay';
240
241 /**
242 * Creates an alarm for the next attempt. The alarm is repeating for the case
243 * when the next attempt crashes before registering next alarm.
244 * @param {number} delaySeconds Delay until next retry.
245 */
246 function createAlarm(delaySeconds) {
247 var alarmInfo = {
248 delayInMinutes: delaySeconds / 60,
249 periodInMinutes: maximumDelaySeconds / 60
250 };
251 chrome.alarms.create(alarmName, alarmInfo);
252 }
253
254 /**
255 * Schedules next attempt.
256 * @param {number=} opt_previousDelaySeconds Previous delay in a sequence of
257 * retry attempts, if specified. Not specified for scheduling first retry
258 * in the exponential sequence.
259 */
260 function scheduleNextAttempt(opt_previousDelaySeconds) {
261 var base = opt_previousDelaySeconds ? opt_previousDelaySeconds * 2 :
262 initialDelaySeconds;
263 var newRetryDelaySeconds =
264 Math.min(base * (1 + 0.2 * Math.random()), maximumDelaySeconds);
265
266 createAlarm(newRetryDelaySeconds);
267
268 var items = {};
269 items[currentDelayStorageKey] = newRetryDelaySeconds;
270 storage.set(items);
271 }
272
273 /**
274 * Starts repeated attempts.
275 * @param {number=} opt_firstDelaySeconds Time until the first attempt, if
276 * specified. Otherwise, initialDelaySeconds will be used for the first
277 * attempt.
278 */
279 function start(opt_firstDelaySeconds) {
280 if (opt_firstDelaySeconds) {
281 createAlarm(opt_firstDelaySeconds);
282 storage.remove(currentDelayStorageKey);
283 } else {
284 scheduleNextAttempt();
285 }
286 }
287
288 /**
289 * Stops repeated attempts.
290 */
291 function stop() {
292 chrome.alarms.clear(alarmName);
293 storage.remove(currentDelayStorageKey);
294 }
295
296 /**
297 * Plans for the next attempt.
298 * @param {function()} callback Completion callback. It will be invoked after
299 * the planning is done.
300 */
301 function planForNext(callback) {
302 tasks.debugSetStepName('planForNext-get-storage');
303 storage.get(currentDelayStorageKey, function(items) {
304 console.log('planForNext-get-storage ' + JSON.stringify(items));
305 scheduleNextAttempt(items[currentDelayStorageKey]);
306 callback();
307 });
308 }
309
310 chrome.alarms.onAlarm.addListener(function(alarm) {
311 if (alarm.name == alarmName)
312 attempt();
313 });
314
315 return {
316 start: start,
317 planForNext: planForNext,
318 stop: stop
319 };
320 }
OLDNEW
« 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