Search in sources :

Example 1 with QuotaResult

use of com.tencent.polaris.api.plugin.ratelimiter.QuotaResult in project polaris-java by polarismesh.

the class QuotaFlow method getQuota.

public QuotaResponse getQuota(CommonQuotaRequest request) throws PolarisException {
    RateLimitWindow rateLimitWindow = lookupRateLimitWindow(request);
    if (null == rateLimitWindow) {
        // 没有限流规则,直接放通
        return new QuotaResponse(new QuotaResult(QuotaResult.Code.QuotaResultOk, 0, RateLimitConstants.RULE_NOT_EXISTS));
    }
    rateLimitWindow.init();
    return new QuotaResponse(rateLimitWindow.allocateQuota(request.getCount()));
}
Also used : QuotaResult(com.tencent.polaris.api.plugin.ratelimiter.QuotaResult) QuotaResponse(com.tencent.polaris.ratelimit.api.rpc.QuotaResponse)

Example 2 with QuotaResult

use of com.tencent.polaris.api.plugin.ratelimiter.QuotaResult in project spring-cloud-tencent by Tencent.

the class QuotaCheckUtilsTest method setUp.

@Before
public void setUp() {
    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 {
            throw new RuntimeException("Mock exception.");
        }
    });
}
Also used : QuotaResult(com.tencent.polaris.api.plugin.ratelimiter.QuotaResult) QuotaRequest(com.tencent.polaris.ratelimit.api.rpc.QuotaRequest) LimitAPI(com.tencent.polaris.ratelimit.api.core.LimitAPI) QuotaResponse(com.tencent.polaris.ratelimit.api.rpc.QuotaResponse) Before(org.junit.Before)

Example 3 with QuotaResult

use of com.tencent.polaris.api.plugin.ratelimiter.QuotaResult in project spring-cloud-tencent by Tencent.

the class QuotaCheckUtils method getQuota.

public static QuotaResponse getQuota(LimitAPI limitAPI, String namespace, String service, int count, Map<String, String> labels, String method) {
    // build quota request
    QuotaRequest quotaRequest = new QuotaRequest();
    quotaRequest.setNamespace(namespace);
    quotaRequest.setService(service);
    quotaRequest.setCount(count);
    quotaRequest.setLabels(labels);
    quotaRequest.setMethod(method);
    try {
        return limitAPI.getQuota(quotaRequest);
    } catch (Throwable throwable) {
        LOG.error("fail to invoke getQuota of LimitAPI with QuotaRequest[{}].", quotaRequest, throwable);
        return new QuotaResponse(new QuotaResult(QuotaResult.Code.QuotaResultOk, 0, "get quota failed"));
    }
}
Also used : QuotaResult(com.tencent.polaris.api.plugin.ratelimiter.QuotaResult) QuotaRequest(com.tencent.polaris.ratelimit.api.rpc.QuotaRequest) QuotaResponse(com.tencent.polaris.ratelimit.api.rpc.QuotaResponse)

Example 4 with QuotaResult

use of com.tencent.polaris.api.plugin.ratelimiter.QuotaResult 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 5 with QuotaResult

use of com.tencent.polaris.api.plugin.ratelimiter.QuotaResult 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)

Aggregations

QuotaResult (com.tencent.polaris.api.plugin.ratelimiter.QuotaResult)6 QuotaResponse (com.tencent.polaris.ratelimit.api.rpc.QuotaResponse)5 QuotaRequest (com.tencent.polaris.ratelimit.api.rpc.QuotaRequest)4 LimitAPI (com.tencent.polaris.ratelimit.api.core.LimitAPI)3 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 UpdateIdentifier (com.tencent.polaris.plugins.ratelimiter.common.bucket.UpdateIdentifier)1 AllocateResult (com.tencent.polaris.plugins.ratelimiter.reject.TokenBucket.AllocateResult)1 TokenBucketMode (com.tencent.polaris.plugins.ratelimiter.reject.TokenBucket.TokenBucketMode)1