Skip to content

Commit

Permalink
added quota rate limit
Browse files Browse the repository at this point in the history
  • Loading branch information
lchayoun committed Sep 15, 2017
1 parent abf3054 commit 552a345
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@
import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.properties.RateLimitProperties.Policy;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
import org.springframework.cloud.netflix.zuul.util.ZuulRuntimeException;
import org.springframework.http.HttpStatus;
import org.springframework.web.util.UrlPathHelper;

/**
Expand Down Expand Up @@ -63,13 +60,4 @@ protected Optional<Policy> policy(final Route route) {
}
return Optional.ofNullable(properties.getDefaultPolicy());
}

protected void rejectExceededRequest(RequestContext ctx) {
HttpStatus tooManyRequests = HttpStatus.TOO_MANY_REQUESTS;
ctx.setResponseStatusCode(tooManyRequests.value());
ctx.put("rateLimitExceeded", "true");
ctx.setSendZuulResponse(false);
ZuulException zuulException = new ZuulException(tooManyRequests.toString(), tooManyRequests.value(), null);
throw new ZuulRuntimeException(zuulException);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.RateLimiter;
import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.properties.RateLimitProperties;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.cloud.netflix.zuul.util.ZuulRuntimeException;
import org.springframework.http.HttpStatus;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.util.UrlPathHelper;

Expand Down Expand Up @@ -87,7 +90,12 @@ public Object run() {
response.setHeader(RESET_HEADER, String.valueOf(rate.getReset()));

if ((limit != null && remaining < 0) || (quota != null && remainingQuota < 0)) {
rejectExceededRequest(ctx);
HttpStatus tooManyRequests = HttpStatus.TOO_MANY_REQUESTS;
ctx.setResponseStatusCode(tooManyRequests.value());
ctx.put("rateLimitExceeded", "true");
ctx.setSendZuulResponse(false);
ZuulException zuulException = new ZuulException(tooManyRequests.toString(), tooManyRequests.value(), null);
throw new ZuulRuntimeException(zuulException);
}
});

Expand Down

0 comments on commit 552a345

Please sign in to comment.