use of com.tencent.polaris.ratelimit.api.rpc.QuotaResponse in project polaris-java by polarismesh.
the class QuotaFlow method getQuota.
public QuotaResponse getQuota(CommonQuotaRequest request) throws PolarisException {
RateLimitWindow rateLimitWindow = lookupRateLimitWindow(request);
if (null == rateLimitWindow) {
// 没有限流规则,直接放通
return new QuotaResponse(new QuotaResult(QuotaResult.Code.QuotaResultOk, 0, RateLimitConstants.RULE_NOT_EXISTS));
}
rateLimitWindow.init();
return new QuotaResponse(rateLimitWindow.allocateQuota(request.getCount()));
}
use of com.tencent.polaris.ratelimit.api.rpc.QuotaResponse in project polaris-java by polarismesh.
the class DefaultLimitAPI method getQuota.
@Override
public QuotaResponse getQuota(QuotaRequest request) throws PolarisException {
checkAvailable("LimitAPI");
LimitValidator.validateQuotaRequest(request);
CommonQuotaRequest commonQuotaRequest = new CommonQuotaRequest(request, sdkContext.getConfig());
QuotaResponse response = quotaFlow.getQuota(commonQuotaRequest);
reportRateLimit(request, response);
return response;
}
use of com.tencent.polaris.ratelimit.api.rpc.QuotaResponse 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.QuotaResponse 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.QuotaResponse in project spring-cloud-tencent by Tencent.
the class RateLimitScgFilter method doFilter.
@Override
public Mono<Void> doFilter(ServerWebExchange exchange, GatewayFilterChain chain) {
// get metadata of current thread
MetadataContext metadataContext = exchange.getAttribute(MetadataConstant.HeaderName.METADATA_CONTEXT);
String peerNamespace = metadataContext.getSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_NAMESPACE);
String peerService = metadataContext.getSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_SERVICE);
String peerPath = metadataContext.getSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_PATH);
Map<String, String> labels = null;
if (StringUtils.isNotBlank(peerPath)) {
labels = new HashMap<>();
labels.put("method", peerPath);
}
try {
QuotaResponse quotaResponse = QuotaCheckUtils.getQuota(limitAPI, peerNamespace, peerService, 1, labels, null);
if (quotaResponse.getCode() == QuotaResultCode.QuotaResultLimited) {
ServerHttpResponse response = exchange.getResponse();
response.setStatusCode(HttpStatus.TOO_MANY_REQUESTS);
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
DataBuffer dataBuffer = response.bufferFactory().allocateBuffer().write((Consts.QUOTA_LIMITED_INFO + quotaResponse.getInfo()).getBytes(StandardCharsets.UTF_8));
return response.writeWith(Mono.just(dataBuffer));
}
} catch (Throwable throwable) {
// 限流API调用出现异常,不应该影响业务流程的调用
LOG.error("fail to rate limit with QuotaRequest[{}-{}-{}].", peerNamespace, peerService, peerPath, throwable);
}
return chain.filter(exchange);
}
Aggregations