Skip to content

Commit

Permalink
Allow custom overrides with global filter concurrency limits
Browse files Browse the repository at this point in the history
  • Loading branch information
argha-c committed Mar 18, 2024
1 parent 99a9869 commit a821f6f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,19 @@ public HttpContent processContentChunk(ZuulMessage zuulMessage, HttpContent chun

@Override
public void incrementConcurrency() throws ZuulFilterConcurrencyExceededException {
final int limit = Math.max(filterConcurrencyCustom.get(), filterConcurrencyDefault.get());
final int limit = calculateConcurency();
if ((concurrencyProtectionEnabled.get()) && (concurrentCount.get() >= limit)) {
concurrencyRejections.increment();
throw new ZuulFilterConcurrencyExceededException(this, limit);
}
concurrentCount.incrementAndGet();
}

protected int calculateConcurency() {
final int customLimit = filterConcurrencyCustom.get();
return customLimit != DEFAULT_FILTER_CONCURRENCY_LIMIT ? customLimit : filterConcurrencyDefault.get();
}

@Override
public void decrementConcurrency() {
concurrentCount.decrementAndGet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,27 @@
import com.netflix.zuul.message.ZuulMessage;
import com.netflix.zuul.message.ZuulMessageImpl;
import org.apache.commons.configuration.AbstractConfiguration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import rx.Observable;

/**
* Tests for {@link BaseFilter}. Currently named BaseFilter2Test as there is an existing class named BaseFilterTest.
* Tests for {@link BaseFilter}
*/
@RunWith(MockitoJUnitRunner.class)
class BaseFilterTest {

@Mock
private BaseFilter f1;

@Mock
private BaseFilter f2;

@Mock
private ZuulMessage req;
private final AbstractConfiguration config = ConfigurationManager.getConfigInstance();

@BeforeEach
public void tearDown() {
config.clear();
}

@Test
void testShouldFilter() {
Expand Down Expand Up @@ -84,7 +85,7 @@ class ConcInboundFilter extends BaseFilter {

@Override
public Observable applyAsync(ZuulMessage input) {
limit[0] = Math.max(filterConcurrencyCustom.get(), filterConcurrencyDefault.get());
limit[0] = calculateConcurency();
return Observable.just("Done");
}

Expand All @@ -103,17 +104,16 @@ public boolean shouldFilter(ZuulMessage msg) {
}

@Test
void validateFilterConcurrencyLimitOverride() {
AbstractConfiguration configuration = ConfigurationManager.getConfigInstance();
configuration.setProperty("zuul.filter.concurrency.limit.default", 7000);
configuration.setProperty("zuul.ConcInboundFilter.in.concurrency.limit", 4000);
void validateFilterGlobalConcurrencyLimitOverride() {
config.setProperty("zuul.filter.concurrency.limit.default", 7000);
config.setProperty("zuul.ConcInboundFilter.in.concurrency.limit", 4000);
final int[] limit = {0};

class ConcInboundFilter extends BaseFilter {

@Override
public Observable applyAsync(ZuulMessage input) {
limit[0] = Math.max(filterConcurrencyCustom.get(), filterConcurrencyDefault.get());
limit[0] = calculateConcurency();
return Observable.just("Done");
}

Expand All @@ -130,4 +130,32 @@ public boolean shouldFilter(ZuulMessage msg) {
new ConcInboundFilter().applyAsync(new ZuulMessageImpl(new SessionContext(), new Headers()));
Truth.assertThat(limit[0]).isEqualTo(7000);
}

@Test
void validateFilterSpecificConcurrencyLimitOverride() {
config.setProperty("zuul.filter.concurrency.limit.default", 7000);
config.setProperty("zuul.ConcInboundFilter.in.concurrency.limit", 4300);
final int[] limit = {0};

class ConcInboundFilter extends BaseFilter {

@Override
public Observable applyAsync(ZuulMessage input) {
limit[0] = calculateConcurency();
return Observable.just("Done");
}

@Override
public FilterType filterType() {
return FilterType.INBOUND;
}

@Override
public boolean shouldFilter(ZuulMessage msg) {
return true;
}
}
new ConcInboundFilter().applyAsync(new ZuulMessageImpl(new SessionContext(), new Headers()));
Truth.assertThat(limit[0]).isEqualTo(4300);
}
}

0 comments on commit a821f6f

Please sign in to comment.