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,
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",

View File

@ -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())

View File

@ -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.")