List of tokens #189
Replies: 2 comments
-
Hello @basanth-ivoyant About quiring actual tokens per each bandwidth: VerboseResult<Long> verboseResult = bucket.asVerbose().getAvailableTokens();
long[] availableTokens = BucketInvestigationUtil.getAvailableTokens(verboseResult);
////////////////////////////////////////////////////////////////////////
package io.github.bucket4j;
// This class needs to be placed to the io.github.bucket4j package in order to have access to the BucketState#getCurrentSize
public class BucketInvestigationUtil {
public static long[] availableTokens getAvailableTokens(VerboseResult<Long> verboseResult) {
BucketConfiguration configuration = verboseResult.getConfiguration();
Bandwidth[] bandwidths = configuration.getBandwidths();
BucketState state = verboseResult.getState();
long[] availableTokens = new long[bandwidths.length];
for (int i = 0; i < bandwidths.length; i++) {
availableTokens[i] = state.getCurrentSize(i);
}
return availableTokens;
}
} Unfortunately, there is no clean way in the current API to set available tokens directly. I think this feature is reasonable and I will think about implementing it. Fortunately, you can use a little bit of dirty workaround through long newAvailableTokensValue = 50;
BucketConfiguration configuration = Bucket4j.configurationBuilder()
.addLimit(
Bandwidth.simple(100, Duration.ofMinutes(1)).withInitialTokens(newAvailableTokensValue)
)
.build();
// newAvailableTokensValue will replace current tokens because bucket state will be totally rewritten
bucket.replaceConfiguration(newConfiguration, TokensInheritanceStrategy.RESET) |
Beta Was this translation helpful? Give feedback.
-
With version 6.4.0 desired behavior can be achieved in the following way: VerboseResult<Long> result = bucket.asVerbose().getAvailableTokens();
long[] availableTokens = result.getDiagnostics().getAvailableTokensPerEachBandwidth(); |
Beta Was this translation helpful? Give feedback.
-
Hi -
Is there a way , where in we can fill the bucket with actual values and any of the consume methods, can return token values as list. I could write implementations of filler, which could query a source to fill the bucket with actual token values.
thank you,
Beta Was this translation helpful? Give feedback.
All reactions