Ori.livneh has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/175322

Change subject: Make PeriodicThread more regular
......................................................................

Make PeriodicThread more regular

Right now, PeriodicThread will sleep the full duration of the interval
regardless of how long the target function takes to complete. This can make the
queue back up precipitously during high load. Mitigate this by subtracting the
run-time of the target function from the sleep interval.

Use abs() to prevent the interval calculation from being thrown off by clock
adjustments. (If we were using Python 3.3+, we'd use time.monotonic() instead.)
Because Event.wait() treats negative timeouts as zero, if the target function
takes longer than the run interval to complete, the next run will start
immediately.

Change-Id: Ib746a2571ea25058de49caeae9aad83ec4c32157
---
M server/eventlogging/utils.py
1 file changed, 10 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/EventLogging 
refs/changes/22/175322/1

diff --git a/server/eventlogging/utils.py b/server/eventlogging/utils.py
index 660c397..e9763bd 100644
--- a/server/eventlogging/utils.py
+++ b/server/eventlogging/utils.py
@@ -10,13 +10,14 @@
 from __future__ import unicode_literals
 
 import threading
+import time
 
 
 __all__ = ('PeriodicThread',)
 
 
 class PeriodicThread(threading.Thread):
-    """Represents a threaded job that runs repeatedly at regular intervals."""
+    """Represents a threaded job that runs repeatedly at a regular interval."""
 
     def __init__(self, interval, *args, **kwargs):
         self.interval = interval
@@ -25,9 +26,15 @@
 
     def run(self):
         while 1:
-            if self.ready.wait(self.interval):
+            run_start = time.time()
+            self._Thread__target(*self._Thread__args, **self._Thread__kwargs)
+            run_stop = time.time()
+
+            run_duration = abs(run_stop - run_start)
+            time_to_next_run = self.interval - run_duration
+
+            if self.ready.wait(time_to_next_run):
                 # If the internal flag of `self.ready` was set, we were
                 # interrupted mid-nap to run immediately. But before we
                 # do, we reset the flag.
                 self.ready.clear()
-            self._Thread__target(*self._Thread__args, **self._Thread__kwargs)

-- 
To view, visit https://gerrit.wikimedia.org/r/175322
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib746a2571ea25058de49caeae9aad83ec4c32157
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/EventLogging
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <o...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to