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

Commit

Permalink
Add metrics tracking for eviction based on time to ResponseCache
Browse files Browse the repository at this point in the history
  • Loading branch information
realtyem committed Jul 29, 2023
1 parent f98f4f2 commit 95ab0ad
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions synapse/util/caches/response_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
)
from synapse.util import Clock
from synapse.util.async_helpers import AbstractObservableDeferred, ObservableDeferred
from synapse.util.caches import register_cache
from synapse.util.caches import register_cache, EvictionReason

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -167,7 +167,7 @@ def on_complete(r: RV) -> RV:
# the should_cache bit, we leave it in the cache for now and schedule
# its removal later.
if self.timeout_sec and context.should_cache:
self.clock.call_later(self.timeout_sec, self.unset, key)
self.clock.call_later(self.timeout_sec, self.entry_timeout, key)
else:
# otherwise, remove the result immediately.
self.unset(key)
Expand All @@ -187,6 +187,16 @@ def unset(self, key: KV) -> None:
"""
self._result_cache.pop(key, None)

def evict_because(self, key: KV, reason: EvictionReason) -> None:
"""Basically the same as unset, but update reason why evicting for metrics"""
self._metrics.inc_evictions(reason)
self.unset(key)

def entry_timeout(self, key: KV) -> None:
"""For the all later to remove from the cache"""
logger.debug(f"Expiring from [{self._name}] - {key}")
self.evict_because(key, EvictionReason.time)

async def wrap(
self,
key: KV,
Expand Down

0 comments on commit 95ab0ad

Please sign in to comment.