From 21b977ccfe11832aa1c7b448c6e657cf0df36ad4 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Tue, 30 Aug 2016 15:39:50 +0100 Subject: [PATCH 1/2] Occaisonally persist unpersisted presence updates --- synapse/handlers/presence.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/synapse/handlers/presence.py b/synapse/handlers/presence.py index 73752b2f8904..a3c7952a4c7a 100644 --- a/synapse/handlers/presence.py +++ b/synapse/handlers/presence.py @@ -191,6 +191,13 @@ def __init__(self, hs): 5000, ) + self.clock.call_later( + 60, + self.clock.looping_call, + self._persist_unpersisted_changes, + 60 * 1000, + ) + metrics.register_callback("wheel_timer_size", lambda: len(self.wheel_timer)) @defer.inlineCallbacks @@ -216,6 +223,27 @@ def _on_shutdown(self): ]) logger.info("Finished _on_shutdown") + @defer.inlineCallbacks + def _persist_unpersisted_changes(self): + """We periodically persist the unpersisted changes, as otherwise they + may stack up and slow down shutdown times. + """ + logger.info( + "Performing _persist_unpersisted_changes. Persiting %d unpersisted changes", + len(self.user_to_current_state) + ) + + unpersisted = self.unpersisted_users_changes + self.unpersisted_users_changes = set() + + if self.unpersisted_users_changes: + yield self.store.update_presence([ + self.user_to_current_state[user_id] + for user_id in unpersisted + ]) + + logger.info("Finished _persist_unpersisted_changes") + @defer.inlineCallbacks def _update_states(self, new_states): """Updates presence of users. Sets the appropriate timeouts. Pokes From 097330bae8cd948ea34a882290efd2182431b51c Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Tue, 30 Aug 2016 15:50:20 +0100 Subject: [PATCH 2/2] Check correct variable --- synapse/handlers/presence.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/handlers/presence.py b/synapse/handlers/presence.py index a3c7952a4c7a..ab33952cd2c8 100644 --- a/synapse/handlers/presence.py +++ b/synapse/handlers/presence.py @@ -236,7 +236,7 @@ def _persist_unpersisted_changes(self): unpersisted = self.unpersisted_users_changes self.unpersisted_users_changes = set() - if self.unpersisted_users_changes: + if unpersisted: yield self.store.update_presence([ self.user_to_current_state[user_id] for user_id in unpersisted