For those that have been following the discussion, since the introduction of Signals in 21.3, the intention has been to use them to replace and optimize the request response cycle using them. In addition to “replacing” listeners and middleware, the idea is to give an even greater depth into the lifecycle of the server and allowing for more customization.
In short, we are going to litter the code with:
await app.dispatch("foo.bar.baz")
Doing this alone would be terrible for performance. There is no need to do more heaviy lifting than required. Therefore, I started to work on both the signal integration, and a startup time parser that would “rewrite” parts of Sanic based upon the registered signals.
Before I get too far into the weeds, I want to pitch the idea and see if there is any more useful feedback.
The startup process would look something like this:
- Identify routes, build router
- Identify registered signals that are within a registered Sanic namespace
- DIspatch any early cycle signals
- Scan a group of predetermined functions that can be “optimized” and either inject or remove code required to if a signal hasbeen registered
- Look thru middleware and create one or more call chains based upon the routes they apply to and rewrite the handlers to have the dispatchers for the appropriate middleware
- Continue as normal dispatching signals instead of events
One change this will require to the existing API is to allow for signals to be awaited in the current task and not pushed off to the background.
Please feel fre to let me know your thoughts. I will push my working branch to the repo later this week.