Search in sources :

Example 1 with DefaultRateLimitResult

use of com.tencent.polaris.api.plugin.stat.DefaultRateLimitResult in project polaris-java by polarismesh.

the class PrometheusPushHandlerTest method testRateLimitStrategy.

@Test
public void testRateLimitStrategy() throws InterruptedException {
    int count = 10;
    int passedNum = 3;
    CountDownLatch latch = new CountDownLatch(2);
    new Thread(() -> {
        try {
            batchDone(() -> {
                StatInfo statInfo = new StatInfo();
                DefaultRateLimitResult rateLimitResult = mockFixedRateLimitResult(RateLimitGauge.Result.PASSED);
                statInfo.setRateLimitGauge(rateLimitResult);
                handler.handle(statInfo);
            }, passedNum);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        latch.countDown();
    }).start();
    new Thread(() -> {
        try {
            batchDone(() -> {
                StatInfo statInfo = new StatInfo();
                DefaultRateLimitResult rateLimitResult = mockFixedRateLimitResult(RateLimitGauge.Result.LIMITED);
                statInfo.setRateLimitGauge(rateLimitResult);
                handler.handle(statInfo);
            }, count - passedNum);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        latch.countDown();
    }).start();
    latch.await();
    Thread.sleep(pushInterval + 1000);
    handler.stopHandle();
    DefaultRateLimitResult example = mockFixedRateLimitResult(RateLimitGauge.Result.LIMITED);
    // assert result
    Double totalResult = getRateLimitTotalResult(example);
    Double passResult = getRateLimitPassedResult(example);
    Double limitResult = getRateLimitLimitedResult(example);
    Assert.assertEquals(new Double(count), totalResult);
    Assert.assertEquals(new Double(passedNum), passResult);
    Assert.assertEquals(new Double(count - passedNum), limitResult);
}
Also used : DefaultRateLimitResult(com.tencent.polaris.api.plugin.stat.DefaultRateLimitResult) CountDownLatch(java.util.concurrent.CountDownLatch) StatInfo(com.tencent.polaris.api.plugin.stat.StatInfo) Test(org.junit.Test)

Example 2 with DefaultRateLimitResult

use of com.tencent.polaris.api.plugin.stat.DefaultRateLimitResult in project polaris-java by polarismesh.

the class DefaultLimitAPI method reportRateLimit.

private void reportRateLimit(QuotaRequest req, QuotaResponse rsp) {
    if (null != statPlugins) {
        try {
            DefaultRateLimitResult rateLimitGauge = new DefaultRateLimitResult();
            rateLimitGauge.setLabels(formatLabelsToStr(req.getLabels()));
            rateLimitGauge.setMethod(req.getMethod());
            rateLimitGauge.setNamespace(req.getNamespace());
            rateLimitGauge.setService(req.getService());
            rateLimitGauge.setResult(rsp.getCode() == QuotaResultOk ? RateLimitGauge.Result.PASSED : RateLimitGauge.Result.LIMITED);
            StatInfo statInfo = new StatInfo();
            statInfo.setRateLimitGauge(rateLimitGauge);
            for (Plugin statPlugin : statPlugins) {
                if (statPlugin instanceof StatReporter) {
                    ((StatReporter) statPlugin).reportStat(statInfo);
                }
            }
        } catch (Exception ex) {
            LOG.info("rate limit report encountered exception, e: {}", ex.getMessage());
        }
    }
}
Also used : DefaultRateLimitResult(com.tencent.polaris.api.plugin.stat.DefaultRateLimitResult) StatReporter(com.tencent.polaris.api.plugin.stat.StatReporter) StatInfo(com.tencent.polaris.api.plugin.stat.StatInfo) PolarisException(com.tencent.polaris.api.exception.PolarisException) Plugin(com.tencent.polaris.api.plugin.Plugin)

Example 3 with DefaultRateLimitResult

use of com.tencent.polaris.api.plugin.stat.DefaultRateLimitResult in project polaris-java by polarismesh.

the class PrometheusPushHandlerTest method mockFixedRateLimitResult.

private DefaultRateLimitResult mockFixedRateLimitResult(RateLimitGauge.Result result) {
    DefaultRateLimitResult rateLimitResult = new DefaultRateLimitResult();
    rateLimitResult.setMethod("GET");
    rateLimitResult.setLabels("a:b|c:d");
    rateLimitResult.setService("callService");
    rateLimitResult.setNamespace("callNamespace");
    rateLimitResult.setResult(result);
    return rateLimitResult;
}
Also used : DefaultRateLimitResult(com.tencent.polaris.api.plugin.stat.DefaultRateLimitResult)

Aggregations

DefaultRateLimitResult (com.tencent.polaris.api.plugin.stat.DefaultRateLimitResult)3 StatInfo (com.tencent.polaris.api.plugin.stat.StatInfo)2 PolarisException (com.tencent.polaris.api.exception.PolarisException)1 Plugin (com.tencent.polaris.api.plugin.Plugin)1 StatReporter (com.tencent.polaris.api.plugin.stat.StatReporter)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1