mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2024-12-26 10:49:21 -05:00
Rewrite make_deferred_yieldable avoiding inlineCallbacks
... because (a) it's actually simpler (b) it might be marginally more performant?
This commit is contained in:
parent
9cb3a190bc
commit
3a75de923b
@ -299,10 +299,6 @@ def preserve_fn(f):
|
|||||||
Useful for wrapping functions that return a deferred which you don't yield
|
Useful for wrapping functions that return a deferred which you don't yield
|
||||||
on.
|
on.
|
||||||
"""
|
"""
|
||||||
def reset_context(result):
|
|
||||||
LoggingContext.set_current_context(LoggingContext.sentinel)
|
|
||||||
return result
|
|
||||||
|
|
||||||
def g(*args, **kwargs):
|
def g(*args, **kwargs):
|
||||||
current = LoggingContext.current_context()
|
current = LoggingContext.current_context()
|
||||||
res = f(*args, **kwargs)
|
res = f(*args, **kwargs)
|
||||||
@ -323,12 +319,11 @@ def preserve_fn(f):
|
|||||||
# which is supposed to have a single entry and exit point. But
|
# which is supposed to have a single entry and exit point. But
|
||||||
# by spawning off another deferred, we are effectively
|
# by spawning off another deferred, we are effectively
|
||||||
# adding a new exit point.)
|
# adding a new exit point.)
|
||||||
res.addBoth(reset_context)
|
res.addBoth(_set_context_cb, LoggingContext.sentinel)
|
||||||
return res
|
return res
|
||||||
return g
|
return g
|
||||||
|
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
|
||||||
def make_deferred_yieldable(deferred):
|
def make_deferred_yieldable(deferred):
|
||||||
"""Given a deferred, make it follow the Synapse logcontext rules:
|
"""Given a deferred, make it follow the Synapse logcontext rules:
|
||||||
|
|
||||||
@ -342,9 +337,16 @@ def make_deferred_yieldable(deferred):
|
|||||||
|
|
||||||
(This is more-or-less the opposite operation to preserve_fn.)
|
(This is more-or-less the opposite operation to preserve_fn.)
|
||||||
"""
|
"""
|
||||||
with PreserveLoggingContext():
|
if isinstance(deferred, defer.Deferred) and not deferred.called:
|
||||||
r = yield deferred
|
prev_context = LoggingContext.set_current_context(LoggingContext.sentinel)
|
||||||
defer.returnValue(r)
|
deferred.addBoth(_set_context_cb, prev_context)
|
||||||
|
return deferred
|
||||||
|
|
||||||
|
|
||||||
|
def _set_context_cb(result, context):
|
||||||
|
"""A callback function which just sets the logging context"""
|
||||||
|
LoggingContext.set_current_context(context)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
# modules to ignore in `logcontext_tracer`
|
# modules to ignore in `logcontext_tracer`
|
||||||
|
Loading…
Reference in New Issue
Block a user