First pass

This commit is contained in:
Adam La Morre 2022-02-01 20:59:06 -08:00
parent 70b2bcf229
commit 5737a83dc0
3 changed files with 14 additions and 0 deletions

View File

@ -148,6 +148,13 @@ class CommandLineInterface:
default=False, default=False,
action="store", action="store",
) )
self.arg_proxy_port = self.parser.add_argument(
"--max-requests",
dest="max_requests",
help="The maximum number of requests a worker will process before restarting.",
default=float('inf'),
action="store",
)
self.parser.add_argument( self.parser.add_argument(
"application", "application",
help="The application to dispatch to as path.to.module:instance.path", help="The application to dispatch to as path.to.module:instance.path",

View File

@ -56,6 +56,7 @@ class WebRequest(http.Request):
self.server = self.channel.factory.server self.server = self.channel.factory.server
self.application_queue = None self.application_queue = None
self._response_started = False self._response_started = False
self._complete_requests_counted = 0
self.server.protocol_connected(self) self.server.protocol_connected(self)
except Exception: except Exception:
logger.error(traceback.format_exc()) logger.error(traceback.format_exc())
@ -197,6 +198,10 @@ class WebRequest(http.Request):
self.application_queue.put_nowait(payload) self.application_queue.put_nowait(payload)
if not more_body: if not more_body:
break break
# Count completed Request and check against Max Requests
self._complete_requests_counted += 1
if self._complete_requests_counted > self.server.max_requests:
self.server.close()
except Exception: except Exception:
logger.error(traceback.format_exc()) logger.error(traceback.format_exc())

View File

@ -57,6 +57,7 @@ class Server:
application_close_timeout=10, application_close_timeout=10,
ready_callable=None, ready_callable=None,
server_name="Daphne", server_name="Daphne",
max_requests=0,
# Deprecated and does not work, remove in version 2.2 # Deprecated and does not work, remove in version 2.2
ws_protocols=None, ws_protocols=None,
): ):
@ -82,6 +83,7 @@ class Server:
self.abort_start = False self.abort_start = False
self.ready_callable = ready_callable self.ready_callable = ready_callable
self.server_name = server_name self.server_name = server_name
self.max_requests = max_requests
# Check our construction is actually sensible # Check our construction is actually sensible
if not self.endpoints: if not self.endpoints:
logger.error("No endpoints. This server will not listen on anything.") logger.error("No endpoints. This server will not listen on anything.")