Search in sources :

Example 16 with QuotaResponse

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

the class QuotaCheckFilter method doFilterInternal.

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    String localNamespace = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.LOCAL_NAMESPACE);
    String localService = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.LOCAL_SERVICE);
    String method = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.LOCAL_PATH);
    Map<String, String> labels = null;
    if (StringUtils.isNotBlank(method)) {
        labels = new HashMap<>();
        labels.put("method", method);
    }
    try {
        QuotaResponse quotaResponse = QuotaCheckUtils.getQuota(limitAPI, localNamespace, localService, 1, labels, null);
        if (quotaResponse.getCode() == QuotaResultCode.QuotaResultLimited) {
            response.setStatus(TOO_MANY_REQUESTS.value());
            response.getWriter().write(Consts.QUOTA_LIMITED_INFO + quotaResponse.getInfo());
        } else {
            filterChain.doFilter(request, response);
        }
    } catch (Throwable t) {
        // 限流API调用出现异常,不应该影响业务流程的调用
        LOG.error("fail to invoke getQuota, service is " + localService, t);
        filterChain.doFilter(request, response);
    }
}
Also used : QuotaResponse(com.tencent.polaris.ratelimit.api.rpc.QuotaResponse)

Example 17 with QuotaResponse

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

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

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