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

Commit ca52419

Browse files
committed
Use wrap_as_background_process
1 parent 71fcc0e commit ca52419

File tree

1 file changed

+21
-28
lines changed

1 file changed

+21
-28
lines changed

synapse/storage/databases/main/cache.py

+21-28
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
from synapse.api.constants import EventTypes
2121
from synapse.config._base import Config
22-
from synapse.metrics.background_process_metrics import run_as_background_process
22+
from synapse.metrics.background_process_metrics import wrap_as_background_process
2323
from synapse.replication.tcp.streams import BackfillStream, CachesStream
2424
from synapse.replication.tcp.streams.events import (
2525
EventsStream,
@@ -584,36 +584,29 @@ def get_cache_stream_token_for_writer(self, instance_name: str) -> int:
584584
else:
585585
return 0
586586

587-
def _clean_up_cache_invalidation_wrapper(self) -> None:
588-
async def _clean_up_cache_invalidation_background() -> None:
589-
"""
590-
Clean up cache invalidation stream table entries occasionally.
591-
If we are behind (i.e. there are entries old enough to
592-
be deleted but too many of them to be deleted in one go),
593-
then we run slightly more frequently.
594-
"""
595-
delete_up_to: int = (
596-
self.hs.get_clock().time_msec()
597-
- RETENTION_PERIOD_OF_CACHE_INVALIDATIONS_MS
598-
)
599-
600-
in_backlog = await self._clean_up_batch_of_old_cache_invalidations(
601-
delete_up_to
602-
)
587+
@wrap_as_background_process("clean_up_old_cache_invalidations")
588+
async def _clean_up_cache_invalidation_wrapper(self) -> None:
589+
"""
590+
Clean up cache invalidation stream table entries occasionally.
591+
If we are behind (i.e. there are entries old enough to
592+
be deleted but too many of them to be deleted in one go),
593+
then we run slightly more frequently.
594+
"""
595+
delete_up_to: int = (
596+
self.hs.get_clock().time_msec() - RETENTION_PERIOD_OF_CACHE_INVALIDATIONS_MS
597+
)
603598

604-
# Vary how long we wait before calling again depending on whether we
605-
# are still sifting through backlog or we have caught up.
606-
if in_backlog:
607-
next_interval = CATCH_UP_CLEANUP_INTERVAL_MS
608-
else:
609-
next_interval = REGULAR_CLEANUP_INTERVAL_MS
599+
in_backlog = await self._clean_up_batch_of_old_cache_invalidations(delete_up_to)
610600

611-
self.hs.get_clock().call_later(
612-
next_interval / 1000, self._clean_up_cache_invalidation_wrapper
613-
)
601+
# Vary how long we wait before calling again depending on whether we
602+
# are still sifting through backlog or we have caught up.
603+
if in_backlog:
604+
next_interval = CATCH_UP_CLEANUP_INTERVAL_MS
605+
else:
606+
next_interval = REGULAR_CLEANUP_INTERVAL_MS
614607

615-
run_as_background_process(
616-
"clean_up_old_cache_invalidations", _clean_up_cache_invalidation_background
608+
self.hs.get_clock().call_later(
609+
next_interval / 1000, self._clean_up_cache_invalidation_wrapper
617610
)
618611

619612
async def _clean_up_batch_of_old_cache_invalidations(

0 commit comments

Comments
 (0)