-
-
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
Middlewares ordering #1947
Comments
@app.middleware("request")
async def middleware1(request):
print("middleware1")
@app.middleware("request")
async def middleware2(request):
print("middleware2")
@app.middleware("response")
async def middleware3(request, response):
print("middleware3")
@app.middleware("response")
async def middleware4(request, response):
print("middleware4")
A few releases back this changed. |
On the ordering issue... I did get into a discussion with someone not too long ago about something like this: @app.middleware("request", priority=20) The idea is okay. My concern here would be that it happen only at startup. Not at execution time. Which then begs the question if it is actually helpful or not. |
Currently it is exactly how I read in documentation: request middlewares in order they were registered, and response middlewares in reverse order. I think about something almost like above:
Yes - I would prefer string not int because for example some "miniapp/ blueprint" could have priorities for example like: "10.1", "10.2", "10.3" - anyway it seams to be more flexible. And I think that you register middleware as above,
or maybe better app.run() does it anyway, |
Then I am a moron. 😒 I had in my mind they were both reverse. If we were to add something like this, then I would not require a type. My biggest hesitancy is that this seems like it has the potential for some crazy scope creep. What if I want dynamic priority? What if I want a custom sort key? What if I want ad hoc sorting (as you suggested)? |
Also the same story for listeners. But we could also add priority parameter to app.listener('before_server_start', priority="A.a"). About Yours hesitancy - You are right. So maybe it is better to take it one day at a time. Just one thing - it should also be compatible with what we have already |
Backwards compat is a must. I would suggest making it even simpler by using As new items are registered, just add a priority if not explicitly passed. Sort based on that. Just make sure we capture any |
If you really want to support string based priority, then make a method on |
OK. But I believe it is better to discuss everything we are aware of then just do things wrong. Now we have request_middleware and named_request_middleware. So if we leave it like that then middleware ordering in general will not work as expected - it will work only within middlewares from named_request_middleware['name_ot_route'] scopes sepparattely. Right now we have:
I wonder why we have two separates request_middleware and named_request_middleware ??? (and what was reasons behind it) I believe it would be better to have just one like:
Then we could even set something like (what would provide even more flexibility and freedom):
Then sorting would be easy, |
If I recall correctly, we discussed this a year or two ago when it came up then. We concluded the current order as-documented is the correct order (and I agree). I will mention here though, that the "Enhanced Middleware System" of Sanic Plugins Framework has the ability to set priorities on Middleware as a core feature, and it used by lots of Sanic Plugins. |
Also keep in mind we are on a breaking change freeze through the end of the year. Any change that will not be backwards compat must wait until 2021. |
So the way things are (I stress this is just my point of view):
|
I will respond in more details later today. First, I thank you you for the offer to put in the work on this. However, there is likely to be some significant changes on this coming Q1. So any work you put in right now might become moot. |
I think the ideas here (in general) need consideration and implementation. I think we should put this on hold until later in the year. This is itself dependent upon the signaling API that is in the works that will ultimate control the attaching and ordering of middleware. |
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. |
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. |
FWIW - #2550 Adds an API for this to be released in v22.9 |
Right now request middlewares are called according to order in which they were added
and response middlewares in reverse order.
I think it could be beneficial if we had possibility to explicitly set order for calling middlewares.
What do You think, Yes or No and why ?
The text was updated successfully, but these errors were encountered: