summary refs log tree commit diff
path: root/synapse
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2015-08-14 15:42:52 +0100
committerErik Johnston <erik@matrix.org>2015-08-14 15:43:11 +0100
commit891dfd90bd97ad485d54dfae3e510c640de8e978 (patch)
treea9d959bd168adec21fd984b439c4afa5a7108b2d /synapse
parentMerge branch 'develop' into erikj/reactor_metrics (diff)
downloadsynapse-891dfd90bd97ad485d54dfae3e510c640de8e978.tar.xz
Fix pending_calls metric to not lie
Diffstat (limited to 'synapse')
-rw-r--r--synapse/metrics/__init__.py21
1 files changed, 18 insertions, 3 deletions
diff --git a/synapse/metrics/__init__.py b/synapse/metrics/__init__.py
index 0be9772991..7b8a20108b 100644
--- a/synapse/metrics/__init__.py
+++ b/synapse/metrics/__init__.py
@@ -158,18 +158,33 @@ def runUntilCurrentTimer(func):
 
     @functools.wraps(func)
     def f(*args, **kwargs):
-        pending_calls = len(reactor.getDelayedCalls())
+        now = reactor.seconds()
+        num_pending = 0
+
+        # _newTimedCalls is one long list of *all* pending calls. Below loop
+        # is based off of impl of reactor.runUntilCurrent
+        for p in reactor._newTimedCalls:
+            if p.time > now:
+                break
+
+            if p.delayed_time > 0:
+                continue
+
+            num_pending += 1
+
+        num_pending += len(reactor.threadCallQueue)
+
         start = time.time() * 1000
         ret = func(*args, **kwargs)
         end = time.time() * 1000
         tick_time.inc_by(end - start)
-        pending_calls_metric.inc_by(pending_calls)
+        pending_calls_metric.inc_by(num_pending)
         return ret
 
     return f
 
 
-if hasattr(reactor, "runUntilCurrent"):
+if hasattr(reactor, "runUntilCurrent") and hasattr(reactor, "_newTimedCalls"):
     # runUntilCurrent is called when we have pending calls. It is called once
     # per iteratation after fd polling.
     reactor.runUntilCurrent = runUntilCurrentTimer(reactor.runUntilCurrent)