diff options
author | Erik Johnston <erik@matrix.org> | 2015-02-10 18:17:27 +0000 |
---|---|---|
committer | Erik Johnston <erik@matrix.org> | 2015-02-10 18:17:27 +0000 |
commit | 0e6b3e4e40ae918eacdef87bb50ff1d19b304e7c (patch) | |
tree | d852ed68f2bd8d60a5b8cb7cc313ed3f48b65e35 /synapse/util | |
parent | Merge pull request #59 from matrix-org/hotfixes-v0.6.1f (diff) | |
download | synapse-0e6b3e4e40ae918eacdef87bb50ff1d19b304e7c.tar.xz |
Time out HTTP federation requests
Diffstat (limited to 'synapse/util')
-rw-r--r-- | synapse/util/async.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/synapse/util/async.py b/synapse/util/async.py index c4fe5d522f..d4d1d4b472 100644 --- a/synapse/util/async.py +++ b/synapse/util/async.py @@ -32,3 +32,23 @@ def run_on_reactor(): iteration of the main loop """ return sleep(0) + + +def time_bound_deferred(given_deferred, clock, time_out): + ret_deferred = defer.Deferred() + + def timed_out(): + if not given_deferred.called: + given_deferred.cancel() + ret_deferred.errback(RuntimeError("Timed out")) + + timer = clock.call_later(time_out, timed_out) + + def succeed(result): + clock.cancel_call_later(timer) + ret_deferred.callback(result) + + given_deferred.addCallback(succeed) + given_deferred.addErrback(ret_deferred.errback) + + return ret_deferred |