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