summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/util
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2017-04-12 11:37:19 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2017-04-12 12:53:40 -0400
commitcef4e5ff38dc7d2200800837c110ab6beec10d8a (patch)
tree24eac58c1fb6e3105c9c4c7046d1aa2b5259a67d /lib/sqlalchemy/util
parent1b463058e3282c73d0fb361f78e96ecaa23ce9f4 (diff)
downloadsqlalchemy-cef4e5ff38dc7d2200800837c110ab6beec10d8a.tar.gz
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
Diffstat (limited to 'lib/sqlalchemy/util')
-rw-r--r--lib/sqlalchemy/util/_collections.py13
1 files changed, 12 insertions, 1 deletions
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)