Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;

import jakarta.validation.constraints.DecimalMin;
import jakarta.validation.constraints.Min;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -139,7 +140,7 @@ public RedisRateLimiter(ReactiveStringRedisTemplate redisTemplate, RedisScript<L
* @param defaultBurstCapacity how many tokens the bucket can hold in token-bucket
* algorithm.
*/
public RedisRateLimiter(int defaultReplenishRate, long defaultBurstCapacity) {
public RedisRateLimiter(double defaultReplenishRate, long defaultBurstCapacity) {
super(Config.class, CONFIGURATION_PROPERTY_NAME, (ConfigurationService) null);
this.defaultConfig = new Config().setReplenishRate(defaultReplenishRate).setBurstCapacity(defaultBurstCapacity);
}
Expand All @@ -151,7 +152,7 @@ public RedisRateLimiter(int defaultReplenishRate, long defaultBurstCapacity) {
* algorithm.
* @param defaultRequestedTokens how many tokens are requested per request.
*/
public RedisRateLimiter(int defaultReplenishRate, long defaultBurstCapacity, int defaultRequestedTokens) {
public RedisRateLimiter(double defaultReplenishRate, long defaultBurstCapacity, int defaultRequestedTokens) {
this(defaultReplenishRate, defaultBurstCapacity);
Objects.requireNonNull(this.defaultConfig, "defaultConfig may not be null");
this.defaultConfig.setRequestedTokens(defaultRequestedTokens);
Expand Down Expand Up @@ -250,7 +251,7 @@ public Mono<Response> isAllowed(String routeId, String id) {
Config routeConfig = loadConfiguration(routeId);

// How many requests per second do you want a user to be allowed to do?
int replenishRate = routeConfig.getReplenishRate();
double replenishRate = routeConfig.getReplenishRate();

// How much bursting do you want to allow?
long burstCapacity = routeConfig.getBurstCapacity();
Expand Down Expand Up @@ -325,20 +326,20 @@ public Map<String, String> getHeaders(Config config, Long tokensLeft) {
@Validated
public static class Config {

@Min(1)
private int replenishRate;
@DecimalMin(value = "0.0", inclusive = false)
private double replenishRate;

@Min(0)
private long burstCapacity = 1;

@Min(1)
private int requestedTokens = 1;

public int getReplenishRate() {
public double getReplenishRate() {
return replenishRate;
}

public Config setReplenishRate(int replenishRate) {
public Config setReplenishRate(double replenishRate) {
this.replenishRate = replenishRate;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,20 @@ public void redisRateConfiguredFromJavaAPIDirectBean() {
assertFilter("alt_custom_redis_rate_limiter", 30, 60, 20, true);
}

private void assertFilter(String key, int replenishRate, int burstCapacity, int requestedTokens,
@Test
public void fractionalReplenishRateTest() {
String key = "fractionalKey";
double replenishRate = 0.5; // half token per second
int burstCapacity = 1;
assertFilter(key, replenishRate, burstCapacity, 1, true);
assertFilter(key, replenishRate, burstCapacity, 1, false);
assertFilter(key, replenishRate, burstCapacity, 1, false); // still denied
assertFilter(key, replenishRate, burstCapacity, 1, true); // allowed after enough
// "internal time"

}

private void assertFilter(String key, double replenishRate, int burstCapacity, int requestedTokens,
boolean useDefaultConfig) {
RedisRateLimiter.Config config;

Expand Down
Loading