-
Notifications
You must be signed in to change notification settings - Fork 2k
aiohttp base nginx setup #9796
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
aiohttp base nginx setup #9796
Changes from all commits
6e2f164
464421b
5cf5fd9
355cd28
0f39fb5
a42adff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| FROM python:3.13 | ||
|
|
||
| ADD ./ /aiohttp | ||
|
|
||
| WORKDIR aiohttp | ||
|
|
||
| RUN pip3 install cython==3.0.11 gunicorn==23.0.0 && \ | ||
| pip3 install -r /aiohttp/requirements.txt | ||
|
|
||
| ENV CONNECTION=RAW | ||
|
|
||
| EXPOSE 8080 | ||
|
|
||
| CMD python3 -O -m gunicorn app.gunicorn:app -c gunicorn_conf.py |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,22 @@ | ||
| FROM python:3.13 | ||
|
|
||
| ADD ./ /aiohttp | ||
| RUN apt-get update && apt-get install -y nginx | ||
|
|
||
| WORKDIR aiohttp | ||
| ADD ./requirements.txt /aiohttp/requirements.txt | ||
|
|
||
| RUN pip3 install cython==3.0.11 && \ | ||
| pip3 install -r /aiohttp/requirements.txt | ||
|
|
||
| ADD ./ /aiohttp | ||
|
|
||
| WORKDIR /aiohttp | ||
|
|
||
| ENV CONNECTION=ORM | ||
|
|
||
| EXPOSE 8080 | ||
|
|
||
| CMD python3 -O -m gunicorn app.gunicorn:app -c gunicorn_conf.py | ||
| RUN chmod +x /aiohttp/nginx-entrypoint.sh | ||
|
|
||
| ENTRYPOINT ["/aiohttp/nginx-entrypoint.sh"] | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,20 @@ | ||
| FROM python:3.13 | ||
|
|
||
| ADD ./ /aiohttp | ||
| RUN apt-get update && apt-get install -y nginx | ||
|
|
||
| WORKDIR aiohttp | ||
| ADD ./requirements.txt /aiohttp/requirements.txt | ||
|
|
||
| RUN pip3 install cython==3.0.11 && \ | ||
| pip3 install -r /aiohttp/requirements.txt | ||
|
|
||
| ADD ./ /aiohttp | ||
|
|
||
| WORKDIR /aiohttp | ||
|
|
||
| ENV CONNECTION=RAW | ||
|
|
||
| EXPOSE 8080 | ||
|
|
||
| CMD python3 -O -m gunicorn app.gunicorn:app -c gunicorn_conf.py | ||
| RUN chmod +x /aiohttp/nginx-entrypoint.sh | ||
|
|
||
| ENTRYPOINT ["/aiohttp/nginx-entrypoint.sh"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import uvloop | ||
| from aiohttp import web | ||
| import argparse | ||
| from .main import create_app | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| uvloop.install() | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument('--socket', type=str, required=True) | ||
| args = parser.parse_args() | ||
|
|
||
| app = create_app() | ||
| web.run_app(app, path=args.socket) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| #!/bin/sh | ||
| set -e | ||
| CORES=$(nproc) | ||
| echo "$CORES cores detected, starting $CORES aiohttp workers..." | ||
|
|
||
| for i in $(seq 0 $((CORES-1))); do | ||
| SOCKET="/run/aiohttp-$i.sock" | ||
| echo "Starting worker on socket $SOCKET" | ||
| python3 -O -m app.app --socket $SOCKET & | ||
| done | ||
|
|
||
| echo "Waiting for all workers to be ready..." | ||
| for i in $(seq 0 $((CORES-1))); do | ||
| SOCKET="/run/aiohttp-$i.sock" | ||
| until [ -S "$SOCKET" ]; do | ||
| echo "Waiting for socket $SOCKET..." | ||
| sleep 0.2 | ||
| done | ||
| chown root:www-data "$SOCKET" | ||
| chmod 660 "$SOCKET" | ||
| done | ||
|
|
||
| cat > /aiohttp/nginx.conf <<EOF | ||
| user www-data; | ||
| worker_processes auto; | ||
|
|
||
| events { | ||
| worker_connections 65535; | ||
| } | ||
|
|
||
| http { | ||
| keepalive_requests 10000000; | ||
|
|
||
| upstream aiohttp { | ||
| least_conn; | ||
| EOF | ||
|
|
||
| for i in $(seq 0 $((CORES-1))); do | ||
| echo " server unix:/run/aiohttp-$i.sock fail_timeout=0;" >> /aiohttp/nginx.conf | ||
| done | ||
|
|
||
| cat >> /aiohttp/nginx.conf <<EOF | ||
| keepalive 32; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm also wondering if this should be much higher? i.e. The aiohttp servers can handle many simultaneous connections. With the above config, it might be that the workers are creating upto 65535 connections, of which 32 of them are allowed to be kept alive. So, we probably want this to be the same value.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 65535 connections could also start slowing down the event loop, so also worth testing with lower numbers too, once these are aligned.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've tried different options, someone helps but all of them insignificantly. So i decided to revert nginx as default proxy and keep it as separate docker file #9807 |
||
| } | ||
|
|
||
| server { | ||
| listen 8080 reuseport; | ||
|
|
||
| access_log off; | ||
| error_log stderr error; | ||
|
|
||
| location / { | ||
| proxy_pass http://aiohttp; | ||
| proxy_http_version 1.1; | ||
| proxy_set_header Connection ""; | ||
| proxy_redirect off; | ||
| proxy_buffering off; | ||
| } | ||
| } | ||
| } | ||
| EOF | ||
|
|
||
| echo "Starting Nginx..." | ||
| nginx -c /aiohttp/nginx.conf -g "daemon off;" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| aiohttp==3.11.16 | ||
| asyncpg==0.30.0 | ||
| gunicorn==23.0.0 | ||
| jinja2==3.1.6 | ||
| SQLAlchemy==2.0.40 | ||
| orjson==3.10.16 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any difference in performance if we drop this and use the default round-robin? Guessing this isn't really needed given that requests should be around 1ms.