jenkins-bot has submitted this change and it was merged.

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.

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

Approvals:
  Ori.livneh: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/server/eventlogging/utils.py b/server/eventlogging/utils.py
index 5fcc5fe..8c441ad 100644
--- a/server/eventlogging/utils.py
+++ b/server/eventlogging/utils.py
@@ -12,12 +12,14 @@
 import re
 import threading
 
+from .compat import monotonic_clock
+
 
 __all__ = ('PeriodicThread', 'uri_delete_query_item')
 
 
 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
@@ -27,12 +29,24 @@
 
     def run(self):
         while not self.stopping.is_set():
-            if self.ready.wait(self.interval):
+            # Run the target function. Check the clock before
+            # and after to determine how long it took to run.
+            time_start = monotonic_clock()
+            self._Thread__target(*self._Thread__args, **self._Thread__kwargs)
+            time_stop = monotonic_clock()
+
+            run_duration = time_stop - time_start
+
+            # Subtract the time it took the target function to run
+            # from the desired run interval. The result is how long
+            # we have to sleep before the next run.
+            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)
 
     def stop(self):
         """Graceful stop: stop once the current iteration is complete."""

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ib746a2571ea25058de49caeae9aad83ec4c32157
Gerrit-PatchSet: 6
Gerrit-Project: mediawiki/extensions/EventLogging
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <o...@wikimedia.org>
Gerrit-Reviewer: Nuria <nu...@wikimedia.org>
Gerrit-Reviewer: Ori.livneh <o...@wikimedia.org>
Gerrit-Reviewer: QChris <christ...@quelltextlich.at>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to