use of com.giffing.bucket4j.spring.boot.starter.context.RateLimitCheck in project bucket4j-spring-boot-starter by MarcGiffing.
the class ServletRequestFilter method doFilterInternal.
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
boolean allConsumed = true;
Long remainingLimit = null;
for (RateLimitCheck rl : filterConfig.getRateLimitChecks()) {
ConsumptionProbe probe = rl.rateLimit(request);
if (probe != null) {
if (probe.isConsumed()) {
remainingLimit = getRemainingLimit(remainingLimit, probe);
} else {
allConsumed = false;
handleHttpResponseOnRateLimiting(httpResponse, probe);
break;
}
if (filterConfig.getStrategy().equals(RateLimitConditionMatchingStrategy.FIRST)) {
break;
}
}
}
;
if (allConsumed) {
if (remainingLimit != null) {
httpResponse.setHeader("X-Rate-Limit-Remaining", "" + remainingLimit);
}
filterChain.doFilter(httpRequest, httpResponse);
}
}
use of com.giffing.bucket4j.spring.boot.starter.context.RateLimitCheck in project bucket4j-spring-boot-starter by MarcGiffing.
the class ServletRateLimitFilter method run.
@Override
public Object run() {
RequestContext context = RequestContext.getCurrentContext();
HttpServletRequest request = context.getRequest();
Long remainingLimit = null;
for (RateLimitCheck rl : filterConfig.getRateLimitChecks()) {
ConsumptionProbe probe = rl.rateLimit(request);
if (probe != null) {
if (probe.isConsumed()) {
remainingLimit = getRemainingLimit(remainingLimit, probe);
} else {
context.setResponseStatusCode(HttpStatus.TOO_MANY_REQUESTS.value());
context.setResponseBody(filterConfig.getHttpResponseBody());
context.setSendZuulResponse(false);
}
if (filterConfig.getStrategy().equals(RateLimitConditionMatchingStrategy.FIRST)) {
break;
}
}
}
;
return null;
}
use of com.giffing.bucket4j.spring.boot.starter.context.RateLimitCheck in project bucket4j-spring-boot-starter by MarcGiffing.
the class ZuulRateLimitFilter method run.
@Override
public Object run() {
RequestContext context = getCurrentRequestContext();
HttpServletRequest request = context.getRequest();
Long remainingLimit = null;
for (RateLimitCheck rl : filterConfig.getRateLimitChecks()) {
ConsumptionProbe probe = rl.rateLimit(request);
if (probe != null) {
if (probe.isConsumed()) {
remainingLimit = getRemainingLimit(remainingLimit, probe);
} else {
context.setResponseStatusCode(HttpStatus.TOO_MANY_REQUESTS.value());
context.setResponseBody(filterConfig.getHttpResponseBody());
context.setSendZuulResponse(false);
break;
}
if (filterConfig.getStrategy().equals(RateLimitConditionMatchingStrategy.FIRST)) {
break;
}
}
}
;
return null;
}
use of com.giffing.bucket4j.spring.boot.starter.context.RateLimitCheck in project bucket4j-spring-boot-starter by MarcGiffing.
the class Bucket4JBaseConfiguration method buildFilterConfig.
public FilterConfiguration buildFilterConfig(Bucket4JConfiguration config, CacheManager cacheManager, ExpressionParser expressionParser, BeanFactory beanFactory) {
FilterConfiguration filterConfig = new FilterConfiguration();
filterConfig.setUrl(config.getUrl());
filterConfig.setOrder(config.getFilterOrder());
filterConfig.setStrategy(config.getStrategy());
filterConfig.setHttpResponseBody(config.getHttpResponseBody());
ProxyManager<String> buckets = Bucket4j.extension(JCache.class).proxyManagerForCache(jCache(config.getCacheName(), cacheManager));
config.getRateLimits().forEach(rl -> {
ConfigurationBuilder<?> configBuilder = Bucket4j.configurationBuilder();
for (BandWidthConfig bandWidth : rl.getBandwidths()) {
configBuilder = configBuilder.addLimit(Bandwidth.simple(bandWidth.getCapacity(), Duration.of(bandWidth.getTime(), bandWidth.getUnit())));
}
;
final ConfigurationBuilder<?> configBuilderToUse = configBuilder;
RateLimitCheck rlc = (servletRequest) -> {
boolean skipRateLimit = false;
if (rl.getSkipCondition() != null) {
skipRateLimit = skipCondition(rl, expressionParser, beanFactory).evalute(servletRequest);
}
if (rl.getExecuteCondition() != null && !skipRateLimit) {
skipRateLimit = !executeCondition(rl, expressionParser, beanFactory).evalute(servletRequest);
}
if (!skipRateLimit) {
String key = getKeyFilter(filterConfig.getUrl(), rl, expressionParser, beanFactory).key(servletRequest);
Bucket bucket = buckets.getProxy(key, () -> configBuilderToUse.buildConfiguration());
ConsumptionProbe probe = bucket.tryConsumeAndReturnRemaining(1);
return probe;
}
return null;
};
filterConfig.getRateLimitChecks().add(rlc);
});
return filterConfig;
}
Aggregations