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

Commit

Permalink
move should_drop_federated_event while look to a helper function
Browse files Browse the repository at this point in the history
Signed-off-by: jesopo <github@lolnerd.net>
  • Loading branch information
jesopo committed May 23, 2022
1 parent 10ae9e3 commit bbf79dd
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions synapse/federation/federation_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,34 @@ async def _handle_received_pdu(self, origin: str, pdu: EventBase) -> None:
pdu.room_id, room_version, lock, origin, pdu
)

async def _get_next_valid_staged_event_for_room(
self, room_id: str, room_version: RoomVersion
) -> Optional[Tuple[str, EventBase]]:
"""Return the first non-spam event from staging queue.
"""

while True:
# We need to do this check outside the lock to avoid a race between
# a new event being inserted by another instance and it attempting
# to acquire the lock.
next = await self.store.get_next_staged_event_for_room(
room_id, room_version
)

if next is None:
return None

origin, event = next

if await self._spam_checker.should_drop_federated_event(event):
logger.warning(
"Staged federated event contains spam, dropping %s",
event.event_id,
)
continue

return next

@wrap_as_background_process("_process_incoming_pdus_in_room_inner")
async def _process_incoming_pdus_in_room_inner(
self,
Expand Down Expand Up @@ -1116,31 +1144,15 @@ async def _process_incoming_pdus_in_room_inner(
(self._clock.time_msec() - received_ts) / 1000
)

while True:
# We need to do this check outside the lock to avoid a race between
# a new event being inserted by another instance and it attempting
# to acquire the lock.
next = await self.store.get_next_staged_event_for_room(
room_id, room_version
)

if next is None:
break

origin, event = next

if await self._spam_checker.should_drop_federated_event(event):
logger.warning(
"Staged federated event contains spam, dropping %s",
event.event_id,
)
continue

break
next = await self._get_next_valid_staged_event_for_room(
room_id, room_version
)

if not next:
break

origin, event = next

# Prune the event queue if it's getting large.
#
# We do this *after* handling the first event as the common case is
Expand Down

0 comments on commit bbf79dd

Please sign in to comment.