Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Max requests flag to exit after N requests #405

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ test_consumer*
.python-version
.pytest_cache/
.vscode
.dccache
2 changes: 1 addition & 1 deletion daphne/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys

__version__ = "3.0.2"
__version__ = "3.0.15"


# Windows on Python 3.8+ uses ProactorEventLoop, which is not compatible with
Expand Down
10 changes: 10 additions & 0 deletions daphne/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ def __init__(self):
default=False,
action="store",
)
self.max_requests = 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",
type=int,
)
self.parser.add_argument(
"application",
help="The application to dispatch to as path.to.module:instance.path",
Expand Down Expand Up @@ -259,6 +267,7 @@ def run(self, args):
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,
Expand All @@ -281,5 +290,6 @@ def run(self, args):
if args.proxy_headers
else None,
server_name=args.server_name,
max_requests=args.max_requests,
)
self.server.run()
12 changes: 11 additions & 1 deletion daphne/http_protocol.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import logging
import os
import sys
import time
import traceback
from urllib.parse import unquote
Expand All @@ -13,7 +15,6 @@

logger = logging.getLogger(__name__)


class WebRequest(http.Request):
"""
Request that either hands off information to channels, or offloads
Expand Down Expand Up @@ -198,6 +199,15 @@ def process(self):
if not more_body:
break

# Count completed requests and check against Max
self.server._complete_requests_counted += 1
if self.server._complete_requests_counted > self.server.max_requests:
logger.info('Max requests completed. Shutting down daphne...')
os.system("ps aux | grep 'daphne'")
bash_command = "kill $(ps aux | grep 'daphne' | awk '{print $2}')"
os.system(bash_command)
sys.exit(0)

Comment on lines +202 to +210
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm. OK, so... the expectation would be that we restart after X requests, not just shut down right?

except Exception:
logger.error(traceback.format_exc())
self.basic_error(
Expand Down
3 changes: 3 additions & 0 deletions daphne/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def __init__(
application_close_timeout=10,
ready_callable=None,
server_name="Daphne",
max_requests=float('inf'),
# Deprecated and does not work, remove in version 2.2
ws_protocols=None,
):
Expand All @@ -82,6 +83,8 @@ def __init__(
self.abort_start = False
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.")
Expand Down
7 changes: 7 additions & 0 deletions notes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Build: python setup.py sdist bdist_wheel
Test Upload: twine upload --repository-url https://test.pypi.org/legacy/ dist/*
Prod Upload: twine upload dist/*

Test Install: pip install -i https://test.pypi.org/simple/ adam-daphne==3.0.9

Uninstall everything: pip freeze | xargs pip uninstall -y
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
long_description = fp.read()

setup(
name="daphne",
name="adam-daphne",
version=__version__,
url="https://github.com/django/daphne",
author="Django Software Foundation",
Expand Down