Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Fix sending presence over federation when using workers (#10163)
Browse files Browse the repository at this point in the history
When using a federation sender we'd send out all local presence updates over
federation even when they shouldn't be.

Fixes #10153.
  • Loading branch information
erikjohnston committed Jun 11, 2021
1 parent c955f22 commit 5e0b471
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelog.d/10163.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug when using federation sender worker where it would send out more presence updates than necessary, leading to high resource usage. Broke in v1.33.0.
25 changes: 19 additions & 6 deletions synapse/handlers/presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,6 @@ async def notify_from_replication(
users=users_to_states.keys(),
)

# If this is a federation sender, notify about presence updates.
await self.maybe_send_presence_to_interested_destinations(states)

async def process_replication_rows(
self, stream_name: str, instance_name: str, token: int, rows: list
):
Expand All @@ -519,11 +516,27 @@ async def process_replication_rows(
for row in rows
]

for state in states:
self.user_to_current_state[state.user_id] = state
# The list of states to notify sync streams and remote servers about.
# This is calculated by comparing the old and new states for each user
# using `should_notify(..)`.
#
# Note that this is necessary as the presence writer will periodically
# flush presence state changes that should not be notified about to the
# DB, and so will be sent over the replication stream.
state_to_notify = []

for new_state in states:
old_state = self.user_to_current_state.get(new_state.user_id)
self.user_to_current_state[new_state.user_id] = new_state

if not old_state or should_notify(old_state, new_state):
state_to_notify.append(new_state)

stream_id = token
await self.notify_from_replication(states, stream_id)
await self.notify_from_replication(state_to_notify, stream_id)

# If this is a federation sender, notify about presence updates.
await self.maybe_send_presence_to_interested_destinations(state_to_notify)

def get_currently_syncing_users_for_replication(self) -> Iterable[str]:
return [
Expand Down

0 comments on commit 5e0b471

Please sign in to comment.