From 5aefa61c7f2b56150f4f6c6af98c688a00e65cae Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Fri, 3 Feb 2023 14:35:38 +0000 Subject: [PATCH 1/4] Skip calculating unread push actions in `/sync` when `enable_push` is false. --- synapse/handlers/sync.py | 5 +++++ synapse/storage/databases/main/event_push_actions.py | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/synapse/handlers/sync.py b/synapse/handlers/sync.py index 3566537894e6..41f194624f07 100644 --- a/synapse/handlers/sync.py +++ b/synapse/handlers/sync.py @@ -269,6 +269,8 @@ def __init__(self, hs: "HomeServer"): self._state_storage_controller = self._storage_controllers.state self._device_handler = hs.get_device_handler() + self.should_calculate_push_rules = hs.config.push.enable_push + # TODO: flush cache entries on subsequent sync request. # Once we get the next /sync request (ie, one with the same access token # that sets 'since' to 'next_batch'), we know that device won't need a @@ -1288,6 +1290,9 @@ async def _find_missing_partial_state_memberships( async def unread_notifs_for_room_id( self, room_id: str, sync_config: SyncConfig ) -> RoomNotifCounts: + if not self.should_calculate_push_rules: + return RoomNotifCounts.empty() + with Measure(self.clock, "unread_notifs_for_room_id"): return await self.store.get_unread_event_push_actions_by_room_for_user( diff --git a/synapse/storage/databases/main/event_push_actions.py b/synapse/storage/databases/main/event_push_actions.py index 3a0c370fde1d..ed19db2f53b4 100644 --- a/synapse/storage/databases/main/event_push_actions.py +++ b/synapse/storage/databases/main/event_push_actions.py @@ -203,6 +203,10 @@ class RoomNotifCounts: # Map of thread ID to the notification counts. threads: Dict[str, NotifCounts] + @staticmethod + def empty() -> "RoomNotifCounts": + return RoomNotifCounts(NotifCounts(), {}) + def __len__(self) -> int: # To properly account for the amount of space in any caches. return len(self.threads) + 1 From f2e8445f0c680bedf5dcfec71ccd3fa0360554ff Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Fri, 3 Feb 2023 14:37:20 +0000 Subject: [PATCH 2/4] Newsfile --- changelog.d/14980.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/14980.misc diff --git a/changelog.d/14980.misc b/changelog.d/14980.misc new file mode 100644 index 000000000000..145f4a788b8e --- /dev/null +++ b/changelog.d/14980.misc @@ -0,0 +1 @@ +Skip calculating unread push actions in /sync when enable_push is false. From 5232c5849e3b7baaccf3ac9de4221046ad4d74fd Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Tue, 14 Feb 2023 09:13:11 +0000 Subject: [PATCH 3/4] Make singleton --- synapse/storage/databases/main/event_push_actions.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/synapse/storage/databases/main/event_push_actions.py b/synapse/storage/databases/main/event_push_actions.py index ed19db2f53b4..eeccf5db2433 100644 --- a/synapse/storage/databases/main/event_push_actions.py +++ b/synapse/storage/databases/main/event_push_actions.py @@ -205,13 +205,16 @@ class RoomNotifCounts: @staticmethod def empty() -> "RoomNotifCounts": - return RoomNotifCounts(NotifCounts(), {}) + return _EMPTY_ROOM_NOTIF_COUNTS def __len__(self) -> int: # To properly account for the amount of space in any caches. return len(self.threads) + 1 +_EMPTY_ROOM_NOTIF_COUNTS = RoomNotifCounts(NotifCounts(), {}) + + def _serialize_action( actions: Collection[Union[Mapping, str]], is_highlight: bool ) -> str: From b43c05a822ae646e5f97fa9dbf9013af2028f4b0 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Tue, 14 Feb 2023 09:14:23 +0000 Subject: [PATCH 4/4] Comment --- synapse/handlers/sync.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/synapse/handlers/sync.py b/synapse/handlers/sync.py index 41f194624f07..e56e01b4dc8d 100644 --- a/synapse/handlers/sync.py +++ b/synapse/handlers/sync.py @@ -1291,6 +1291,9 @@ async def unread_notifs_for_room_id( self, room_id: str, sync_config: SyncConfig ) -> RoomNotifCounts: if not self.should_calculate_push_rules: + # If push rules have been universally disabled then we know we won't + # have any unread counts in the DB, so we may as well skip asking + # the DB. return RoomNotifCounts.empty() with Measure(self.clock, "unread_notifs_for_room_id"):