Open
Description
It appears that mangum runs ASGI lifecycle events (startup, shutdown etc.) on each lambda function invocation instead of run once and cached. This adds an extra ~200ms of runtime to each invocation and can exhaust available database connections unless lambda is calling through a proxy (ex. RDS Proxy) configured to aggressively drop idle connections.
This seems to fix the problem for pgstac:
# app.py
async def connect_to_database(settings: Settings):
db = DB()
logger.info("Connecting to database.")
readpool = await db.create_pool(settings.reader_connection_string, settings)
writepool = await db.create_pool(settings.writer_connection_string, settings)
logger.info("Succesfully connected to database.")
return readpool, writepool
# Define read/write pool in global scope, lambda will cache across invocations.
loop = asyncio.get_event_loop()
READPOOL, WRITEPOOL = loop.run_until_complete(connect_to_database(settings))
# Update startup event.
# Also need to delete the shutdown event.
@app.on_event("startup")
async def startup_event():
"""Connect to database on startup."""
app.state.readpool = READPOOL
app.state.writepool = WRITEPOOL