mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2025-06-06 00:38:57 -04:00
Delete stale non-e2e devices for users, take 2 (#14595)
This should help reduce the number of devices e.g. simple bots the repeatedly login rack up. We only delete non-e2e devices as they should be safe to delete, whereas if we delete e2e devices for a user we may accidentally break their ability to receive e2e keys for a message.
This commit is contained in:
parent
a58b550eac
commit
c2de2ca630
5 changed files with 113 additions and 4 deletions
|
@ -52,6 +52,7 @@ from synapse.util import stringutils
|
|||
from synapse.util.async_helpers import Linearizer
|
||||
from synapse.util.caches.expiringcache import ExpiringCache
|
||||
from synapse.util.cancellation import cancellable
|
||||
from synapse.util.iterutils import batch_iter
|
||||
from synapse.util.metrics import measure_func
|
||||
from synapse.util.retryutils import NotRetryingDestination
|
||||
|
||||
|
@ -421,6 +422,9 @@ class DeviceHandler(DeviceWorkerHandler):
|
|||
|
||||
self._check_device_name_length(initial_device_display_name)
|
||||
|
||||
# Prune the user's device list if they already have a lot of devices.
|
||||
await self._prune_too_many_devices(user_id)
|
||||
|
||||
if device_id is not None:
|
||||
new_device = await self.store.store_device(
|
||||
user_id=user_id,
|
||||
|
@ -452,6 +456,31 @@ class DeviceHandler(DeviceWorkerHandler):
|
|||
|
||||
raise errors.StoreError(500, "Couldn't generate a device ID.")
|
||||
|
||||
async def _prune_too_many_devices(self, user_id: str) -> None:
|
||||
"""Delete any excess old devices this user may have."""
|
||||
device_ids = await self.store.check_too_many_devices_for_user(user_id)
|
||||
if not device_ids:
|
||||
return
|
||||
|
||||
# We don't want to block and try and delete tonnes of devices at once,
|
||||
# so we cap the number of devices we delete synchronously.
|
||||
first_batch, remaining_device_ids = device_ids[:10], device_ids[10:]
|
||||
await self.delete_devices(user_id, first_batch)
|
||||
|
||||
if not remaining_device_ids:
|
||||
return
|
||||
|
||||
# Now spawn a background loop that deletes the rest.
|
||||
async def _prune_too_many_devices_loop() -> None:
|
||||
for batch in batch_iter(remaining_device_ids, 10):
|
||||
await self.delete_devices(user_id, batch)
|
||||
|
||||
await self.clock.sleep(1)
|
||||
|
||||
run_as_background_process(
|
||||
"_prune_too_many_devices_loop", _prune_too_many_devices_loop
|
||||
)
|
||||
|
||||
async def _delete_stale_devices(self) -> None:
|
||||
"""Background task that deletes devices which haven't been accessed for more than
|
||||
a configured time period.
|
||||
|
@ -481,7 +510,7 @@ class DeviceHandler(DeviceWorkerHandler):
|
|||
device_ids = [d for d in device_ids if d != except_device_id]
|
||||
await self.delete_devices(user_id, device_ids)
|
||||
|
||||
async def delete_devices(self, user_id: str, device_ids: List[str]) -> None:
|
||||
async def delete_devices(self, user_id: str, device_ids: Collection[str]) -> None:
|
||||
"""Delete several devices
|
||||
|
||||
Args:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue