Bug 1353731 - Only attempt to reset timer every per osfile.reset_worker_delay. r?Yoric draft
authorKan-Ru Chen <kanru@kanru.info>
Tue, 27 Jun 2017 16:04:51 -0700
changeset 601297 25cd38663b7be13a2a3d5612bc883b87ad479035
parent 600694 8f80d594c08d5c7a112e5d4b9eb44ffca717eb7b
child 635200 28341a8808f027da88bf9b8c4f7ec60cd34863c2
push id65997
push userbmo:kchen@mozilla.com
push dateWed, 28 Jun 2017 16:35:08 +0000
reviewersYoric
bugs1353731
milestone56.0a1
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
toolkit/components/osfile/modules/osfile_async_front.jsm
--- 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.