0
0
Fork 1
mirror of https://mau.dev/maunium/synapse.git synced 2024-09-27 20:19:03 +02:00

Don't schedule an async task on every sync (#16312)

This commit is contained in:
Erik Johnston 2023-09-13 11:59:44 +01:00 committed by GitHub
parent be3c7b08a3
commit e9addf6a01
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 11 deletions

1
changelog.d/16312.misc Normal file
View file

@ -0,0 +1 @@
Delete device messages asynchronously and in staged batches using the task scheduler.

View file

@ -362,21 +362,36 @@ class SyncHandler:
# (since we now know that the device has received them) # (since we now know that the device has received them)
if since_token is not None: if since_token is not None:
since_stream_id = since_token.to_device_key since_stream_id = since_token.to_device_key
# Delete device messages asynchronously and in batches using the task scheduler # Fast path: delete a limited number of to-device messages up front.
await self._task_scheduler.schedule_task( # We do this to avoid the overhead of scheduling a task for every
DELETE_DEVICE_MSGS_TASK_NAME, # sync.
resource_id=sync_config.device_id, device_deletion_limit = 100
params={ deleted = await self.store.delete_messages_for_device(
"user_id": sync_config.user.to_string(), sync_config.user.to_string(),
"device_id": sync_config.device_id, sync_config.device_id,
"up_to_stream_id": since_stream_id, since_stream_id,
}, limit=device_deletion_limit,
) )
logger.debug( logger.debug(
"Deletion of to-device messages up to %d scheduled", "Deleted %d to-device messages up to %d", deleted, since_stream_id
since_stream_id,
) )
# If we hit the limit, schedule a background task to delete the rest.
if deleted >= device_deletion_limit:
await self._task_scheduler.schedule_task(
DELETE_DEVICE_MSGS_TASK_NAME,
resource_id=sync_config.device_id,
params={
"user_id": sync_config.user.to_string(),
"device_id": sync_config.device_id,
"up_to_stream_id": since_stream_id,
},
)
logger.debug(
"Deletion of to-device messages up to %d scheduled",
since_stream_id,
)
if timeout == 0 or since_token is None or full_state: if timeout == 0 or since_token is None or full_state:
# we are going to return immediately, so don't bother calling # we are going to return immediately, so don't bother calling
# notifier.wait_for_events. # notifier.wait_for_events.