public class Bandwidth extends Object implements Serializable
capacity
and refill
. Where:
Simple
- most popular way, which does not require from you to fully understand the token-bucket algorithm.
Use this way when you just want to specify easy limitation N tokens per M time window.
See this example of usage.
classic(long, Refill)
Classic} - hard way to specify limitation,
use it when you want to utilize the whole power of token-bucket. See this example of usage.
// Adds bandwidth that restricts to consume not often 1000 tokens per 1 minute and not often than 100 tokens per second
Bucket bucket = Bucket4j.builder().
.addLimit(Bandwidth.create(1000, Duration.ofMinutes(1)));
.addLimit(Bandwidth.create(100, Duration.ofSeconds(1)));
.build()
Bandwidth.simple(600, Duration.ofMinutes(1));
Bandwidth.simple(10, Duration.ofSeconds(1));
Bandwidth.simple(1, Duration.ofMillis(100));
If greediness is undesired, then you can specify the fixed interval refill via withFixedRefillInterval
.
When fixed refill interval was specified then greediness is turned-off.
For example the bandwidth bellow will refill 10 tokens per 1 second instead of 1 token per 100 milliseconds:
Bandwidth.simple(600, Duration.ofMinutes(1)).withFixedRefillInterval(Duration.ofSecond(1));
Modifier and Type | Method and Description |
---|---|
static Bandwidth |
classic(long capacity,
Refill refill)
Specifies limitation in classic interpretation of token-bucket algorithm.
|
static Bandwidth |
simple(long capacity,
Duration period)
Specifies simple limitation capacity tokens per period time window.
|
String |
toString() |
Bandwidth |
withFixedRefillInterval(Duration fixedRefillInterval)
Creates the copy of current bandwidth for which the greediness of refill is switched-off.
|
Bandwidth |
withInitialTokens(long initialTokens)
By default new created bandwidth has amount of tokens that equals its capacity.
|
public static Bandwidth simple(long capacity, Duration period)
capacity
- period
- public static Bandwidth classic(long capacity, Refill refill)
capacity
- refill
- public Bandwidth withInitialTokens(long initialTokens)
initialTokens
- public Bandwidth withFixedRefillInterval(Duration fixedRefillInterval)
fixedRefillInterval
- specifies the interval between refill of tokensBandwidth
configured by fixedRefillInterval
Copyright © 2018. All rights reserved.