RateLimit-Reset Header : getting time to wait for refill even limit is not exceeded #197
-
Hi, It would be interesting to get remaining time for a refill, even if the limit is not reached. This allows client to not exceed the limit. Indeed, Extract of RateLimit Header Fields for HTTP draft-polli-ratelimit-headers-00 :
Actually, with bucket4j, it's not possible to have RateLimit-Reset without exceed the limit. Isn't it ? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hello, To get such information you need to use Verbose API: VerboseResult<ConsumptionProbe> result = bucket.asVerbose().tryConsumeAndReturnRemaining(1);
ConsumptionProbe probe = result.getValue();
Diagnostics diagnostics = result.getDiagnostics();
if (probe.isConsumed()) {
long nanosToRefillUpToLimit = diagnostics.calculateFullRefillingTime();
...
} else {
long nanosToRefillUpToLimit = diagnostics.calculateFullRefillingTime();
long nanosToRefillRequestedTokens = probe.getNanosToWaitForRefill();
...
} See this doc for more details https://bucket4j.com/7.0.0/toc.html#verbose-api especially this https://bucket4j.com/7.0.0/toc.html#example-of-verbose-api-usage |
Beta Was this translation helpful? Give feedback.
@xavier-b
Hello,
ConsumptionProbe#getNanosToWaitForRefill
has nothing in common withRateLimit-Reset
. As mentioned in the javadocs, it returns the time required to refill requested amount of tokens, not the time required to refill tokens up to max possible values.To get such information you need to use Verbose API: