summary refs log tree commit diff
path: root/synapse/util/__init__.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2015-02-11 10:25:06 +0000
committerErik Johnston <erik@matrix.org>2015-02-11 10:25:06 +0000
commitdcf52469e821b3b2b69a0610c2c4f025a5aac68f (patch)
treeec7527eedb3de013c6ff2a34883e627df1890a09 /synapse/util/__init__.py
parentTime out HTTP federation requests (diff)
downloadsynapse-dcf52469e821b3b2b69a0610c2c4f025a5aac68f.tar.xz
Move time_bound_deferred into Clock
Diffstat (limited to 'synapse/util/__init__.py')
-rw-r--r--synapse/util/__init__.py21
1 files changed, 20 insertions, 1 deletions
diff --git a/synapse/util/__init__.py b/synapse/util/__init__.py
index 4e837a918e..2da8dfa719 100644
--- a/synapse/util/__init__.py
+++ b/synapse/util/__init__.py
@@ -15,7 +15,7 @@
 
 from synapse.util.logcontext import LoggingContext
 
-from twisted.internet import reactor
+from twisted.internet import defer, reactor
 
 import time
 
@@ -45,3 +45,22 @@ class Clock(object):
 
     def cancel_call_later(self, timer):
         timer.cancel()
+
+    def time_bound_deferred(self, given_deferred, time_out):
+        ret_deferred = defer.Deferred()
+
+        def timed_out():
+            if not given_deferred.called:
+                given_deferred.cancel()
+                ret_deferred.errback(RuntimeError("Timed out"))
+
+        timer = self.call_later(time_out, timed_out)
+
+        def succeed(result):
+            self.cancel_call_later(timer)
+            ret_deferred.callback(result)
+
+        given_deferred.addCallback(succeed)
+        given_deferred.addErrback(ret_deferred.errback)
+
+        return ret_deferred