redis脚本问题修复

This commit is contained in:
Wenchao Gong 2021-10-19 23:13:40 +08:00
parent 783a3f9946
commit e4ed70ee7e
3 changed files with 11 additions and 7 deletions

View File

@ -89,12 +89,17 @@ public class RedisConfig extends CachingConfigurerSupport {
.append("local key = KEYS[1]\n") .append("local key = KEYS[1]\n")
.append("local count = tonumber(ARGV[1])\n") .append("local count = tonumber(ARGV[1])\n")
.append("local time = tonumber(ARGV[2])\n") .append("local time = tonumber(ARGV[2])\n")
.append("local current = redis.call('get', key);\n") .append("local current = tonumber(redis.call('get', key));\n")
.append("if current and tonumber(current) > count then\n") .append("if current == nil then\n")
.append(" current = 1 \n")
.append("end\n")
//.append("redis.log(redis.LOG_WARNING, \"current \" .. current)\n")
.append("if current > count then\n")
.append(" return current;\n") .append(" return current;\n")
.append("end\n") .append("end\n")
.append("current = redis.call('incr', key)\n") .append("current = tonumber(redis.call('incr', key))\n")
.append("if tonumber(current) == 1 then\n") //.append("redis.log(redis.LOG_WARNING, \"current \" .. current)\n")
.append("if current == 1 then\n")
.append(" redis.call('expire', key, time)\n") .append(" redis.call('expire', key, time)\n")
.append("end\n") .append("end\n")
.append("return current;") .append("return current;")

View File

@ -289,8 +289,7 @@ public class CustomerGlobalFilter implements GlobalFilter, Ordered {
private <T> Mono<T> rateLimiter(ServerWebExchange exchange, IscRule rule, Route route, final int index, private <T> Mono<T> rateLimiter(ServerWebExchange exchange, IscRule rule, Route route, final int index,
Supplier<Mono<T>> rateLimiterAfterSupplier) { Supplier<Mono<T>> rateLimiterAfterSupplier) {
final TimeUnit timeUnit = TIME_UNITS[index]; final TimeUnit timeUnit = TIME_UNITS[index];
final Long limit = TimeUnit.SECONDS.equals(timeUnit) ? rule.getSecondsLimit() : final Long limit = TimeUnit.SECONDS.equals(timeUnit) ? rule.getSecondsLimit() : TimeUnit.MINUTES.equals(timeUnit)
TimeUnit.MINUTES.equals(timeUnit)
? rule.getMinutesLimit() : TimeUnit.HOURS.equals(timeUnit) ? rule.getHoursLimit() : rule.getDaysLimit(); ? rule.getMinutesLimit() : TimeUnit.HOURS.equals(timeUnit) ? rule.getHoursLimit() : rule.getDaysLimit();
if (Objects.isNull(limit) || limit <= 0L) { if (Objects.isNull(limit) || limit <= 0L) {
if (TimeUnit.DAYS.equals(timeUnit)) { if (TimeUnit.DAYS.equals(timeUnit)) {

View File

@ -149,7 +149,7 @@ public class CustomerRedisRateLimiter extends RedisRateLimiter {
} }
return Mono.just(-1L); return Mono.just(-1L);
}).map(tokensLeft -> { }).map(tokensLeft -> {
boolean allowed = tokensLeft < replenishRate; boolean allowed = tokensLeft > 0 && tokensLeft < replenishRate;
Response response = new Response(allowed, getHeaders(routeConfig, tokensLeft, timeUnit)); Response response = new Response(allowed, getHeaders(routeConfig, tokensLeft, timeUnit));
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("response: " + response); log.debug("response: " + response);