From cef4e5ff38dc7d2200800837c110ab6beec10d8a Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 12 Apr 2017 11:37:19 -0400 Subject: Warn on _compiled_cache growth Added warnings to the LRU "compiled cache" used by the :class:`.Mapper` (and ultimately will be for other ORM-based LRU caches) such that when the cache starts hitting its size limits, the application will emit a warning that this is a performance-degrading situation that may require attention. The LRU caches can reach their size limits primarily if an application is making use of an unbounded number of :class:`.Engine` objects, which is an antipattern. Otherwise, this may suggest an issue that should be brought to the SQLAlchemy developer's attention. Additionally, adjusted the test_memusage algorithm again as the previous one could still allow a growing memory size to be missed. Change-Id: I020d1ceafb7a08f6addfa990a1e7acd09f933240 --- lib/sqlalchemy/util/_collections.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'lib/sqlalchemy/util') diff --git a/lib/sqlalchemy/util/_collections.py b/lib/sqlalchemy/util/_collections.py index bdd40fc79..d94af5f62 100644 --- a/lib/sqlalchemy/util/_collections.py +++ b/lib/sqlalchemy/util/_collections.py @@ -868,9 +868,12 @@ class LRUCache(dict): """ - def __init__(self, capacity=100, threshold=.5): + __slots__ = 'capacity', 'threshold', 'size_alert', '_counter', '_mutex' + + def __init__(self, capacity=100, threshold=.5, size_alert=None): self.capacity = capacity self.threshold = threshold + self.size_alert = size_alert self._counter = 0 self._mutex = threading.Lock() @@ -910,11 +913,19 @@ class LRUCache(dict): item[1] = value self._manage_size() + @property + def size_threshold(self): + return self.capacity + self.capacity * self.threshold + def _manage_size(self): if not self._mutex.acquire(False): return try: + size_alert = bool(self.size_alert) while len(self) > self.capacity + self.capacity * self.threshold: + if size_alert: + size_alert = False + self.size_alert() by_counter = sorted(dict.values(self), key=operator.itemgetter(2), reverse=True) -- cgit v1.2.1