use of com.tencent.polaris.ratelimit.api.rpc.QuotaRequest 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();
}
use of com.tencent.polaris.ratelimit.api.rpc.QuotaRequest in project polaris-java-agent by polarismesh.
the class PolarisOperator method getQuota.
/**
* 调用LIMIT_API进行服务限流
*
* @param count 本次请求的配额
* @return 是否通过,为false则需要对本次请求限流
*/
public boolean getQuota(String service, String method, Map<String, String> labels, int count) {
init();
if (!inited.get()) {
LOGGER.error("[POLARIS] fail to get quota, service:{}, method:{}, polaris init failed", service, method);
throw new RuntimeException("polaris init failed");
}
QuotaRequest quotaRequest = new QuotaRequest();
quotaRequest.setNamespace(polarisConfig.getNamespace());
quotaRequest.setService(service);
quotaRequest.setMethod(method);
quotaRequest.setLabels(labels);
quotaRequest.setCount(count);
QuotaResponse quota = limitAPI.getQuota(quotaRequest);
return quota.getCode() == QuotaResultCode.QuotaResultOk;
}
use of com.tencent.polaris.ratelimit.api.rpc.QuotaRequest 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.");
}
});
}
use of com.tencent.polaris.ratelimit.api.rpc.QuotaRequest 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"));
}
}
use of com.tencent.polaris.ratelimit.api.rpc.QuotaRequest 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);
}
Aggregations