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 to ResponseCache (#16028)
Browse files Browse the repository at this point in the history
Track whether the ResponseCache is evicting due to invalidation
or due to time.
  • Loading branch information
realtyem authored Aug 1, 2023
1 parent a4102d2 commit 7cbb2a0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog.d/16028.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Collect additional metrics from `ResponseCache` for eviction.
10 changes: 8 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 EvictionReason, register_cache

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 @@ -185,6 +185,12 @@ def unset(self, key: KV) -> None:
Args:
key: key used to remove the cached value
"""
self._metrics.inc_evictions(EvictionReason.invalidation)
self._result_cache.pop(key, None)

def _entry_timeout(self, key: KV) -> None:
"""For the call_later to remove from the cache"""
self._metrics.inc_evictions(EvictionReason.time)
self._result_cache.pop(key, None)

async def wrap(
Expand Down

0 comments on commit 7cbb2a0

Please sign in to comment.