Bug 1353731 - Only attempt to reset timer every per osfile.reset_worker_delay. r?Yoric
After this patch for every restartTimer() call we only set a flag to
indicating that we should reset the timer when the timer fires.
MozReview-Commit-ID: GRInHxuEEQM
--- a/toolkit/components/osfile/modules/osfile_async_front.jsm
+++ b/toolkit/components/osfile/modules/osfile_async_front.jsm
@@ -213,54 +213,61 @@ var Scheduler = this.Scheduler = {
},
/**
* A timer used to automatically shut down the worker after some time.
*/
resetTimer: null,
/**
+ * A flag indicating whether we had some activities when waiting the
+ * timer and if it's not we can shut down the worker.
+ */
+ hasRecentActivity: false,
+
+ /**
* The worker to which to send requests.
*
* If the worker has never been created or has been reset, this is a
* fresh worker, initialized with osfile_async_worker.js.
*
* @type {PromiseWorker}
*/
get worker() {
if (!this._worker) {
// Either the worker has never been created or it has been
// reset. In either case, it is time to instantiate the worker.
this._worker = new BasePromiseWorker("resource://gre/modules/osfile/osfile_async_worker.js");
this._worker.log = LOG;
this._worker.ExceptionHandlers["OS.File.Error"] = OSError.fromMsg;
+
+ let delay = Services.prefs.getIntPref("osfile.reset_worker_delay", 0);
+ if (delay) {
+ this.resetTimer = setInterval(
+ () => {
+ if (this.hasRecentActivity) {
+ this.hasRecentActivity = false;
+ return;
+ }
+ clearInterval(this.resetTimer);
+ Scheduler.kill({reset: true, shutdown: false});
+ },
+ delay);
+ }
}
return this._worker;
},
_worker: null,
/**
- * Prepare to kill the OS.File worker after a few seconds.
+ * Restart the OS.File worker killer timer.
*/
restartTimer: function(arg) {
- let delay = Services.prefs.getIntPref("osfile.reset_worker_delay", 0);
-
- if (!delay) {
- // Don't auto-shutdown if we don't have a delay preference set.
- return;
- }
-
- if (this.resetTimer) {
- clearTimeout(this.resetTimer);
- }
- this.resetTimer = setTimeout(
- () => Scheduler.kill({reset: true, shutdown: false}),
- delay
- );
+ this.hasRecentActivity = true;
},
/**
* Shutdown OS.File.
*
* @param {*} options
* - {boolean} shutdown If |true|, reject any further request. Otherwise,
* further requests will resurrect the worker.