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

xiaoyu 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 fe4bfaf01 [type:BUG] RateLimiterPlugin concurrent hanlder error. 
(#3852)
fe4bfaf01 is described below

commit fe4bfaf014f3058d4b0264d3492cd5c2d25c8ef1
Author: yunlongn <[email protected]>
AuthorDate: Fri Sep 2 13:51:33 2022 +0800

    [type:BUG] RateLimiterPlugin concurrent hanlder error. (#3852)
    
    * [type:BUG] RateLimiterPlugin concurrent hanlder error.
    
    * [type:BUG] RateLimiterPlugin concurrent hanlder error.
    
    * [type:BUG] RateLimiterPlugin concurrent hanlder error.
    
    * [type:BUG] RateLimiterPlugin concurrent hanlder error.
---
 .../shenyu/plugin/ratelimiter/RateLimiterPlugin.java    |  7 ++++++-
 .../plugin/ratelimiter/executor/RedisRateLimiter.java   |  8 +++++---
 .../ratelimiter/response/RateLimiterResponse.java       | 17 ++++++++++++++++-
 .../plugin/ratelimiter/RateLimiterPluginTest.java       |  4 ++--
 4 files changed, 29 insertions(+), 7 deletions(-)

diff --git 
a/shenyu-plugin/shenyu-plugin-ratelimiter/src/main/java/org/apache/shenyu/plugin/ratelimiter/RateLimiterPlugin.java
 
b/shenyu-plugin/shenyu-plugin-ratelimiter/src/main/java/org/apache/shenyu/plugin/ratelimiter/RateLimiterPlugin.java
index 90b454c66..2eb2cbeb8 100644
--- 
a/shenyu-plugin/shenyu-plugin-ratelimiter/src/main/java/org/apache/shenyu/plugin/ratelimiter/RateLimiterPlugin.java
+++ 
b/shenyu-plugin/shenyu-plugin-ratelimiter/src/main/java/org/apache/shenyu/plugin/ratelimiter/RateLimiterPlugin.java
@@ -27,6 +27,8 @@ import org.apache.shenyu.plugin.api.result.ShenyuResultWrap;
 import org.apache.shenyu.plugin.api.utils.WebFluxResultUtils;
 import org.apache.shenyu.plugin.base.AbstractShenyuPlugin;
 import org.apache.shenyu.plugin.base.utils.CacheKeyUtils;
+import org.apache.shenyu.plugin.ratelimiter.algorithm.RateLimiterAlgorithm;
+import 
org.apache.shenyu.plugin.ratelimiter.algorithm.RateLimiterAlgorithmFactory;
 import org.apache.shenyu.plugin.ratelimiter.executor.RedisRateLimiter;
 import 
org.apache.shenyu.plugin.ratelimiter.handler.RateLimiterPluginDataHandler;
 import 
org.apache.shenyu.plugin.ratelimiter.resolver.RateLimiterKeyResolverFactory;
@@ -76,7 +78,10 @@ public class RateLimiterPlugin extends AbstractShenyuPlugin {
                         Object error = ShenyuResultWrap.error(exchange, 
ShenyuResultEnum.TOO_MANY_REQUESTS);
                         return WebFluxResultUtils.result(exchange, error);
                     }
-                    return chain.execute(exchange);
+                    return chain.execute(exchange).doFinally(signalType -> {
+                        RateLimiterAlgorithm<?> rateLimiterAlgorithm = 
RateLimiterAlgorithmFactory.newInstance(limiterHandle.getAlgorithmName());
+                        
rateLimiterAlgorithm.callback(rateLimiterAlgorithm.getScript(), 
response.getKeys(), null);
+                    });
                 });
     }
 }
diff --git 
a/shenyu-plugin/shenyu-plugin-ratelimiter/src/main/java/org/apache/shenyu/plugin/ratelimiter/executor/RedisRateLimiter.java
 
b/shenyu-plugin/shenyu-plugin-ratelimiter/src/main/java/org/apache/shenyu/plugin/ratelimiter/executor/RedisRateLimiter.java
index ef0203c5b..d679390ff 100644
--- 
a/shenyu-plugin/shenyu-plugin-ratelimiter/src/main/java/org/apache/shenyu/plugin/ratelimiter/executor/RedisRateLimiter.java
+++ 
b/shenyu-plugin/shenyu-plugin-ratelimiter/src/main/java/org/apache/shenyu/plugin/ratelimiter/executor/RedisRateLimiter.java
@@ -65,10 +65,12 @@ public class RedisRateLimiter {
                 }).map(results -> {
                     boolean allowed = results.get(0) == 1L;
                     Long tokensLeft = results.get(1);
-                    return new RateLimiterResponse(allowed, tokensLeft);
+                    return new RateLimiterResponse(allowed, tokensLeft, keys);
                 })
