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