This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Send federation events concurrently #3078
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
145d146
Handle exceptions in get_hosts_for_room when sending events over fede…
erikjohnston 11974f3
Send federation events concurrently
erikjohnston 6e025a9
Handle all events in a room correctly
erikjohnston d49cbf7
Log event ID on exception
erikjohnston 1246d23
Preserve log contexts correctly
erikjohnston a060dfa
Use run_in_background instead
erikjohnston File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,32 +169,38 @@ def _process_event_queue_loop(self): | |
while True: | ||
last_token = yield self.store.get_federation_out_pos("events") | ||
next_token, events = yield self.store.get_all_new_events_stream( | ||
last_token, self._last_poked_id, limit=20, | ||
last_token, self._last_poked_id, limit=100, | ||
) | ||
|
||
logger.debug("Handling %s -> %s", last_token, next_token) | ||
|
||
if not events and next_token >= self._last_poked_id: | ||
break | ||
|
||
for event in events: | ||
@defer.inlineCallbacks | ||
def handle_event(event): | ||
# Only send events for this server. | ||
send_on_behalf_of = event.internal_metadata.get_send_on_behalf_of() | ||
is_mine = self.is_mine_id(event.event_id) | ||
if not is_mine and send_on_behalf_of is None: | ||
continue | ||
|
||
# Get the state from before the event. | ||
# We need to make sure that this is the state from before | ||
# the event and not from after it. | ||
# Otherwise if the last member on a server in a room is | ||
# banned then it won't receive the event because it won't | ||
# be in the room after the ban. | ||
destinations = yield self.state.get_current_hosts_in_room( | ||
event.room_id, latest_event_ids=[ | ||
prev_id for prev_id, _ in event.prev_events | ||
], | ||
) | ||
return | ||
|
||
try: | ||
# Get the state from before the event. | ||
# We need to make sure that this is the state from before | ||
# the event and not from after it. | ||
# Otherwise if the last member on a server in a room is | ||
# banned then it won't receive the event because it won't | ||
# be in the room after the ban. | ||
destinations = yield self.state.get_current_hosts_in_room( | ||
event.room_id, latest_event_ids=[ | ||
prev_id for prev_id, _ in event.prev_events | ||
], | ||
) | ||
except Exception: | ||
logger.exception("Failed to calculate hosts in room") | ||
return | ||
|
||
destinations = set(destinations) | ||
|
||
if send_on_behalf_of is not None: | ||
|
@@ -207,6 +213,20 @@ def _process_event_queue_loop(self): | |
|
||
self._send_pdu(event, destinations) | ||
|
||
@defer.inlineCallbacks | ||
def handle_room_events(events): | ||
for event in events: | ||
yield handle_event(event) | ||
|
||
events_by_room = {} | ||
for event in events: | ||
events_by_room.setdefault(event.room_id, []).append(event) | ||
|
||
yield logcontext.make_deferred_yieldable(defer.gatherResults( | ||
[handle_room_events(evs) for evs in events_by_room.itervalues()], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this looks like it needs a |
||
consumeErrors=True | ||
)) | ||
|
||
events_processed_counter.inc_by(len(events)) | ||
|
||
yield self.store.update_federation_out_pos( | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why would this happen?
if we're going to do this, surely we should at least log the event id?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It shouldn't happen really, but it empirically seems to have on matrix.org in the past so is probably worth keeping for paranoia's sake.