Search in sources :

Example 6 with QuotaRequest

use of com.tencent.polaris.ratelimit.api.rpc.QuotaRequest 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 7 with QuotaRequest

use of com.tencent.polaris.ratelimit.api.rpc.QuotaRequest in project polaris-java by polarismesh.

the class APIFacade method getQuota.

public static boolean getQuota(String namespace, String service, String method, Map<String, String> labels, int count) {
    if (!inited.get()) {
        LOGGER.info("polaris not inited, getQuota fail");
        return false;
    }
    QuotaRequest quotaRequest = new QuotaRequest();
    quotaRequest.setNamespace(namespace);
    quotaRequest.setService(service);
    quotaRequest.setMethod(method);
    quotaRequest.setLabels(labels);
    quotaRequest.setCount(count);
    QuotaResponse quota = limitAPI.getQuota(quotaRequest);
    return quota.getCode() == QuotaResultCode.QuotaResultOk;
}
Also used : QuotaRequest(com.tencent.polaris.ratelimit.api.rpc.QuotaRequest) QuotaResponse(com.tencent.polaris.ratelimit.api.rpc.QuotaResponse)

Example 8 with QuotaRequest

use of com.tencent.polaris.ratelimit.api.rpc.QuotaRequest in project polaris-java by polarismesh.

the class LocalTest method testQuotaAcquire.

private static void testQuotaAcquire(LimitAPI limitAPI, Map<String, String> labels, int maxCount) {
    QuotaRequest payRequest = new QuotaRequest();
    payRequest.setNamespace(Consts.NAMESPACE_TEST);
    payRequest.setService(Consts.LOCAL_LIMIT_SERVICE);
    payRequest.setCount(1);
    payRequest.setLabels(labels);
    boolean payLimit = false;
    boolean payPass = false;
    for (int i = 0; i < maxCount; i++) {
        QuotaResponse response = limitAPI.getQuota(payRequest);
        if (response.getCode() == QuotaResultCode.QuotaResultOk) {
            payPass = true;
        } else if (response.getCode() == QuotaResultCode.QuotaResultLimited) {
            payLimit = true;
        }
    }
    Assert.assertTrue(payPass);
    Assert.assertTrue(payLimit);
}
Also used : QuotaRequest(com.tencent.polaris.ratelimit.api.rpc.QuotaRequest) QuotaResponse(com.tencent.polaris.ratelimit.api.rpc.QuotaResponse)

Aggregations

QuotaRequest (com.tencent.polaris.ratelimit.api.rpc.QuotaRequest)8 QuotaResponse (com.tencent.polaris.ratelimit.api.rpc.QuotaResponse)8 QuotaResult (com.tencent.polaris.api.plugin.ratelimiter.QuotaResult)4 LimitAPI (com.tencent.polaris.ratelimit.api.core.LimitAPI)4 Before (org.junit.Before)3 RateLimitRuleLabelResolver (com.tencent.cloud.polaris.ratelimit.RateLimitRuleLabelResolver)2 PolarisRateLimitProperties (com.tencent.cloud.polaris.ratelimit.config.PolarisRateLimitProperties)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 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