Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazily create expensive Optional default values. Fixes #294. #295

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public Optional<RemoteHost> choose(LoadBalancer.Preferences context) {
}
}
)
.orElse(delegate.choose(context));
.orElseGet(() -> delegate.choose(context));
}

private Predicate<RemoteHost> originIsAllowed(String cookieValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public Optional<String> preferredOrigins() {
return rewrittenRequest.cookie(originsRestrictionCookieName)
.map(RequestCookie::value)
.map(Optional::of)
.orElse(rewrittenRequest.cookie("styx_origin_" + id).map(RequestCookie::value));
.orElseGet(() -> rewrittenRequest.cookie("styx_origin_" + id).map(RequestCookie::value));
} else {
return rewrittenRequest.cookie("styx_origin_" + id).map(RequestCookie::value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Optional<RemoteHost> choose(LoadBalancer.Preferences context) {
return context.preferredOrigins()
.flatMap(preferredHost -> originById(activeOrigins.snapshot(), preferredHost))
.map(Optional::of)
.orElse(delegate.choose(context));
.orElseGet(() -> delegate.choose(context));
}

private Optional<RemoteHost> originById(Iterable<RemoteHost> origins, String id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,23 @@ public void usesSingleOriginMatchingRegularExpression() {
assertThat(chosenOne.get(), is(origins.get(1)));
}

@Test
public void dontDelegateWhenOriginRestrictionCookieMatches() {
Random mockRandom = mock(Random.class);
when(mockRandom.nextInt(eq(1))).thenReturn(0);

strategy = new OriginRestrictionLoadBalancingStrategy(() -> origins, delegate, mockRandom);

strategy.choose(lbPreference(Optional.of("origin-1")));
strategy.choose(lbPreference(Optional.of("origin-1")));
strategy.choose(lbPreference(Optional.of("origin-1")));
strategy.choose(lbPreference(Optional.of("origin-1")));
strategy.choose(lbPreference(Optional.of("origin-1")));
strategy.choose(lbPreference(Optional.of("origin-1")));

verify(delegate, never()).choose(any(LoadBalancer.Preferences.class));
}

@Test
public void usesMultipleOriginsMatchingRegularExpression() {
Random mockRandom = mock(Random.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
import static java.util.Collections.EMPTY_LIST;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;


Expand Down Expand Up @@ -63,6 +66,18 @@ public void selectsThePreferredOrigin() {
assertThat(chosenOne, is(Optional.of(ORIGIN_1)));
}

@Test
public void dontDelegateWhenPreferredOriginIsFound() {
LoadBalancer delegate = mock(LoadBalancer.class);
StickySessionLoadBalancingStrategy aStrategy = new StickySessionLoadBalancingStrategy(activeOrigins, delegate);

when(activeOrigins.snapshot()).thenReturn(asList(ORIGIN_0, ORIGIN_1, ORIGIN_2));

aStrategy.choose(lbPreferences(Optional.of(ORIGIN_1.id())));

verify(delegate, never()).choose(any(LoadBalancer.Preferences.class));
}

@Test
public void failsOverToNextAvailableOriginSelectedByTheLoadBalancingStrategyIfRequestedOriginIsNotRegistered() {
when(activeOrigins.snapshot()).thenReturn(asList(ORIGIN_0, ORIGIN_2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static LoadBalancingStrategyFactoryProvider newProvider(Configuration con
public LoadBalancerFactory get() {
return configurations.get(LOAD_BALANCING_STRATEGY_KEY)
.map(this::newFactoryInstance)
.orElse(busyConnectionBalancer());
.orElseGet(this::busyConnectionBalancer);
}

private LoadBalancerFactory busyConnectionBalancer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public Registry<BackendService> create(Environment environment, Configuration re
"missing [services.registry.factory.config.originsFile] config value for factory class FileBackedBackendServicesRegistry.Factory"));

FileMonitorSettings monitorSettings = registryConfiguration.get("monitor", FileMonitorSettings.class)
.orElse(new FileMonitorSettings());
.orElseGet(FileMonitorSettings::new);

return registry(originsFile, monitorSettings);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public HttpHeader header(String key) {
public ContentTypeHeader contentTypeHeader() {
return styxRequest.header(CONTENT_TYPE)
.map(ContentTypeHeader::new)
.orElse(ContentTypeHeader.absent());
.orElseGet(ContentTypeHeader::absent);
}

@Override
Expand All @@ -102,7 +102,7 @@ public Set<String> getAllHeaderKeys() {
public QueryParameter queryParameter(String key) {
return styxRequest.queryParam(key)
.map(value -> new QueryParameter(key, ImmutableList.of(value)))
.orElse(QueryParameter.absent(key));
.orElseGet(() -> QueryParameter.absent(key));
}

@Override
Expand Down