-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Signals http.middleware.before
and http.middleware.after
are dispatched for _every_ middleware
#2352
Comments
Correct, that is intentional. Out of curiosity, what is the use case that you are after with dispatching once before and after middleware? Is it to have control over when something executes? There are certainly other signals you could potentially use if you need to do something earlier, like when the request is created, or routing is performed. Will these help? Or, are you particularly interested in creating a sort of "priority" middleware? The reason I ask is that there is a need for a bit of a revamp in middleware this year anyway. We have been talking about this for a long time now, and I think with some of the other improvements we made last year, we are finally getting close to being there. There are two main changes I think we should make:
|
I am trying to emulate the capabilities of Sanic-Plugin-Toolkit without monkey-patching, using the signals mechanism. I need to be able to run a queue of registered plugin middlewares before the application's middlewares are run. Putting it on The most logical place to put it would be on Given it is specific to a middleware use-case, that is why I proposed a signal called This leads into the other conversation in #2353 where I need to be able to check if any of those plugin middlewares produced an early response, and that needs to be passed back from the signal handler, through the signal dispatcher, to the calling site. That is not possible so I think this needs to wait until a revamp of the middleware stack. |
Yeah, I thought an alternative would be after the I think |
We should probably also include a lifecycle diagram in the docs with the flow of handlers, middleware, and signals. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is incorrect, please respond with an update. Thank you for your contributions. |
I think (tested) current version of Sanic doesn't read the body if the request type is GET without any payload in the body and hence won't trigger the event Like app = Sanic("MyHelloWorldApp")
@app.get("/")
async def hello_world1(request):
return text("Hello, world.")
@app.signal("http.lifecycle.read_body")
async def http_lifecycle_read_body(body):
print("http.lifecycle.read_body")
app.run() |
Never mind, because |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is incorrect, please respond with an update. Thank you for your contributions. |
For a single request, the signals
http.middleware.before
andhttp.middleware.after
are dispatched for every middle on the app.Example app:
Output:
Applicable section of code:
sanic/sanic/app.py
Lines 1346 to 1372 in a7bc8b5
It appears this is done intentionally. I don't know what the usefulness of running a handler for every middleware is, except for perhaps debug tracing or handler logger.
Maybe it makes sense to create two new signals like
http.middleware.before_all
andhttp.middleware.after_all
, in a place like this:The text was updated successfully, but these errors were encountered: