summary refs log tree commit diff
path: root/synapse/util/async.py
diff options
context:
space:
mode:
authorWill Hunt <will@half-shot.uk>2018-07-09 13:31:21 +0000
committerGitHub <noreply@github.com>2018-07-09 13:31:21 +0000
commita7f4ebbd3e786ce2e156127a3806aa2594233542 (patch)
treec73795182b60fcc17a5e5b13864eb84758d96c29 /synapse/util/async.py
parent/limits => /config (diff)
parentMerge pull request #3464 from matrix-org/hawkowl/isort-run (diff)
downloadsynapse-a7f4ebbd3e786ce2e156127a3806aa2594233542.tar.xz
Merge branch 'develop' into hs/upload-limits
Diffstat (limited to 'synapse/util/async.py')
-rw-r--r--synapse/util/async.py51
1 files changed, 22 insertions, 29 deletions
diff --git a/synapse/util/async.py b/synapse/util/async.py

index 1df5c5600c..5d0fb39130 100644 --- a/synapse/util/async.py +++ b/synapse/util/async.py
@@ -13,39 +13,26 @@ # See the License for the specific language governing permissions and # limitations under the License. +import logging +from contextlib import contextmanager -from twisted.internet import defer, reactor +from six.moves import range + +from twisted.internet import defer from twisted.internet.defer import CancelledError from twisted.python import failure +from synapse.util import Clock, logcontext, unwrapFirstError + from .logcontext import ( - PreserveLoggingContext, make_deferred_yieldable, preserve_fn + PreserveLoggingContext, + make_deferred_yieldable, + run_in_background, ) -from synapse.util import logcontext, unwrapFirstError - -from contextlib import contextmanager - -import logging logger = logging.getLogger(__name__) -@defer.inlineCallbacks -def sleep(seconds): - d = defer.Deferred() - with PreserveLoggingContext(): - reactor.callLater(seconds, d.callback, seconds) - res = yield d - defer.returnValue(res) - - -def run_on_reactor(): - """ This will cause the rest of the function to be invoked upon the next - iteration of the main loop - """ - return sleep(0) - - class ObservableDeferred(object): """Wraps a deferred object so that we can add observer deferreds. These observer deferreds do not affect the callback chain of the original @@ -158,13 +145,13 @@ def concurrently_execute(func, args, limit): def _concurrently_execute_inner(): try: while True: - yield func(it.next()) + yield func(next(it)) except StopIteration: pass return logcontext.make_deferred_yieldable(defer.gatherResults([ - preserve_fn(_concurrently_execute_inner)() - for _ in xrange(limit) + run_in_background(_concurrently_execute_inner) + for _ in range(limit) ], consumeErrors=True)).addErrback(unwrapFirstError) @@ -178,13 +165,18 @@ class Linearizer(object): # do some work. """ - def __init__(self, name=None): + def __init__(self, name=None, clock=None): if name is None: self.name = id(self) else: self.name = name self.key_to_defer = {} + if not clock: + from twisted.internet import reactor + clock = Clock(reactor) + self._clock = clock + @defer.inlineCallbacks def queue(self, key): # If there is already a deferred in the queue, we pull it out so that @@ -225,7 +217,7 @@ class Linearizer(object): # the context manager, but it needs to happen while we hold the # lock, and the context manager's exit code must be synchronous, # so actually this is the only sensible place. - yield run_on_reactor() + yield self._clock.sleep(0) else: logger.info("Acquired uncontended linearizer lock %r for key %r", @@ -402,7 +394,7 @@ class DeferredTimeoutError(Exception): """ -def add_timeout_to_deferred(deferred, timeout, on_timeout_cancel=None): +def add_timeout_to_deferred(deferred, timeout, reactor, on_timeout_cancel=None): """ Add a timeout to a deferred by scheduling it to be cancelled after timeout seconds. @@ -417,6 +409,7 @@ def add_timeout_to_deferred(deferred, timeout, on_timeout_cancel=None): Args: deferred (defer.Deferred): deferred to be timed out timeout (Number): seconds to time out after + reactor (twisted.internet.reactor): the Twisted reactor to use on_timeout_cancel (callable): A callable which is called immediately after the deferred times out, and not if this deferred is