Throw an error when replenishRate > burstCapacity

Fixes gh-2453
This commit is contained in:
Cristian Ruiz Bonilla
2021-12-01 20:12:11 +01:00
committed by spencergibb
parent 65d84afcb8
commit 0e23b9bc2f
2 changed files with 9 additions and 0 deletions

View File

@@ -40,6 +40,7 @@ import org.springframework.context.ApplicationContextAware;
import org.springframework.core.style.ToStringCreator;
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
/**
@@ -335,6 +336,8 @@ public class RedisRateLimiter extends AbstractRateLimiter<RedisRateLimiter.Confi
}
public Config setBurstCapacity(int burstCapacity) {
Assert.isTrue(burstCapacity >= this.replenishRate, "BurstCapacity(" + burstCapacity
+ ") must be greater than or equal than replenishRate(" + this.replenishRate + ")");
this.burstCapacity = burstCapacity;
return this;
}

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.gateway.filter.ratelimit;
import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -55,6 +56,11 @@ public class RedisRateLimiterConfigTests {
routeLocator.getRoutes().collectList().block();
}
@Test
public void shouldThrowAnErrorWhenReplenishRateIsHigherThanBurstCapacity() {
Assertions.assertThatThrownBy(() -> new RedisRateLimiter(10, 5)).isInstanceOf(IllegalArgumentException.class);
}
@Test
public void redisRateConfiguredFromEnvironment() {
assertFilter("redis_rate_limiter_config_test", 10, 20, 1, false);