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

Commit

Permalink
Remove useless async job to delete device messages on sync
Browse files Browse the repository at this point in the history
  • Loading branch information
MatMaul committed Oct 14, 2023
1 parent 166ffc0 commit 86f89ac
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 24 deletions.
1 change: 1 addition & 0 deletions changelog.d/16491.misc
@@ -0,0 +1 @@
Remove useless async job to delete device messages on sync, since we only deliver (and hence delete) up to 100 device messages at a time.
22 changes: 0 additions & 22 deletions synapse/handlers/sync.py
Expand Up @@ -40,7 +40,6 @@
from synapse.api.presence import UserPresenceState
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
from synapse.events import EventBase
from synapse.handlers.device import DELETE_DEVICE_MSGS_TASK_NAME
from synapse.handlers.relations import BundledAggregations
from synapse.logging import issue9533_logger
from synapse.logging.context import current_context
Expand Down Expand Up @@ -363,36 +362,15 @@ async def _wait_for_sync_for_user(
# (since we now know that the device has received them)
if since_token is not None:
since_stream_id = since_token.to_device_key
# Fast path: delete a limited number of to-device messages up front.
# We do this to avoid the overhead of scheduling a task for every
# sync.
device_deletion_limit = 100
deleted = await self.store.delete_messages_for_device(
sync_config.user.to_string(),
sync_config.device_id,
since_stream_id,
limit=device_deletion_limit,
)
logger.debug(
"Deleted %d to-device messages up to %d", deleted, 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:
# we are going to return immediately, so don't bother calling
# notifier.wait_for_events.
Expand Down
5 changes: 3 additions & 2 deletions synapse/storage/databases/main/deviceinbox.py
Expand Up @@ -450,7 +450,7 @@ async def delete_messages_for_device(
user_id: str,
device_id: Optional[str],
up_to_stream_id: int,
limit: int,
limit: Optional[int] = None,
) -> int:
"""
Args:
Expand Down Expand Up @@ -481,11 +481,12 @@ async def delete_messages_for_device(
ROW_ID_NAME = self.database_engine.row_id_name

def delete_messages_for_device_txn(txn: LoggingTransaction) -> int:
limit_statement = "" if limit is None else f"LIMIT {limit}"
sql = f"""
DELETE FROM device_inbox WHERE {ROW_ID_NAME} IN (
SELECT {ROW_ID_NAME} FROM device_inbox
WHERE user_id = ? AND device_id = ? AND stream_id <= ?
LIMIT {limit}
{limit_statement}
)
"""
txn.execute(sql, (user_id, device_id, up_to_stream_id))
Expand Down

0 comments on commit 86f89ac

Please sign in to comment.