mliezun / caddy-snake

Caddy plugin to serve Python apps
MIT License
87 stars 3 forks source link

simple_app.py: response_headers don't work #1

Closed gnat closed 7 months ago

gnat commented 7 months ago

First, awesome, awesome project. Thank you!!

Amazing to finally get an alternative to mod_wsgi, specifically for Caddy! Kudos.

Been playing around with caddy-snake on Ubuntu, performance is pretty awesome under load for a WSGI server. (using hey: ulimit -n 1000000; ./hey -z 10s -c 1000 http://127.0.0.1:9080/) getting 6,000+ requests/sec on 16 thread machine. Not too shabby!

First thing I tried was to set a custom header, which didn't seem to work.

import uuid

def main(environ, start_response):
    """A simple WSGI application"""
    status = '200 OK'
    response_headers = [('Content-type', 'text/plain'),('Hello', 'World')]
    start_response(status, response_headers)
    return [f"<br>Hello World {str(uuid.uuid4())}".encode()]

image

Seems like Chrome is auto-converting to text/html as well after including <br> in the response.

mliezun commented 7 months ago

Hi @gnat! Thanks for filing the first issue.

Im also excited with what's possible by using wsgi in Caddy.

I was able to reproduce the problem from your comment. The root cause was that I was calling WriteHeader before setting the user provided headers.

I pushed the fix to main branch and I can confirm that it works as intended now by doing the following:

$ CGO_ENABLED=1 xcaddy build --with github.com/mliezun/caddy-snake@main
$ ./caddy run --config Caddyfile
$ curl -i http://localhost:9080/
HTTP/1.1 200 OK
Content-Type: text/plain
Hello: World
Server: Caddy
Date: Sun, 18 Feb 2024 15:59:14 GMT
Content-Length: 48

Hello World 2e924ad4-9070-4623-9ce1-fa55bd1a2f30

Can you confirm in your end that it works as intended?

mliezun commented 7 months ago

I also ran similar benchmarks and got around 6k requests/s on my machine. 😎

With gunicorn out of the box I got 1000 requests/s. Nonetheless gunicorn can be tweaked to be more performant.

Still, this is just a simple test and not indicative of real world applications performance.

gnat commented 7 months ago

Yup, that was it. Cheers @mliezun !!