progress
This commit is contained in:
44
app.py
44
app.py
@@ -1,35 +1,37 @@
|
||||
import asyncio
|
||||
|
||||
import httpx
|
||||
|
||||
from config import load_config
|
||||
from docker_manager import DockerManager
|
||||
from middleware import ProxyMiddleware
|
||||
|
||||
|
||||
# Single shared HTTP client – reuses TCP connections via connection pooling.
|
||||
_http_client = httpx.AsyncClient(timeout=30.0)
|
||||
docker_mgr = DockerManager()
|
||||
routes = load_config("config.yml")
|
||||
|
||||
|
||||
class _LifespanApp:
|
||||
"""Minimal ASGI app that only handles lifespan events."""
|
||||
|
||||
async def __call__(self, scope, receive, send):
|
||||
if scope["type"] != "lifespan":
|
||||
async def _lifespan(scope, receive, send):
|
||||
if scope["type"] != "lifespan":
|
||||
return
|
||||
task = None
|
||||
while True:
|
||||
event = await receive()
|
||||
if event["type"] == "lifespan.startup":
|
||||
task = asyncio.create_task(docker_mgr.idle_watcher())
|
||||
await send({"type": "lifespan.startup.complete"})
|
||||
elif event["type"] == "lifespan.shutdown":
|
||||
if task:
|
||||
task.cancel()
|
||||
try:
|
||||
await task
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
await _http_client.aclose()
|
||||
await send({"type": "lifespan.shutdown.complete"})
|
||||
return
|
||||
task = None
|
||||
while True:
|
||||
event = await receive()
|
||||
if event["type"] == "lifespan.startup":
|
||||
task = asyncio.create_task(docker_mgr.idle_watcher())
|
||||
await send({"type": "lifespan.startup.complete"})
|
||||
elif event["type"] == "lifespan.shutdown":
|
||||
if task:
|
||||
task.cancel()
|
||||
try:
|
||||
await task
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
await send({"type": "lifespan.shutdown.complete"})
|
||||
return
|
||||
|
||||
|
||||
app = ProxyMiddleware(_LifespanApp(), routes=routes, docker=docker_mgr)
|
||||
app = ProxyMiddleware(_lifespan, routes=routes, docker=docker_mgr, http_client=_http_client)
|
||||
|
||||
Reference in New Issue
Block a user