Search in sources :

Example 11 with QuotaResponse

use of com.tencent.polaris.ratelimit.api.rpc.QuotaResponse in project spring-cloud-tencent by Tencent.

the class CalleeControllerTests method setUp.

@Before
public void setUp() {
    QuotaResponse quotaResponse = mock(QuotaResponse.class);
    when(quotaResponse.getCode()).thenReturn(QuotaResultCode.QuotaResultOk);
    when(limitAPI.getQuota(any())).thenReturn(quotaResponse);
}
Also used : QuotaResponse(com.tencent.polaris.ratelimit.api.rpc.QuotaResponse) Before(org.junit.Before)

Example 12 with QuotaResponse

use of com.tencent.polaris.ratelimit.api.rpc.QuotaResponse in project spring-cloud-tencent by Tencent.

the class QuotaCheckReactiveFilterTest method setUp.

@Before
public void setUp() {
    MetadataContext.LOCAL_NAMESPACE = "TEST";
    LimitAPI limitAPI = mock(LimitAPI.class);
    when(limitAPI.getQuota(any(QuotaRequest.class))).thenAnswer(invocationOnMock -> {
        String serviceName = ((QuotaRequest) invocationOnMock.getArgument(0)).getService();
        if (serviceName.equals("TestApp1")) {
            return new QuotaResponse(new QuotaResult(QuotaResult.Code.QuotaResultOk, 0, "QuotaResultOk"));
        } else if (serviceName.equals("TestApp2")) {
            return new QuotaResponse(new QuotaResult(QuotaResult.Code.QuotaResultOk, 1000, "QuotaResultOk"));
        } else if (serviceName.equals("TestApp3")) {
            return new QuotaResponse(new QuotaResult(QuotaResult.Code.QuotaResultLimited, 0, "QuotaResultLimited"));
        } else {
            return new QuotaResponse(new QuotaResult(null, 0, null));
        }
    });
    PolarisRateLimitProperties polarisRateLimitProperties = new PolarisRateLimitProperties();
    polarisRateLimitProperties.setRejectRequestTips("RejectRequestTips");
    polarisRateLimitProperties.setRejectHttpCode(419);
    RateLimitRuleLabelResolver rateLimitRuleLabelResolver = mock(RateLimitRuleLabelResolver.class);
    when(rateLimitRuleLabelResolver.getExpressionLabelKeys(anyString(), anyString())).thenReturn(Collections.EMPTY_SET);
    this.quotaCheckReactiveFilter = new QuotaCheckReactiveFilter(limitAPI, labelResolver, polarisRateLimitProperties, rateLimitRuleLabelResolver);
}
Also used : QuotaResult(com.tencent.polaris.api.plugin.ratelimiter.QuotaResult) RateLimitRuleLabelResolver(com.tencent.cloud.polaris.ratelimit.RateLimitRuleLabelResolver) QuotaRequest(com.tencent.polaris.ratelimit.api.rpc.QuotaRequest) LimitAPI(com.tencent.polaris.ratelimit.api.core.LimitAPI) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PolarisRateLimitProperties(com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitProperties) QuotaResponse(com.tencent.polaris.ratelimit.api.rpc.QuotaResponse) Before(org.junit.Before)

Example 13 with QuotaResponse

use of com.tencent.polaris.ratelimit.api.rpc.QuotaResponse in project spring-cloud-tencent by Tencent.

the class QuotaCheckServletFilterTest method setUp.

@Before
public void setUp() {
    MetadataContext.LOCAL_NAMESPACE = "TEST";
    LimitAPI limitAPI = mock(LimitAPI.class);
    when(limitAPI.getQuota(any(QuotaRequest.class))).thenAnswer(invocationOnMock -> {
        String serviceName = ((QuotaRequest) invocationOnMock.getArgument(0)).getService();
        if (serviceName.equals("TestApp1")) {
            return new QuotaResponse(new QuotaResult(QuotaResult.Code.QuotaResultOk, 0, "QuotaResultOk"));
        } else if (serviceName.equals("TestApp2")) {
            return new QuotaResponse(new QuotaResult(QuotaResult.Code.QuotaResultOk, 1000, "QuotaResultOk"));
        } else if (serviceName.equals("TestApp3")) {
            return new QuotaResponse(new QuotaResult(QuotaResult.Code.QuotaResultLimited, 0, "QuotaResultLimited"));
        } else {
            return new QuotaResponse(new QuotaResult(null, 0, null));
        }
    });
    PolarisRateLimitProperties polarisRateLimitProperties = new PolarisRateLimitProperties();
    polarisRateLimitProperties.setRejectRequestTips("RejectRequestTips");
    polarisRateLimitProperties.setRejectHttpCode(419);
    RateLimitRuleLabelResolver rateLimitRuleLabelResolver = mock(RateLimitRuleLabelResolver.class);
    when(rateLimitRuleLabelResolver.getExpressionLabelKeys(anyString(), anyString())).thenReturn(Collections.EMPTY_SET);
    this.quotaCheckServletFilter = new QuotaCheckServletFilter(limitAPI, labelResolver, polarisRateLimitProperties, rateLimitRuleLabelResolver);
}
Also used : QuotaResult(com.tencent.polaris.api.plugin.ratelimiter.QuotaResult) RateLimitRuleLabelResolver(com.tencent.cloud.polaris.ratelimit.RateLimitRuleLabelResolver) QuotaRequest(com.tencent.polaris.ratelimit.api.rpc.QuotaRequest) LimitAPI(com.tencent.polaris.ratelimit.api.core.LimitAPI) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PolarisRateLimitProperties(com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitProperties) QuotaResponse(com.tencent.polaris.ratelimit.api.rpc.QuotaResponse) Before(org.junit.Before)

