diff --git a/daphne/cli.py b/daphne/cli.py index 923b9d3..4c4333d 100755 --- a/daphne/cli.py +++ b/daphne/cli.py @@ -148,6 +148,13 @@ class CommandLineInterface: default=False, 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( "application", help="The application to dispatch to as path.to.module:instance.path", diff --git a/daphne/http_protocol.py b/daphne/http_protocol.py index 7df7bae..a38803d 100755 --- a/daphne/http_protocol.py +++ b/daphne/http_protocol.py @@ -56,6 +56,7 @@ class WebRequest(http.Request): self.server = self.channel.factory.server self.application_queue = None self._response_started = False + self._complete_requests_counted = 0 self.server.protocol_connected(self) except Exception: logger.error(traceback.format_exc()) @@ -197,6 +198,10 @@ class WebRequest(http.Request): self.application_queue.put_nowait(payload) if not more_body: 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: logger.error(traceback.format_exc()) diff --git a/daphne/server.py b/daphne/server.py index 0d463d0..f4a8960 100755 --- a/daphne/server.py +++ b/daphne/server.py @@ -57,6 +57,7 @@ class Server: application_close_timeout=10, ready_callable=None, server_name="Daphne", + max_requests=0, # Deprecated and does not work, remove in version 2.2 ws_protocols=None, ): @@ -82,6 +83,7 @@ class Server: self.abort_start = False self.ready_callable = ready_callable self.server_name = server_name + self.max_requests = max_requests # Check our construction is actually sensible if not self.endpoints: logger.error("No endpoints. This server will not listen on anything.")