client_python icon indicating copy to clipboard operation
client_python copied to clipboard

Add no-op asgi-lifespan handling

Open sheenobu opened this issue 3 years ago • 1 comments

Super simple hook that checks for lifespan and immediately responds type.complete, essentially making startup/shutdown lifespan events no-ops for the prometheus ASGI app.

Fixes #855

from hypercorn.middleware import DispatcherMiddleware
from prometheus_client import make_asgi_app
import asyncio

async def main():

    from hypercorn.asyncio import serve
    from hypercorn.config import Config

    app = DispatcherMiddleware({
        "/metrics": make_asgi_app(),
    })


    config = Config()
    config.bind = ["localhost:8080"]

    await serve(app, config)

Before:

    assert scope.get("type") == "http"
AssertionError

After:

[2022-11-15 16:05:20 -0800] [59048] [INFO] Running on http://127.0.0.1:8080 (CTRL + C to quit)
INFO:hypercorn.error:Running on http://127.0.0.1:8080 (CTRL + C to quit)

Signed-off-by: Sheena Artrip [email protected]

sheenobu avatar Nov 16 '22 00:11 sheenobu

If an exception is raised when calling the application callable with a lifespan.startup message or a scope with type lifespan, the server must continue but not send any lifespan events.

this might be a better solution!

sheenobu avatar Nov 16 '22 00:11 sheenobu