- java.lang.Object
-
- io.github.bucket4j.Refill
-
public class Refill extends Object
Specifies the speed of tokens regeneration.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static Refillgreedy(long tokens, Duration period)Creates theRefillthat does refill of tokens in greedy manner, it will try to add the tokens to bucket as soon as possible.static Refillintervally(long tokens, Duration period)Creates theRefillthat does refill of tokens in intervally manner.static RefillintervallyAligned(long tokens, Duration period, Instant timeOfFirstRefill, boolean useAdaptiveInitialTokens)Creates theRefillthat does refill of tokens in intervally manner.static Refillof(long tokens, Duration period)Deprecated.static Refillsmooth(long tokens, Duration period)Deprecated.StringtoString()
-
-
-
Method Detail
-
of
@Deprecated public static Refill of(long tokens, Duration period)
Deprecated.
-
smooth
@Deprecated public static Refill smooth(long tokens, Duration period)
Deprecated.
-
greedy
public static Refill greedy(long tokens, Duration period)
Creates theRefillthat does refill of tokens in greedy manner, it will try to add the tokens to bucket as soon as possible. For example refill "10 tokens per 1 second" will add 1 token per each 100 millisecond, in other words refill will not wait 1 second to regenerate whole bunch of 10 tokens.The three refills bellow do refill of tokens with same speed:
Refill.greedy(600, Duration.ofMinutes(1));Refill.greedy(10, Duration.ofSeconds(1));Refill.greedy(1, Duration.ofMillis(100));If greediness is undesired then you can specify the fixed interval refill via
intervally(long, Duration)- Parameters:
tokens- amount of tokensperiod- the period withintokenswill be fully regenerated- Returns:
- the
Refillthat does refill of tokens in of manner
-
intervally
public static Refill intervally(long tokens, Duration period)
Creates theRefillthat does refill of tokens in intervally manner. "Intervally" in opposite to "greedy" will wait until wholeperiodwill be elapsed before regeneratetokens- Parameters:
tokens- amount of tokensperiod- the period withintokenswill be fully regenerated- Returns:
- the
Refillthat does refill of tokens in intervally manner
-
intervallyAligned
public static Refill intervallyAligned(long tokens, Duration period, Instant timeOfFirstRefill, boolean useAdaptiveInitialTokens)
Creates theRefillthat does refill of tokens in intervally manner. "Intervally" in opposite to "greedy" will wait until wholeperiodwill be elapsed before regeneratetokens.
In additional tointervally(long, Duration)it is possible to specify the time when first refill should happen viatimeOfFirstRefill. This option can be used to configure clear interval boundary i.e. start of second, minute, hour, day.Special notes about useAdaptiveInitialTokens:
IfuseAdaptiveInitialTokens == trueand timeOfFirstRefill is a moment in the future, then initial amount of tokens in the bandwidth will be calculated by following formula:Math.min(capacity, Math.max(0, bandwidthCapacity - refillTokens) + (timeOfFirstRefillMillis - nowMillis)/refillPeriod * refillTokens)
Bellow the list of examples of how does this formula can be applied:
Restrictions:// imagine that wall clock is 16:20, the first refill will happen at 17:00 // first refill will happen in the beginning of next hour Instant firstRefillTime = ZonedDateTime.now() .truncatedTo(ChronoUnit.HOURS) .plus(1, ChronoUnit.HOURS) .toInstant(); // initial tokens 266 will be Bandwidth.classic(400, Refill.intervallyAligned(400, Duration.ofHours(1), firstRefillTime, true)); // calculated by formula min(400, max(0, 400 - 400) + 40/60*400) = min(400, 0 + 266) = 266 // initial tokens will be 300 Bandwidth.classic(400, Refill.intervallyAligned(300, Duration.ofHours(1), firstRefillTime, true)); // calculated by formula min(400, max(0, 400 - 300) + 40/60*300) = min(400, 100 + 200) = 300 // initial tokens will be 333 Bandwidth.classic(400, Refill.intervallyAligned(200, Duration.ofHours(1), firstRefillTime, true)); // calculated by formula min(400, max(0, 400 - 200) + 40/60*200) = min(400, 200 + 133) = 333 // initial tokens will be 366 Bandwidth.classic(400, Refill.intervallyAligned(100, Duration.ofHours(1), firstRefillTime, true)); // calculated by formula min(400, max(0, 400 - 100) + 40/60*100) = min(400, 300 + 66) = 366-
If
useAdaptiveInitialTokensistruethen any attempt to explicitly specify initial amount of tokens viaBandwidth.withInitialTokens(long)will fail with exception, because it is impossible at the same time to specify tokens in explicitly and adaptively manners. -
It is impossible to use this method together with nanoTime based clock
LocalBucketBuilder.withNanosecondPrecision(), because we need inSystem.currentTimeMillis()based clock in order to properly measure the distance fromtimeOfFirstRefill
- Parameters:
tokens- amount of tokensperiod- the period withintokenswill be fully regeneratedtimeOfFirstRefill- the time of first refill, typically it should be a moment in the futureuseAdaptiveInitialTokens- iftruethen initialTokens may be reduced- Returns:
- the
Refillthat does refill of tokens in intervally manner
-
If
-
-