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);
}
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());
}
}
}
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;
}
Aggregations