mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2024-09-06 18:31:56 +00:00
Fix so timing out connections to actually work.
This commit is contained in:
parent
41a9a76a99
commit
ef276e8770
@ -72,5 +72,7 @@ class ReplicationLayer(FederationClient, FederationServer):
|
||||
|
||||
self._order = 0
|
||||
|
||||
self.hs = hs
|
||||
|
||||
def __str__(self):
|
||||
return "<ReplicationLayer(%s)>" % self.server_name
|
||||
|
@ -18,6 +18,9 @@ from synapse.util.logcontext import LoggingContext
|
||||
from twisted.internet import defer, reactor, task
|
||||
|
||||
import time
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Clock(object):
|
||||
@ -55,20 +58,51 @@ class Clock(object):
|
||||
timer.cancel()
|
||||
|
||||
def time_bound_deferred(self, given_deferred, time_out):
|
||||
if given_deferred.called:
|
||||
return given_deferred
|
||||
|
||||
ret_deferred = defer.Deferred()
|
||||
|
||||
def timed_out():
|
||||
if not given_deferred.called:
|
||||
given_deferred.cancel()
|
||||
def timed_out_fn():
|
||||
try:
|
||||
ret_deferred.errback(RuntimeError("Timed out"))
|
||||
except:
|
||||
pass
|
||||
|
||||
timer = self.call_later(time_out, timed_out)
|
||||
try:
|
||||
given_deferred.cancel()
|
||||
except:
|
||||
pass
|
||||
|
||||
def succeed(result):
|
||||
timer = None
|
||||
|
||||
def cancel(res):
|
||||
try:
|
||||
self.cancel_call_later(timer)
|
||||
ret_deferred.callback(result)
|
||||
except:
|
||||
pass
|
||||
return res
|
||||
|
||||
given_deferred.addCallback(succeed)
|
||||
given_deferred.addErrback(ret_deferred.errback)
|
||||
ret_deferred.addBoth(cancel)
|
||||
|
||||
def sucess(res):
|
||||
try:
|
||||
ret_deferred.callback(res)
|
||||
except:
|
||||
pass
|
||||
|
||||
return res
|
||||
|
||||
def err(res):
|
||||
try:
|
||||
ret_deferred.errback(res)
|
||||
except:
|
||||
pass
|
||||
|
||||
return res
|
||||
|
||||
given_deferred.addCallbacks(callback=sucess, errback=err)
|
||||
|
||||
timer = self.call_later(time_out, timed_out_fn)
|
||||
|
||||
return ret_deferred
|
||||
|
Loading…
Reference in New Issue
Block a user