Don't convert to deferreds when not necessary

This commit is contained in:
Erik Johnston 2017-03-28 10:41:08 +01:00
parent f9b4bb05e0
commit 86780a8bc3
5 changed files with 12 additions and 8 deletions

View File

@ -17,15 +17,12 @@ from twisted.internet import defer
from synapse.push.presentable_names import ( from synapse.push.presentable_names import (
calculate_room_name, name_from_member_event calculate_room_name, name_from_member_event
) )
from synapse.util.logcontext import preserve_fn, preserve_context_over_deferred
@defer.inlineCallbacks @defer.inlineCallbacks
def get_badge_count(store, user_id): def get_badge_count(store, user_id):
invites, joins = yield preserve_context_over_deferred(defer.gatherResults([ invites = yield store.get_invited_rooms_for_user(user_id)
preserve_fn(store.get_invited_rooms_for_user)(user_id), joins = yield store.get_rooms_for_user(user_id)
preserve_fn(store.get_rooms_for_user)(user_id),
], consumeErrors=True))
my_receipts_by_room = yield store.get_receipts_for_user( my_receipts_by_room = yield store.get_receipts_for_user(
user_id, "m.read", user_id, "m.read",

View File

@ -101,7 +101,7 @@ class ObservableDeferred(object):
return d return d
else: else:
success, res = self._result success, res = self._result
return defer.succeed(res) if success else defer.fail(res) return res if success else defer.fail(res)
def observers(self): def observers(self):
return self._observers return self._observers

View File

@ -341,7 +341,10 @@ class CacheDescriptor(_CacheDescriptorBase):
cache.set(cache_key, result_d, callback=invalidate_callback) cache.set(cache_key, result_d, callback=invalidate_callback)
observer = result_d.observe() observer = result_d.observe()
if isinstance(observer, defer.Deferred):
return logcontext.make_deferred_yieldable(observer) return logcontext.make_deferred_yieldable(observer)
else:
return observer
wrapped.invalidate = cache.invalidate wrapped.invalidate = cache.invalidate
wrapped.invalidate_all = cache.invalidate_all wrapped.invalidate_all = cache.invalidate_all

View File

@ -315,6 +315,9 @@ def preserve_context_over_deferred(deferred, context=None):
the deferred follow the synapse logcontext rules: try the deferred follow the synapse logcontext rules: try
``make_deferred_yieldable`` instead. ``make_deferred_yieldable`` instead.
""" """
if not isinstance(deferred, defer.Deferred):
return deferred
if context is None: if context is None:
context = LoggingContext.current_context() context = LoggingContext.current_context()
d = _PreservingContextDeferred(context) d = _PreservingContextDeferred(context)

View File

@ -56,7 +56,8 @@ def filter_events_for_clients(store, user_tuples, events, event_id_to_state):
events ([synapse.events.EventBase]): list of events to filter events ([synapse.events.EventBase]): list of events to filter
""" """
forgotten = yield preserve_context_over_deferred(defer.gatherResults([ forgotten = yield preserve_context_over_deferred(defer.gatherResults([
preserve_fn(store.who_forgot_in_room)( defer.maybeDeferred(
preserve_fn(store.who_forgot_in_room),
room_id, room_id,
) )
for room_id in frozenset(e.room_id for e in events) for room_id in frozenset(e.room_id for e in events)