diff --git a/AppServer/google/appengine/tools/dev_appserver.py b/AppServer/google/appengine/tools/dev_appserver.py index b6d1d7fe68..8e92fad5ae 100644 --- a/AppServer/google/appengine/tools/dev_appserver.py +++ b/AppServer/google/appengine/tools/dev_appserver.py @@ -63,7 +63,8 @@ import tempfile import yaml - +#AppScale +import resource @@ -185,7 +186,6 @@ MAX_URL_LENGTH = 2047 - DEFAULT_ENV = { 'GATEWAY_INTERFACE': 'CGI/1.1', 'AUTH_DOMAIN': 'gmail.com', @@ -232,6 +232,16 @@ DEVEL_FAKE_IS_ADMIN_HEADER = 'HTTP_X_APPENGINE_FAKE_IS_ADMIN' DEVEL_FAKE_IS_ADMIN_RAW_HEADER = 'X-AppEngine-Fake-Is-Admin' +#AppScale +# Soft cap on memory. If over dev_appserver will stop serving traffic and +# shut down. It will randomly choose to exit based on MAX_RANDOM_TARGET, +# hence the reason it is soft and not hard . Units are in KBs. +SOFT_CAP_MEM = 150000 + +# Max number for randomly killing the dev_appserver when over the soft +# memory cap. +MAX_RANDOM_TARGET = 25 + class Error(Exception): """Base-class for exceptions in this module.""" @@ -3634,6 +3644,19 @@ def serve_forever(self): """Handle one request at a time until told to stop.""" while not self._stopped: self.handle_request() + + # AppScale + # If this process is using too much memory kill it randomly + if resource.getrusage(resource.RUSAGE_SELF).ru_maxrss > SOFT_CAP_MEM: + # Stagger the killing so all processes do not go down at the same + # time. The lower the MAX_RANDOM_TARGET the higher probability it + # will shut down when over the soft memory cap. + rand = random.randint(0,MAX_RANDOM_TARGET) + if rand == 0: + logging.error("Usage of memory exceeded soft cap of " + \ + str(SOFT_CAP_MEM) + ". Exiting.") + break + self.server_close() def stop_serving_forever(self):