Move time_bound_deferred into Clock

This commit is contained in:
Erik Johnston 2015-02-11 10:25:06 +00:00
parent 0e6b3e4e40
commit dcf52469e8
3 changed files with 22 additions and 24 deletions

View File

@ -22,7 +22,7 @@ from twisted.web._newclient import ResponseDone
from synapse.http.agent_name import AGENT_NAME
from synapse.http.endpoint import matrix_federation_endpoint
from synapse.util.async import sleep, time_bound_deferred
from synapse.util.async import sleep
from synapse.util.logcontext import PreserveLoggingContext
from syutil.jsonutil import encode_canonical_json
@ -129,9 +129,8 @@ class MatrixFederationHttpClient(object):
producer
)
response = yield time_bound_deferred(
response = yield self.clock.time_bound_deferred(
request_deferred,
clock=self.clock,
time_out=60,
)

View File

@ -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

View File

@ -32,23 +32,3 @@ 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