Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Log contexts and squash things together
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjohnston committed Apr 7, 2016
1 parent 639cd07 commit ee5aef6
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions synapse/util/async.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

from twisted.internet import defer, reactor

from .logcontext import PreserveLoggingContext, preserve_fn
from .logcontext import (
PreserveLoggingContext, preserve_fn, preserve_context_over_deferred,
)
from synapse.util import unwrapFirstError

from contextlib import contextmanager
Expand Down Expand Up @@ -141,14 +143,6 @@ def _concurrently_execute_inner():
], consumeErrors=True).addErrback(unwrapFirstError)


@contextmanager
def _trigger_defer_manager(d):
try:
yield
finally:
d.callback(None)


class Linearizer(object):
"""Linearizes access to resources based on a key. Useful to ensure only one
thing is happening at a time on a given resource.
Expand Down Expand Up @@ -177,13 +171,17 @@ def queue(self, key):
new_defer = defer.Deferred()
self.key_to_defer[key] = new_defer

def remove_if_current(_):
d = self.key_to_defer.get(key)
if d is new_defer:
self.key_to_defer.pop(key, None)

new_defer.addBoth(remove_if_current)
if current_defer:
yield preserve_context_over_deferred(current_defer)

yield current_defer
@contextmanager
def _ctx_manager(d):
try:
yield
finally:
d.callback(None)
d = self.key_to_defer.get(key)
if d is new_defer:
self.key_to_defer.pop(key, None)

defer.returnValue(_trigger_defer_manager(new_defer))
defer.returnValue(_ctx_manager(new_defer))

0 comments on commit ee5aef6

Please sign in to comment.