use of com.netflix.zuul.context.RequestContext 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.netflix.zuul.context.RequestContext 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.netflix.zuul.context.RequestContext in project bucket4j-spring-boot-starter by MarcGiffing.
the class ZuulRateLimitFilter method shouldFilter.
@Override
public boolean shouldFilter() {
RequestContext currentContext = RequestContext.getCurrentContext();
HttpServletRequest request = currentContext.getRequest();
String requestURI = request.getRequestURI();
return requestURI.matches(filterConfig.getUrl());
}
use of com.netflix.zuul.context.RequestContext in project bucket4j-spring-boot-starter by MarcGiffing.
the class ServletRateLimitFilterTest method should_execute_only_one_check_when_using_RateLimitConditionMatchingStrategy_FIRST.
@Test
public void should_execute_only_one_check_when_using_RateLimitConditionMatchingStrategy_FIRST() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/url");
RequestContext context = new RequestContext();
context.setRequest(request);
RequestContext.testSetCurrentContext(context);
configuration.setStrategy(RateLimitConditionMatchingStrategy.FIRST);
when(rateLimitCheck1.rateLimit(any())).thenReturn(consumptionProbe);
standaloneSetup(new TestController()).addFilters(filter).build().perform(get(("/test")));
verify(rateLimitCheck1, times(1)).rateLimit(any());
verify(rateLimitCheck2, times(0)).rateLimit(any());
verify(rateLimitCheck3, times(0)).rateLimit(any());
}
use of com.netflix.zuul.context.RequestContext in project micro-service by Lovnx.
the class FirstFilter method run.
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
// HttpServletResponse response = ctx.getResponse();
log.info("第一级过滤器!");
log.info("===============");
// log.info("access token ok");
return null;
}
Aggregations