diff --git a/CHANGELOG.md b/CHANGELOG.md index 9357b5f55..6f5a97cae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,3 +12,4 @@ - [fix:solve ratelimit-callee-service UnknownHostException.](https://github.com/Tencent/spring-cloud-tencent/pull/291) - [Update GitHub Actions workflow](https://github.com/Tencent/spring-cloud-tencent/pull/295) - [Feature:add restTemplate Report Polaris](https://github.com/Tencent/spring-cloud-tencent/pull/300) +- [Fix the current limiting effect is that other requests cannot be processed when queuing at a constant speed](https://github.com/Tencent/spring-cloud-tencent/pull/317) diff --git a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilter.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilter.java index 8796fc1ab..31320dda0 100644 --- a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilter.java +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilter.java @@ -19,6 +19,7 @@ package com.tencent.cloud.polaris.ratelimit.filter; import java.nio.charset.StandardCharsets; +import java.time.Duration; import java.util.HashMap; import java.util.Map; import java.util.Set; @@ -55,7 +56,7 @@ import static com.tencent.cloud.polaris.ratelimit.constant.RateLimitConstant.LAB /** * Reactive filter to check quota. * - * @author Haotian Zhang, lepdou, cheese8 + * @author Haotian Zhang, lepdou, cheese8, kaiy */ public class QuotaCheckReactiveFilter implements WebFilter, Ordered { @@ -113,7 +114,7 @@ public class QuotaCheckReactiveFilter implements WebFilter, Ordered { } // Unirate if (quotaResponse.getCode() == QuotaResultCode.QuotaResultOk && quotaResponse.getWaitMs() > 0) { - Thread.sleep(quotaResponse.getWaitMs()); + return Mono.delay(Duration.ofMillis(quotaResponse.getWaitMs())).flatMap(e -> chain.filter(exchange)); } } catch (Throwable t) { diff --git a/spring-cloud-starter-tencent-polaris-ratelimit/src/test/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilterTest.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/test/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilterTest.java index 0a9a4988f..6d3ef804f 100644 --- a/spring-cloud-starter-tencent-polaris-ratelimit/src/test/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilterTest.java +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/test/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilterTest.java @@ -22,6 +22,7 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Collections; import java.util.Map; +import java.util.concurrent.CountDownLatch; import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.util.ApplicationContextAwareUtils; @@ -65,7 +66,7 @@ import static org.mockito.Mockito.when; /** * Test for {@link QuotaCheckReactiveFilter}. * - * @author Haotian Zhang, cheese8 + * @author Haotian Zhang, cheese8, kaiy */ @RunWith(MockitoJUnitRunner.class) @SpringBootTest(classes = QuotaCheckReactiveFilterTest.TestApplication.class, properties = { @@ -201,7 +202,14 @@ public class QuotaCheckReactiveFilterTest { // Unirate waiting 1000ms MetadataContext.LOCAL_SERVICE = "TestApp2"; long startTimestamp = System.currentTimeMillis(); - quotaCheckReactiveFilter.filter(exchange, webFilterChain); + CountDownLatch countDownLatch = new CountDownLatch(1); + quotaCheckReactiveFilter.filter(exchange, webFilterChain).subscribe(e -> { }, t -> { }, countDownLatch::countDown); + try { + countDownLatch.await(); + } + catch (InterruptedException e) { + fail("Exception encountered.", e); + } assertThat(System.currentTimeMillis() - startTimestamp).isGreaterThanOrEqualTo(1000L); // Rate limited