Skip to content

Commit

Permalink
Refactor user_to_num_current_syncs from both worker and presence writ…
Browse files Browse the repository at this point in the history
…er versions of the handler onto the base
  • Loading branch information
realtyem committed Aug 5, 2023
1 parent b5b5d3a commit 3d30713
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions synapse/handlers/presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ def __init__(self, hs: "HomeServer"):

self._busy_presence_enabled = hs.config.experimental.msc3026_enabled

# The number of *ongoing* syncs on this process, by user id. Empty if
# use_presence is false. While this is non zero a user will never go offline.
self._user_to_num_current_syncs: Dict[str, int] = {}

active_presence = self.store.take_presence_startup_info()
self.user_to_current_state = {state.user_id: state for state in active_presence}

Expand Down Expand Up @@ -413,10 +417,6 @@ def __init__(self, hs: "HomeServer"):
hs.config.worker.writers.presence,
)

# The number of ongoing syncs on this process, by user id.
# Empty if _presence_enabled is false.
self._user_to_num_current_syncs: Dict[str, int] = {}

self.notifier = hs.get_notifier()
self.instance_id = hs.get_instance_id()

Expand Down Expand Up @@ -545,8 +545,8 @@ async def user_syncing(
self.mark_as_coming_online(user_id)

def _end() -> None:
# We check that the user_id is in user_to_num_current_syncs because
# user_to_num_current_syncs may have been cleared if we are
# We check that the user_id is in _user_to_num_current_syncs because
# _user_to_num_current_syncs may have been cleared if we are
# shutting down.
if user_id in self._user_to_num_current_syncs:
self._user_to_num_current_syncs[user_id] -= 1
Expand Down Expand Up @@ -751,10 +751,6 @@ def __init__(self, hs: "HomeServer"):

self._next_serial = 1

# Keeps track of the number of *ongoing* syncs on this process. While
# this is non zero a user will never go offline.
self.user_to_num_current_syncs: Dict[str, int] = {}

# Keeps track of the number of *ongoing* syncs on other processes.
# While any sync is ongoing on another process the user will never
# go offline.
Expand Down Expand Up @@ -984,7 +980,7 @@ async def _handle_timeouts(self) -> None:

syncing_user_ids = {
user_id
for user_id, count in self.user_to_num_current_syncs.items()
for user_id, count in self._user_to_num_current_syncs.items()
if count
}
for user_ids in self.external_process_to_current_syncs.values():
Expand Down Expand Up @@ -1051,8 +1047,8 @@ async def user_syncing(
affect_presence = False

if affect_presence:
curr_sync = self.user_to_num_current_syncs.get(user_id, 0) + 1
self.user_to_num_current_syncs[user_id] = curr_sync
curr_sync = self._user_to_num_current_syncs.get(user_id, 0) + 1
self._user_to_num_current_syncs[user_id] = curr_sync

prev_state = await self.current_state_for_user(user_id)

Expand Down Expand Up @@ -1095,7 +1091,7 @@ async def user_syncing(

async def _end() -> None:
try:
self.user_to_num_current_syncs[user_id] -= 1
self._user_to_num_current_syncs[user_id] -= 1

prev_state_inner = await self.current_state_for_user(user_id)
await self._update_states(
Expand Down

0 comments on commit 3d30713

Please sign in to comment.