Limiting concurrent requests from expect handler #6679
Replies: 3 comments 3 replies
-
I'm not too sure, but I suspect the answer is no. Middlewares are going to be run, so there is likely plenty of time in middlewares for the task to get cancelled. It's certainly not documented behaviour, so I wouldn't rely on it even if you can prove that it won't cancel with the current code. I'd suggest just implementing it in a middleware (make it the first middleware that is run), that's what they're there for. Additionally, the expect handler will only be called if the expect header is present, so not very useful if you want this as some kind of security feature. If the idea is to allow the client to avoid sending the body if it is going to be rejected, I'd suggest doing a check without incrementing the count in the expect handler, then increment it in a middleware. Maybe there is a very rare race condition where the server says OK, and then rejects the body, but it shouldn't be frequent enough to be a problem. Maybe there's also something clever you can come up with to ensure that it will always allows the connection after doing an expect check (like adding a flag to the request that bypasses the count check). |
Beta Was this translation helpful? Give feedback.
-
Maybe it's also possible to avoid the entire counting problem altogether, by just looking at the number of connections or tasks currently in use. e.g. https://docs.aiohttp.org/en/stable/web_reference.html?highlight=connections#aiohttp.web.Server.connections |
Beta Was this translation helpful? Give feedback.
-
Have you considered not doing this at the application level at all? There are good rate limiting options already built-in |
Beta Was this translation helpful? Give feedback.
-
I'm writing an API endpoint that potentially allows large POST uploads, and need to limit the number of requests in progress at the same time. So I have something like this:
Now, I want to reject the request earlier in the
Expect:
handler. My question is:Is it theoretically possible that, for a certain request, the
expect
handler does run, but then the main handler does not run? For example, maybe if the client disconnects at just the right moment?If this scenario is guaranteed impossible, then I can test and increment counter in the expect handler, set a flag on the request instance, and skip incrementing in the main handler, thus supporting requests with and without Expect header. On the other hand, if the scenario is possible in theory, then by Murphy's law it will happen in production and increment the counter all the way without ever decrementing it, leading to my service rejecting all requests.
Please also enlighten me if there's a much better way than the above to handle concurrency limits.
Beta Was this translation helpful? Give feedback.
All reactions