This is an automated email from the ASF dual-hosted git repository.

Aias00 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new 78bf41aaca fix(ratelimiter): evict stale requests before count (#7120)
78bf41aaca is described below

commit 78bf41aacaf9e30c027487b3c683ecb1e620a655
Author: Liming Deng <[email protected]>
AuthorDate: Tue Sep 22 15:10:58 2026 +0800

    fix(ratelimiter): evict stale requests before count (#7120)
---
 .../scripts/sliding_window_request_rate_limiter.lua   |  3 +--
 .../executor/RedisRateLimiterScriptsTest.java         | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git 
a/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-ratelimiter/src/main/resources/META-INF/scripts/sliding_window_request_rate_limiter.lua
 
b/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-ratelimiter/src/main/resources/META-INF/scripts/sliding_window_request_rate_limiter.lua
index cb2911bd38..637c04066b 100644
--- 
a/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-ratelimiter/src/main/resources/META-INF/scripts/sliding_window_request_rate_limiter.lua
+++ 
b/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-ratelimiter/src/main/resources/META-INF/scripts/sliding_window_request_rate_limiter.lua
@@ -34,6 +34,7 @@ local window_time = 1
 local last_requested = 0
 local exists_key = redis.call('exists', tokens_key)
 if (exists_key == 1) then
+    redis.call('zremrangebyscore', tokens_key, 0, now - window_size / 
window_time)
     last_requested = redis.call('zcard', tokens_key)
 end
 --redis.log(redis.LOG_WARNING, "last_requested " .. last_requested)
@@ -48,8 +49,6 @@ end
 --redis.log(redis.LOG_WARNING, "remain_request " .. remain_request)
 --redis.log(redis.LOG_WARNING, "allowed_num " .. allowed_num)
 
-redis.call('zremrangebyscore', tokens_key, 0, now - window_size / window_time)
 redis.call('expire', tokens_key, math.ceil(window_size))
 
 return { allowed_num, remain_request }
-
diff --git 
a/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-ratelimiter/src/test/java/org/apache/shenyu/plugin/ratelimiter/executor/RedisRateLimiterScriptsTest.java
 
b/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-ratelimiter/src/test/java/org/apache/shenyu/plugin/ratelimiter/executor/RedisRateLimiterScriptsTest.java
index 39fe4fdd6e..7f727018f6 100644
--- 
a/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-ratelimiter/src/test/java/org/apache/shenyu/plugin/ratelimiter/executor/RedisRateLimiterScriptsTest.java
+++ 
b/shenyu-plugin/shenyu-plugin-fault-tolerance/shenyu-plugin-ratelimiter/src/test/java/org/apache/shenyu/plugin/ratelimiter/executor/RedisRateLimiterScriptsTest.java
@@ -135,4 +135,23 @@ public class RedisRateLimiterScriptsTest {
                 .expectComplete()
                 .verify();
     }
+
+    @Test
+    @SuppressWarnings("unchecked")
+    public void slidingWindowRemovesExpiredRequestsBeforeCounting() {
+        RateLimiterAlgorithm<?> rateLimiterAlgorithm = 
RateLimiterAlgorithmFactory.newInstance("slidingWindow");
+        RedisScript<List<Long>> script = (RedisScript<List<Long>>) 
rateLimiterAlgorithm.getScript();
+        String tokenKey = "test-slidingWindow-expired";
+        long now = Instant.now().getEpochSecond();
+        ReactiveRedisTemplate<String, String> redisTemplate = 
Singleton.INST.get(ReactiveRedisTemplate.class);
+        redisTemplate.opsForZSet().add(tokenKey, "expired", now - 2).block();
+        List<String> keys = Arrays.asList(tokenKey, "current");
+        List<String> scriptArgs = Arrays.asList("1", "1", String.valueOf(now), 
"1");
+
+        Flux<List<Long>> resultFlux = redisTemplate.execute(script, keys, 
scriptArgs);
+
+        StepVerifier.create(resultFlux)
+                .expectNext(Arrays.asList(1L, 1L))
+                .verifyComplete();
+    }
 }

Reply via email to