Clarification on the use of Bucket4j to limit concurrent requests. #342
-
Hi, I have to implement a solution at work to limit the number of concurrent requests users can make to some API endpoints. Reading the documentation and looking at examples I see that it can be done with Bucket4j but if I have understood correctly some examples I have seen on the internet of how tryConsume works:
My doubts are when the consumed token is released, if when the whole code is finished executing inside if or as soon as it sees that there is a token available in the bucket and consumes it. To make it more clear I will expose two scenarios:
So my question is whether Bucket4j allows tokens to be released when the request code finishes executing, i.e. when the if block code finishes executing.
In this way, if the user can make 5 requests every X amount of time, as long as the user does not finish the 5 he currently has, the user cannot request more. And if the user finishes them much earlier, the suer could make them again. I have reviewed the refill strategies and it is not clear to me if these policies support scenario 1 and 2. That the bucket is not refilled until the execution of each request is finished instead of refilling every X amount of time. I hope I have made myself clear. Best regards. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello @MinionAttack Bucket4j does not limit concurrent requests and does not track the requests in progress. |
Beta Was this translation helpful? Give feedback.
Hello @MinionAttack
Bucket4j does not limit concurrent requests and does not track the requests in progress.