adriangb / xpresso

A composable Python ASGI web framework
https://xpresso-api.dev/
MIT License
178 stars 4 forks source link

feat: support lifespans for mounted Xpresso apps #33

Closed adriangb closed 2 years ago

adriangb commented 2 years ago

We should be able to support multiple lifespans just using an AsyncExitStack to accumulate them.

This would be nice in conjunction with #32.

Basically, when we run App._setup as we traverse the routing tree we'd grab lifespans from any other Xpresso App that is mounted and add it to some list of lifespans. Then in App._lifespan we'd put them all into an AsyncExitStack. One change we may want to make is to do a BFS instead of the current DFS, I think it may make the order in which lifespans are entered more reasonable, but not sure it matters, and it would be an implementation detail anyway.

The main reason why this makes sense here and not in Starlette itself is that we are already searching / traversing the routing tree from the root (App) down. Starlette never does this, so adding this functionality to Starlette would be a lot of added complexity, here it is relatively little complexity. A smaller reason is that Starlette's handling of lifespans (putting them in routers and always wrapping them for backwards compatibility) would make it hard to detect when the user actually supplied a lifespan.

adriangb commented 2 years ago

Implemented in #35