-                .doOnError(throwable -> LOG.error("Error occurred while 
judging if user is allowed by RedisRateLimiter:{}", throwable.getMessage()))
-                .doFinally(signalType -> rateLimiterAlgorithm.callback(script, 
keys, scriptArgs));
+                .doOnError(throwable -> {
+                    
rateLimiterAlgorithm.callback(rateLimiterAlgorithm.getScript(), keys, 
scriptArgs);
+                    LOG.error("Error occurred while judging if user is allowed 
by RedisRateLimiter:{}", throwable.getMessage());
+                });
     }
     
     private String doubleToString(final double param) {
diff --git 
a/shenyu-plugin/shenyu-plugin-ratelimiter/src/main/java/org/apache/shenyu/plugin/ratelimiter/response/RateLimiterResponse.java
 
b/shenyu-plugin/shenyu-plugin-ratelimiter/src/main/java/org/apache/shenyu/plugin/ratelimiter/response/RateLimiterResponse.java
index a871cdaac..534cae66d 100644
--- 
a/shenyu-plugin/shenyu-plugin-ratelimiter/src/main/java/org/apache/shenyu/plugin/ratelimiter/response/RateLimiterResponse.java
+++ 
b/shenyu-plugin/shenyu-plugin-ratelimiter/src/main/java/org/apache/shenyu/plugin/ratelimiter/response/RateLimiterResponse.java
@@ -18,6 +18,7 @@
 package org.apache.shenyu.plugin.ratelimiter.response;
 
 import java.io.Serializable;
+import java.util.List;
 
 /**
  * rateLimiter response.
@@ -30,15 +31,19 @@ public class RateLimiterResponse implements Serializable {
 
     private final long tokensRemaining;
 
+    private final List<String> keys;
+
     /**
      * Instantiates a new Rate limiter response.
      *
      * @param allowed         the allowed
      * @param tokensRemaining the tokens remaining
+     * @param keys the redis keys
      */
-    public RateLimiterResponse(final boolean allowed, final long 
tokensRemaining) {
+    public RateLimiterResponse(final boolean allowed, final long 
tokensRemaining, final List<String> keys) {
         this.allowed = allowed;
         this.tokensRemaining = tokensRemaining;
+        this.keys = keys;
     }
 
     /**
@@ -59,6 +64,16 @@ public class RateLimiterResponse implements Serializable {
         return tokensRemaining;
     }
 
+
+    /**
+     * get redis keys.
+     *
+     * @return getKeys
+     */
+    public List<String> getKeys() {
+        return keys;
+    }
+
     @Override
     public String toString() {
         return "Response{" + "allowed=" + allowed + ", tokensRemaining=" + 
tokensRemaining + '}';
diff --git 
a/shenyu-plugin/shenyu-plugin-ratelimiter/src/test/java/org/apache/shenyu/plugin/ratelimiter/RateLimiterPluginTest.java
 
b/shenyu-plugin/shenyu-plugin-ratelimiter/src/test/java/org/apache/shenyu/plugin/ratelimiter/RateLimiterPluginTest.java
index 11f696230..2b1839276 100644
--- 
a/shenyu-plugin/shenyu-plugin-ratelimiter/src/test/java/org/apache/shenyu/plugin/ratelimiter/RateLimiterPluginTest.java
+++ 
b/shenyu-plugin/shenyu-plugin-ratelimiter/src/test/java/org/apache/shenyu/plugin/ratelimiter/RateLimiterPluginTest.java
@@ -85,7 +85,7 @@ public final class RateLimiterPluginTest {
     public void doExecuteAllowedTest() {
         doExecutePreInit();
         when(redisRateLimiter.isAllowed(anyString(), 
any(RateLimiterHandle.class))).thenReturn(
-                Mono.just(new RateLimiterResponse(true, 1)));
+                Mono.just(new RateLimiterResponse(true, 1, null)));
         Mono<Void> result = rateLimiterPlugin.doExecute(exchange, chain, 
selectorData, ruleData);
         StepVerifier.create(result).expectSubscription().verifyComplete();
     }
@@ -97,7 +97,7 @@ public final class RateLimiterPluginTest {
     public void doExecuteNotAllowedTest() {
         doExecutePreInit();
         when(redisRateLimiter.isAllowed(anyString(), 
any(RateLimiterHandle.class))).thenReturn(
-                Mono.just(new RateLimiterResponse(false, 1)));
+                Mono.just(new RateLimiterResponse(false, 1, null)));
         ConfigurableApplicationContext context = 
mock(ConfigurableApplicationContext.class);
         when(context.getBean(ShenyuResult.class)).thenReturn(new 
DefaultShenyuResult());
         SpringBeanUtils.getInstance().setApplicationContext(context);

Reply via email to