This commit is contained in:
Adam La Morre 2022-02-03 20:31:32 -08:00
parent a75b67b395
commit aae49a0bae
3 changed files with 10 additions and 5 deletions

View File

@ -154,6 +154,7 @@ class CommandLineInterface:
help="The maximum number of requests a worker will process before restarting.",
default=float('inf'),
action="store",
type=int,
)
self.parser.add_argument(
"application",
@ -266,6 +267,7 @@ class CommandLineInterface:
endpoints = sorted(args.socket_strings + endpoints)
# Start the server
logger.info("Starting server at {}".format(", ".join(endpoints)))
logger.info(args.max_requests)
self.server = self.server_class(
application=application,
endpoints=endpoints,
@ -288,5 +290,6 @@ class CommandLineInterface:
if args.proxy_headers
else None,
server_name=args.server_name,
max_requests=args.max_requests,
)
self.server.run()

View File

@ -1,4 +1,5 @@
import logging
import sys
import time
import traceback
from urllib.parse import unquote
@ -56,7 +57,6 @@ 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())
@ -146,6 +146,11 @@ class WebRequest(http.Request):
# Boring old HTTP.
else:
# Count completed Request and check against Max Requests
self.server._complete_requests_counted += 1
if self.server._complete_requests_counted > self.server.max_requests:
sys.exit(0)
# Sanitize and decode headers, potentially extracting root path
self.clean_headers = []
self.root_path = self.server.root_path
@ -198,10 +203,6 @@ 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

@ -84,6 +84,7 @@ class Server:
self.ready_callable = ready_callable
self.server_name = server_name
self.max_requests = max_requests
self._complete_requests_counted = 0
# Check our construction is actually sensible
if not self.endpoints:
logger.error("No endpoints. This server will not listen on anything.")