mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2024-10-01 11:49:51 -04:00
Fix ExpiringCache.__len__ to be accurate
It used to try and produce an estimate, which was sometimes negative. This caused metrics to be sad, so lets always just calculate it from scratch.
This commit is contained in:
parent
5230bc1471
commit
19dc676d1a
@ -16,7 +16,7 @@
|
|||||||
import logging
|
import logging
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
|
||||||
from six import iteritems
|
from six import iteritems, itervalues
|
||||||
|
|
||||||
from synapse.metrics.background_process_metrics import run_as_background_process
|
from synapse.metrics.background_process_metrics import run_as_background_process
|
||||||
from synapse.util.caches import register_cache
|
from synapse.util.caches import register_cache
|
||||||
@ -59,8 +59,6 @@ class ExpiringCache(object):
|
|||||||
|
|
||||||
self.iterable = iterable
|
self.iterable = iterable
|
||||||
|
|
||||||
self._size_estimate = 0
|
|
||||||
|
|
||||||
self.metrics = register_cache("expiring", cache_name, self)
|
self.metrics = register_cache("expiring", cache_name, self)
|
||||||
|
|
||||||
if not self._expiry_ms:
|
if not self._expiry_ms:
|
||||||
@ -79,16 +77,11 @@ class ExpiringCache(object):
|
|||||||
now = self._clock.time_msec()
|
now = self._clock.time_msec()
|
||||||
self._cache[key] = _CacheEntry(now, value)
|
self._cache[key] = _CacheEntry(now, value)
|
||||||
|
|
||||||
if self.iterable:
|
|
||||||
self._size_estimate += len(value)
|
|
||||||
|
|
||||||
# Evict if there are now too many items
|
# Evict if there are now too many items
|
||||||
while self._max_len and len(self) > self._max_len:
|
while self._max_len and len(self) > self._max_len:
|
||||||
_key, value = self._cache.popitem(last=False)
|
_key, value = self._cache.popitem(last=False)
|
||||||
if self.iterable:
|
if self.iterable:
|
||||||
removed_len = len(value.value)
|
self.metrics.inc_evictions(len(value.value))
|
||||||
self.metrics.inc_evictions(removed_len)
|
|
||||||
self._size_estimate -= removed_len
|
|
||||||
else:
|
else:
|
||||||
self.metrics.inc_evictions()
|
self.metrics.inc_evictions()
|
||||||
|
|
||||||
@ -111,7 +104,9 @@ class ExpiringCache(object):
|
|||||||
return default
|
return default
|
||||||
|
|
||||||
if self.iterable:
|
if self.iterable:
|
||||||
self._size_estimate -= len(value.value)
|
self.metrics.inc_evictions(len(value.value))
|
||||||
|
else:
|
||||||
|
self.metrics.inc_evictions()
|
||||||
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
@ -149,7 +144,9 @@ class ExpiringCache(object):
|
|||||||
for k in keys_to_delete:
|
for k in keys_to_delete:
|
||||||
value = self._cache.pop(k)
|
value = self._cache.pop(k)
|
||||||
if self.iterable:
|
if self.iterable:
|
||||||
self._size_estimate -= len(value.value)
|
self.metrics.inc_evictions(len(value.value))
|
||||||
|
else:
|
||||||
|
self.metrics.inc_evictions()
|
||||||
|
|
||||||
logger.debug(
|
logger.debug(
|
||||||
"[%s] _prune_cache before: %d, after len: %d",
|
"[%s] _prune_cache before: %d, after len: %d",
|
||||||
@ -158,7 +155,7 @@ class ExpiringCache(object):
|
|||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
if self.iterable:
|
if self.iterable:
|
||||||
return self._size_estimate
|
return sum(len(entry.value) for entry in itervalues(self._cache))
|
||||||
else:
|
else:
|
||||||
return len(self._cache)
|
return len(self._cache)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user