Spring WebFlux + Bucket4j #148
-
Hello! I am trying to understand how I can integrate with Spring WebFlux correctly. Is Bucket thread-safe? The goal is to have a Server-Side Rate Limiter in a Spring WebFlux CRUD Application.
LOG Why do the tokens consumed are more than 5 in 5 seconds? I would have expected only no more than 5 tokens consumed in 5 seconds.
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
@sbilello hello, Of course, bucket is thread-safe. When you create bucket via this code Bandwidth limit = Bandwidth.simple(5, Duration.ofSeconds(5)); you get bucket with 5 initial tokens and 1 token is added to the bucket each second. |
Beta Was this translation helpful? Give feedback.
-
It is not true. Pay deep attention that token-bucket does not declare contract in the form N tokens/M timewindow. Declared contact is more complex https://en.wikipedia.org/wiki/Token_bucket#Algorithm One time when the bucket is full you are able to consume 1 token, and one token will be added immediately after 5 seconds(because 1 token is added every 5 seconds), so after 5 seconds you are able to consume the token again, that leads to the possibility to come in a situation when you are able to consume 2 tokens during 5 seconds. Also, I want to notice that mocking a clock is the best way to play with Token-bucket algorithm, see example there #128 (comment) |
Beta Was this translation helpful? Give feedback.
-
@sbilello all solutions based on Token-Bucket/Leaky-Bucket(and similar algorithms that use fixed memory) are not suitable to cover these hard requirements. |
Beta Was this translation helpful? Give feedback.
@sbilello all solutions based on Token-Bucket/Leaky-Bucket(and similar algorithms that use fixed memory) are not suitable to cover these hard requirements.