fix the current limiting effect is that other requests cannot be processed when queuing at a constant speed (#316)

pull/319/head
kaiybaby 2 years ago committed by GitHub
parent 6a870b9935
commit db8184a576
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -18,3 +18,4 @@
- [UT: add metadata-transfer unit test](https://github.com/Tencent/spring-cloud-tencent/pull/294) - [UT: add metadata-transfer unit test](https://github.com/Tencent/spring-cloud-tencent/pull/294)
- [Feature:add restTemplate Report Polaris](https://github.com/Tencent/spring-cloud-tencent/pull/272) - [Feature:add restTemplate Report Polaris](https://github.com/Tencent/spring-cloud-tencent/pull/272)
- [Use jdk constants instead of magic variables](https://github.com/Tencent/spring-cloud-tencent/pull/313) - [Use jdk constants instead of magic variables](https://github.com/Tencent/spring-cloud-tencent/pull/313)
- [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/316)

@ -19,6 +19,7 @@
package com.tencent.cloud.polaris.ratelimit.filter; package com.tencent.cloud.polaris.ratelimit.filter;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -55,7 +56,7 @@ import static com.tencent.cloud.polaris.ratelimit.constant.RateLimitConstant.LAB
/** /**
* Reactive filter to check quota. * Reactive filter to check quota.
* *
* @author Haotian Zhang, lepdou, cheese8 * @author Haotian Zhang, lepdou, cheese8, kaiy
*/ */
public class QuotaCheckReactiveFilter implements WebFilter, Ordered { public class QuotaCheckReactiveFilter implements WebFilter, Ordered {
@ -113,7 +114,7 @@ public class QuotaCheckReactiveFilter implements WebFilter, Ordered {
} }
// Unirate // Unirate
if (quotaResponse.getCode() == QuotaResultCode.QuotaResultOk && quotaResponse.getWaitMs() > 0) { 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) { catch (Throwable t) {

@ -22,6 +22,7 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.concurrent.CountDownLatch;
import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.util.ApplicationContextAwareUtils; import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
@ -65,7 +66,7 @@ import static org.mockito.Mockito.when;
/** /**
* Test for {@link QuotaCheckReactiveFilter}. * Test for {@link QuotaCheckReactiveFilter}.
* *
* @author Haotian Zhang, cheese8 * @author Haotian Zhang, cheese8, kaiy
*/ */
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
@SpringBootTest(classes = QuotaCheckReactiveFilterTest.TestApplication.class, properties = { @SpringBootTest(classes = QuotaCheckReactiveFilterTest.TestApplication.class, properties = {
@ -201,7 +202,14 @@ public class QuotaCheckReactiveFilterTest {
// Unirate waiting 1000ms // Unirate waiting 1000ms
MetadataContext.LOCAL_SERVICE = "TestApp2"; MetadataContext.LOCAL_SERVICE = "TestApp2";
long startTimestamp = System.currentTimeMillis(); 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); assertThat(System.currentTimeMillis() - startTimestamp).isGreaterThanOrEqualTo(1000L);
// Rate limited // Rate limited

Loading…
Cancel
Save