From 04604062984a6dc27174fa42e4c9f91fbe7b0c42 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Tue, 16 Jun 2015 16:59:38 +0100 Subject: [PATCH 1/3] Don't do unecessary db ops in presence.get_state --- synapse/handlers/message.py | 1 + synapse/handlers/presence.py | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/synapse/handlers/message.py b/synapse/handlers/message.py index 867fdbefb0..1ed9e961f1 100644 --- a/synapse/handlers/message.py +++ b/synapse/handlers/message.py @@ -403,6 +403,7 @@ class MessageHandler(BaseHandler): target_user=UserID.from_string(m.user_id), auth_user=auth_user, as_event=True, + check_auth=False, ) presence.append(member_presence) except SynapseError: diff --git a/synapse/handlers/presence.py b/synapse/handlers/presence.py index 023ad33ab0..7c03198313 100644 --- a/synapse/handlers/presence.py +++ b/synapse/handlers/presence.py @@ -191,24 +191,24 @@ class PresenceHandler(BaseHandler): defer.returnValue(False) @defer.inlineCallbacks - def get_state(self, target_user, auth_user, as_event=False): + def get_state(self, target_user, auth_user, as_event=False, check_auth=True): if self.hs.is_mine(target_user): - visible = yield self.is_presence_visible( - observer_user=auth_user, - observed_user=target_user - ) + if check_auth: + visible = yield self.is_presence_visible( + observer_user=auth_user, + observed_user=target_user + ) - if not visible: - raise SynapseError(404, "Presence information not visible") - state = yield self.store.get_presence_state(target_user.localpart) - if "mtime" in state: - del state["mtime"] - state["presence"] = state.pop("state") + if not visible: + raise SynapseError(404, "Presence information not visible") if target_user in self._user_cachemap: - cached_state = self._user_cachemap[target_user].get_state() - if "last_active" in cached_state: - state["last_active"] = cached_state["last_active"] + state = self._user_cachemap[target_user].get_state() + else: + state = yield self.store.get_presence_state(target_user.localpart) + if "mtime" in state: + del state["mtime"] + state["presence"] = state.pop("state") else: # TODO(paul): Have remote server send us permissions set state = self._get_or_offline_usercache(target_user).get_state() From b849a64f8d467a7b1159b4e8c4db4f3d73696f78 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Tue, 16 Jun 2015 17:03:24 +0100 Subject: [PATCH 2/3] Use DeferredList --- synapse/handlers/message.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/synapse/handlers/message.py b/synapse/handlers/message.py index 1ed9e961f1..de9c6da9ec 100644 --- a/synapse/handlers/message.py +++ b/synapse/handlers/message.py @@ -396,20 +396,20 @@ class MessageHandler(BaseHandler): ] presence_handler = self.hs.get_handlers().presence_handler - presence = [] - for m in room_members: - try: - member_presence = yield presence_handler.get_state( + presence_defs = yield defer.DeferredList( + [ + presence_handler.get_state( target_user=UserID.from_string(m.user_id), auth_user=auth_user, as_event=True, check_auth=False, ) - presence.append(member_presence) - except SynapseError: - logger.exception( - "Failed to get member presence of %r", m.user_id - ) + for m in room_members + ], + consumeErrors=True, + ) + + presence = [p for success, p in presence_defs if success] time_now = self.clock.time_msec() From eceb554a2f3e5bc3d1eb8b4f87c756dfd4c48340 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Tue, 16 Jun 2015 17:12:27 +0100 Subject: [PATCH 3/3] Use another deferred list --- synapse/handlers/message.py | 45 ++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/synapse/handlers/message.py b/synapse/handlers/message.py index de9c6da9ec..e324662f18 100644 --- a/synapse/handlers/message.py +++ b/synapse/handlers/message.py @@ -380,15 +380,6 @@ class MessageHandler(BaseHandler): if limit is None: limit = 10 - messages, token = yield self.store.get_recent_events_for_room( - room_id, - limit=limit, - end_token=now_token.room_key, - ) - - start_token = now_token.copy_and_replace("room_key", token[0]) - end_token = now_token.copy_and_replace("room_key", token[1]) - room_members = [ m for m in current_state.values() if m.type == EventTypes.Member @@ -396,20 +387,38 @@ class MessageHandler(BaseHandler): ] presence_handler = self.hs.get_handlers().presence_handler - presence_defs = yield defer.DeferredList( + + @defer.inlineCallbacks + def get_presence(): + presence_defs = yield defer.DeferredList( + [ + presence_handler.get_state( + target_user=UserID.from_string(m.user_id), + auth_user=auth_user, + as_event=True, + check_auth=False, + ) + for m in room_members + ], + consumeErrors=True, + ) + + defer.returnValue([p for success, p in presence_defs if success]) + + presence, (messages, token) = yield defer.gatherResults( [ - presence_handler.get_state( - target_user=UserID.from_string(m.user_id), - auth_user=auth_user, - as_event=True, - check_auth=False, + get_presence(), + self.store.get_recent_events_for_room( + room_id, + limit=limit, + end_token=now_token.room_key, ) - for m in room_members ], consumeErrors=True, - ) + ).addErrback(unwrapFirstError) - presence = [p for success, p in presence_defs if success] + start_token = now_token.copy_and_replace("room_key", token[0]) + end_token = now_token.copy_and_replace("room_key", token[1]) time_now = self.clock.time_msec()