Example 14 with QuotaResponse

use of com.tencent.polaris.ratelimit.api.rpc.QuotaResponse in project spring-cloud-tencent by Tencent.

the class QuotaCheckUtilsTest method testGetQuota.

@Test
public void testGetQuota() {
    // Pass
    String serviceName = "TestApp1";
    QuotaResponse quotaResponse = QuotaCheckUtils.getQuota(limitAPI, null, serviceName, 1, null, null);
    assertThat(quotaResponse.getCode()).isEqualTo(QuotaResultCode.QuotaResultOk);
    assertThat(quotaResponse.getWaitMs()).isEqualTo(0);
    assertThat(quotaResponse.getInfo()).isEqualTo("QuotaResultOk");
    // Unirate waiting 1000ms
    serviceName = "TestApp2";
    quotaResponse = QuotaCheckUtils.getQuota(limitAPI, null, serviceName, 1, null, null);
    assertThat(quotaResponse.getCode()).isEqualTo(QuotaResultCode.QuotaResultOk);
    assertThat(quotaResponse.getWaitMs()).isEqualTo(1000);
    assertThat(quotaResponse.getInfo()).isEqualTo("QuotaResultOk");
    // Rate limited
    serviceName = "TestApp3";
    quotaResponse = QuotaCheckUtils.getQuota(limitAPI, null, serviceName, 1, null, null);
    assertThat(quotaResponse.getCode()).isEqualTo(QuotaResultCode.QuotaResultLimited);
    assertThat(quotaResponse.getWaitMs()).isEqualTo(0);
    assertThat(quotaResponse.getInfo()).isEqualTo("QuotaResultLimited");
    // Exception
    serviceName = "TestApp4";
    quotaResponse = QuotaCheckUtils.getQuota(limitAPI, null, serviceName, 1, null, null);
    assertThat(quotaResponse.getCode()).isEqualTo(QuotaResultCode.QuotaResultOk);
    assertThat(quotaResponse.getWaitMs()).isEqualTo(0);
    assertThat(quotaResponse.getInfo()).isEqualTo("get quota failed");
}
Also used : QuotaResponse(com.tencent.polaris.ratelimit.api.rpc.QuotaResponse) Test(org.junit.Test)

Example 15 with QuotaResponse

use of com.tencent.polaris.ratelimit.api.rpc.QuotaResponse in project spring-cloud-tencent by Tencent.

the class QuotaCheckReactiveFilter method filter.

@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
    String localNamespace = MetadataContext.LOCAL_NAMESPACE;
    String localService = MetadataContext.LOCAL_SERVICE;
    Map<String, String> labels = getRequestLabels(exchange, localNamespace, localService);
    try {
        String path = exchange.getRequest().getURI().getPath();
        QuotaResponse quotaResponse = QuotaCheckUtils.getQuota(limitAPI, localNamespace, localService, 1, labels, path);
        if (quotaResponse.getCode() == QuotaResultCode.QuotaResultLimited) {
            ServerHttpResponse response = exchange.getResponse();
            response.setRawStatusCode(polarisRateLimitProperties.getRejectHttpCode());
            response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
            DataBuffer dataBuffer = response.bufferFactory().allocateBuffer().write(rejectTips.getBytes(StandardCharsets.UTF_8));
            return response.writeWith(Mono.just(dataBuffer));
        }
        // Unirate
        if (quotaResponse.getCode() == QuotaResultCode.QuotaResultOk && quotaResponse.getWaitMs() > 0) {
            Thread.sleep(quotaResponse.getWaitMs());
        }
    } catch (Throwable t) {
        // An exception occurs in the rate limiting API call,
        // which should not affect the call of the business process.
        LOG.error("fail to invoke getQuota, service is " + localService, t);
    }
    return chain.filter(exchange);
}
Also used : QuotaResponse(com.tencent.polaris.ratelimit.api.rpc.QuotaResponse) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Aggregations

QuotaResponse (com.tencent.polaris.ratelimit.api.rpc.QuotaResponse)18 QuotaRequest (com.tencent.polaris.ratelimit.api.rpc.QuotaRequest)8 QuotaResult (com.tencent.polaris.api.plugin.ratelimiter.QuotaResult)5 LimitAPI (com.tencent.polaris.ratelimit.api.core.LimitAPI)4 Before (org.junit.Before)4 RateLimitRuleLabelResolver (com.tencent.cloud.polaris.ratelimit.RateLimitRuleLabelResolver)2 PolarisRateLimitProperties (com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitProperties)2 Test (org.junit.Test)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 DataBuffer (org.springframework.core.io.buffer.DataBuffer)2 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)2 RequestContext (com.netflix.zuul.context.RequestContext)1 MetadataContext (com.tencent.cloud.metadata.context.MetadataContext)1 CommonQuotaRequest (com.tencent.polaris.ratelimit.client.pojo.CommonQuotaRequest)1 InitResult (com.tencent.polaris.ratelimit.example.utils.LimitExampleUtils.InitResult)1 ArrayList (java.util.ArrayList)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 TooManyRequests (org.springframework.web.client.HttpClientErrorException.TooManyRequests)1