|
|
|
@ -58,11 +58,13 @@ import static com.tencent.cloud.polaris.ratelimit.constant.RateLimitConstant.LAB
|
|
|
|
|
@Order(RateLimitConstant.FILTER_ORDER)
|
|
|
|
|
public class QuotaCheckServletFilter extends OncePerRequestFilter {
|
|
|
|
|
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(QuotaCheckServletFilter.class);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Default Filter Registration Bean Name Defined.
|
|
|
|
|
* Default Filter Registration Bean Name Defined .
|
|
|
|
|
*/
|
|
|
|
|
public static final String QUOTA_FILTER_BEAN_NAME = "quotaFilterRegistrationBean";
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(QuotaCheckServletFilter.class);
|
|
|
|
|
|
|
|
|
|
private final LimitAPI limitAPI;
|
|
|
|
|
|
|
|
|
|
private final PolarisRateLimiterLabelServletResolver labelResolver;
|
|
|
|
@ -97,8 +99,8 @@ public class QuotaCheckServletFilter extends OncePerRequestFilter {
|
|
|
|
|
Map<String, String> labels = getRequestLabels(request, localNamespace, localService);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
QuotaResponse quotaResponse = QuotaCheckUtils.getQuota(
|
|
|
|
|
limitAPI, localNamespace, localService, 1, labels, request.getRequestURI());
|
|
|
|
|
QuotaResponse quotaResponse = QuotaCheckUtils.getQuota(limitAPI,
|
|
|
|
|
localNamespace, localService, 1, labels, request.getRequestURI());
|
|
|
|
|
|
|
|
|
|
if (quotaResponse.getCode() == QuotaResultCode.QuotaResultLimited) {
|
|
|
|
|
response.setStatus(polarisRateLimitProperties.getRejectHttpCode());
|
|
|
|
@ -108,6 +110,7 @@ public class QuotaCheckServletFilter extends OncePerRequestFilter {
|
|
|
|
|
}
|
|
|
|
|
// Unirate
|
|
|
|
|
if (quotaResponse.getCode() == QuotaResultCode.QuotaResultOk && quotaResponse.getWaitMs() > 0) {
|
|
|
|
|
LOG.debug("The request of [{}] will waiting for {}ms.", request.getRequestURI(), quotaResponse.getWaitMs());
|
|
|
|
|
Thread.sleep(quotaResponse.getWaitMs());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -147,8 +150,7 @@ public class QuotaCheckServletFilter extends OncePerRequestFilter {
|
|
|
|
|
return labelResolver.resolve(request);
|
|
|
|
|
}
|
|
|
|
|
catch (Throwable e) {
|
|
|
|
|
LOG.error("resolve custom label failed. resolver = {}",
|
|
|
|
|
labelResolver.getClass().getName(), e);
|
|
|
|
|
LOG.error("resolve custom label failed. resolver = {}", labelResolver.getClass().getName(), e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Collections.emptyMap();
|
|
|
|
|