Search in sources :

Example 1 with LimitAPI

use of com.tencent.polaris.ratelimit.api.core.LimitAPI in project polaris-java by polarismesh.

the class LocalTest method testSingleThreadLimit.

@Test
public void testSingleThreadLimit() {
    Configuration configuration = TestUtils.configWithEnvAddress();
    try (LimitAPI limitAPI = LimitAPIFactory.createLimitAPIByConfig(configuration)) {
        adjustTime();
        testQuotaAcquire(limitAPI, Consts.createSingleValueMap(new String[] { Consts.LABEL_METHOD }, new String[] { Consts.METHOD_PAY }), Consts.MAX_PAY_COUNT);
        testQuotaAcquire(limitAPI, Consts.createSingleValueMap(new String[] { Consts.LABEL_METHOD }, new String[] { Consts.METHOD_CASH }), Consts.MAX_CASH_COUNT);
        testQuotaAcquire(limitAPI, null, Consts.MAX_SERVICE_COUNT);
        System.out.println("start to wait expired");
        Utils.sleepUninterrupted(10 * 1000);
    }
}
Also used : Configuration(com.tencent.polaris.api.config.Configuration) LimitAPI(com.tencent.polaris.ratelimit.api.core.LimitAPI) MatchString(com.tencent.polaris.client.pb.ModelProto.MatchString) Test(org.junit.Test)

Example 2 with LimitAPI

use of com.tencent.polaris.ratelimit.api.core.LimitAPI in project polaris-java by polarismesh.

the class RateLimitExample method main.

public static void main(String[] args) throws Exception {
    InitResult initResult = LimitExampleUtils.initRateLimitConfiguration(args);
    String namespace = initResult.getNamespace();
    String service = initResult.getService();
    int concurrency = initResult.getConcurrency();
    ScheduledExecutorService executorService = Executors.newScheduledThreadPool(concurrency);
    // 注意:使用本地限流时,限流阈值计数器会存放在LimitAPI实例内部,无法跨实例共享,因此LimitAPI建议通过进程单例模式使用
    try (LimitAPI limitAPI = LimitAPIFactory.createLimitAPI()) {
        Runnable runnable = new Runnable() {

            @Override
            public void run() {
                QuotaRequest quotaRequest = new QuotaRequest();
                quotaRequest.setNamespace(namespace);
                quotaRequest.setService(service);
                quotaRequest.setMethod("echo");
                quotaRequest.setCount(1);
                QuotaResponse quotaResponse = limitAPI.getQuota(quotaRequest);
                System.out.println("quotaResponse is " + quotaResponse.getCode());
            }
        };
        List<ScheduledFuture<?>> futures = new ArrayList<>();
        for (int i = 0; i < concurrency; i++) {
            ScheduledFuture<?> scheduledFuture = executorService.scheduleWithFixedDelay(runnable, 10 + i, 500, TimeUnit.MILLISECONDS);
            futures.add(scheduledFuture);
        }
        Thread.sleep(500000);
        for (ScheduledFuture<?> future : futures) {
            future.cancel(true);
        }
    }
    executorService.shutdown();
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) InitResult(com.tencent.polaris.ratelimit.example.utils.LimitExampleUtils.InitResult) ArrayList(java.util.ArrayList) QuotaRequest(com.tencent.polaris.ratelimit.api.rpc.QuotaRequest) LimitAPI(com.tencent.polaris.ratelimit.api.core.LimitAPI) QuotaResponse(com.tencent.polaris.ratelimit.api.rpc.QuotaResponse) ScheduledFuture(java.util.concurrent.ScheduledFuture)

Example 3 with LimitAPI

use of com.tencent.polaris.ratelimit.api.core.LimitAPI 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 4 with LimitAPI

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

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

LimitAPI (com.tencent.polaris.ratelimit.api.core.LimitAPI)5 QuotaRequest (com.tencent.polaris.ratelimit.api.rpc.QuotaRequest)4 QuotaResponse (com.tencent.polaris.ratelimit.api.rpc.QuotaResponse)4 QuotaResult (com.tencent.polaris.api.plugin.ratelimiter.QuotaResult)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 Configuration (com.tencent.polaris.api.config.Configuration)1 MatchString (com.tencent.polaris.client.pb.ModelProto.MatchString)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 Test (org.junit.Test)1