Skip to content

Commit

Permalink
Add /apps/<app_id> to the rate limiter filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomasz Bak committed Nov 6, 2014
1 parent 5b959b4 commit c22b69a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.netflix.appinfo.AbstractEurekaIdentity;
import com.netflix.appinfo.EurekaClientIdentity;
Expand Down Expand Up @@ -83,7 +85,9 @@ public class RateLimitingFilter implements Filter {
Arrays.asList(EurekaClientIdentity.DEFAULT_CLIENT_NAME, EurekaServerIdentity.DEFAULT_SERVER_NAME)
);

enum Target {FullFetch, DeltaFetch, Other}
private static final Pattern TARGET_RE = Pattern.compile("^.*/apps(/[^/]*)?$");

enum Target {FullFetch, DeltaFetch, Application, Other}

/**
* Includes both full and delta fetches.
Expand Down Expand Up @@ -125,11 +129,15 @@ private static Target getTarget(ServletRequest request) {
HttpServletRequest httpRequest = (HttpServletRequest) request;

if ("GET".equals(httpRequest.getMethod())) {
String path = httpRequest.getPathInfo();
if (path.endsWith("/apps") || path.endsWith("/apps/")) {
target = Target.FullFetch;
} else if (path.endsWith("/apps/delta") || path.endsWith("/apps/delta/")) {
target = Target.DeltaFetch;
Matcher matcher = TARGET_RE.matcher(httpRequest.getPathInfo());
if (matcher.matches()) {
if (matcher.groupCount() == 0 || "/".equals(matcher.group(1))) {
target = Target.FullFetch;
} else if ("/delta".equals(matcher.group(1))) {
target = Target.DeltaFetch;
} else {
target = Target.Application;
}
}
}
}
Expand All @@ -154,7 +162,7 @@ private static boolean isOverloaded(Target target) {
int fetchWindowSize = config().getRateLimiterRegistryFetchAverageRate();
boolean overloaded = !registryFetchRateLimiter.acquire(maxInWindow, fetchWindowSize);

if (target == Target.FullFetch) {
if (target == Target.FullFetch || target == Target.Application) {
int fullFetchWindowSize = config().getRateLimiterFullFetchAverageRate();
overloaded |= !registryFullFetchRateLimiter.acquire(maxInWindow, fullFetchWindowSize);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class RateLimitingFilterTest {

private static final String FULL_FETCH = "base/apps";
private static final String DELTA_FETCH = "base/apps/delta";
private static final String APP_FETCH = "base/apps/myAppId";

private static final String CUSTOM_CLIENT = "CustomClient";
private static final String PYTHON_CLIENT = "PythonClient";
Expand Down Expand Up @@ -83,15 +84,15 @@ public void testPrivilegedClientAlwaysServed() throws Exception {
whenRequest(DELTA_FETCH, EurekaClientIdentity.DEFAULT_CLIENT_NAME);
filter.doFilter(request, response, filterChain);

whenRequest(FULL_FETCH, EurekaServerIdentity.DEFAULT_SERVER_NAME);
whenRequest(APP_FETCH, EurekaServerIdentity.DEFAULT_SERVER_NAME);
filter.doFilter(request, response, filterChain);

verify(filterChain, times(3)).doFilter(request, response);
verify(response, never()).setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
}

@Test
public void testStandardClientsThrottled() throws Exception {
public void testStandardClientsThrottlingEnforceable() throws Exception {
ConfigurationManager.getConfigInstance().setProperty("eureka.rateLimiter.throttleStandardClients", true);

// Custom clients will go up to the window limit
Expand Down

0 comments on commit c22b69a

Please sign in to comment.