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

#235 - Removed unused method #241

Merged
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 @@ -29,8 +29,8 @@
import com.hotels.styx.api.HttpResponseStatus;
import com.hotels.styx.api.MetricRegistry;
import com.hotels.styx.api.metrics.codahale.CodaHaleMetricRegistry;
import com.hotels.styx.api.extension.service.BackendService;
import com.hotels.styx.api.extension.service.RewriteRule;
import com.hotels.styx.api.extension.service.StickySessionConfig;
import com.hotels.styx.client.retry.RetryNTimes;
import com.hotels.styx.client.stickysession.StickySessionLoadBalancingStrategy;
import com.hotels.styx.server.HttpInterceptorContext;
Expand All @@ -49,6 +49,7 @@
import static com.hotels.styx.api.HttpHeaderNames.CONTENT_LENGTH;
import static com.hotels.styx.api.HttpHeaderNames.TRANSFER_ENCODING;
import static com.hotels.styx.api.StyxInternalObservables.toRxObservable;
import static com.hotels.styx.api.extension.service.StickySessionConfig.stickySessionDisabled;
import static com.hotels.styx.client.stickysession.StickySessionCookie.newStickySessionCookie;
import static io.netty.handler.codec.http.HttpMethod.HEAD;
import static java.util.Collections.emptyList;
Expand All @@ -70,14 +71,15 @@ public final class StyxHttpClient implements HttpClient {
private final LoadBalancer loadBalancer;
private final RetryPolicy retryPolicy;
private final OriginStatsFactory originStatsFactory;
private final BackendService backendService;
private final MetricRegistry metricsRegistry;
private final boolean contentValidation;
private final String originsRestrictionCookieName;
private final StickySessionConfig stickySessionConfig;

private StyxHttpClient(Builder builder) {
this.backendService = requireNonNull(builder.backendService);
this.id = backendService.id();
this.id = requireNonNull(builder.backendServiceId);

this.stickySessionConfig = requireNonNull(builder.stickySessionConfig);

this.originStatsFactory = requireNonNull(builder.originStatsFactory);

Expand All @@ -99,17 +101,13 @@ public Observable<HttpResponse> sendRequest(HttpRequest request) {
return sendRequest(rewriteUrl(request), new ArrayList<>(), 0);
}

public boolean isHttps() {
return backendService.tlsSettings().isPresent();
}

/**
* Create a new builder.
*
* @return a new builder
*/
public static Builder newHttpClientBuilder(BackendService backendService) {
return new Builder(backendService);
public static Builder newHttpClientBuilder(Id backendServiceId) {
return new Builder(backendServiceId);
}

private static boolean isError(HttpResponseStatus status) {
Expand Down Expand Up @@ -191,7 +189,6 @@ public List<Origin> avoidOrigins() {
} else {
return Observable.error(cause);
}

}

private static final class RetryPolicyContext implements RetryPolicy.Context {
Expand Down Expand Up @@ -307,7 +304,7 @@ public List<Origin> avoidOrigins() {

private HttpResponse addStickySessionIdentifier(HttpResponse httpResponse, Origin origin) {
if (this.loadBalancer instanceof StickySessionLoadBalancingStrategy) {
int maxAge = backendService.stickySessionConfig().stickySessionTimeoutSeconds();
int maxAge = stickySessionConfig.stickySessionTimeoutSeconds();
return httpResponse.newBuilder()
.addCookies(newStickySessionCookie(id, origin.id(), maxAge))
.build();
Expand All @@ -324,7 +321,7 @@ private HttpRequest rewriteUrl(HttpRequest request) {
public String toString() {
return toStringHelper(this)
.add("id", id)
.add("stickySessionConfig", backendService.stickySessionConfig())
.add("stickySessionConfig", stickySessionConfig)
.add("retryPolicy", retryPolicy)
.add("rewriteRuleset", rewriteRuleset)
.add("loadBalancingStrategy", loadBalancer)
Expand All @@ -335,17 +332,24 @@ public String toString() {
* A builder for {@link com.hotels.styx.client.StyxHttpClient}.
*/
public static class Builder {
private final BackendService backendService;

private final Id backendServiceId;
private MetricRegistry metricsRegistry = new CodaHaleMetricRegistry();
private List<RewriteRule> rewriteRules = emptyList();
private RetryPolicy retryPolicy = new RetryNTimes(3);
private LoadBalancer loadBalancer;
private boolean contentValidation;
private OriginStatsFactory originStatsFactory;
private String originsRestrictionCookieName;
private StickySessionConfig stickySessionConfig = stickySessionDisabled();

public Builder(BackendService backendService) {
this.backendService = requireNonNull(backendService);
public Builder(Id backendServiceId) {
this.backendServiceId = requireNonNull(backendServiceId);
}

public Builder stickySessionConfig(StickySessionConfig stickySessionConfig) {
this.stickySessionConfig = requireNonNull(stickySessionConfig);
return this;
}

public Builder metricsRegistry(MetricRegistry metricsRegistry) {
Expand Down Expand Up @@ -390,5 +394,6 @@ public StyxHttpClient build() {
}
return new StyxHttpClient(this);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class HttpClientSpec extends FunSuite with BeforeAndAfterAll with ShouldMatchers
.responseTimeoutMillis(responseTimeout)
.build()

client = newHttpClientBuilder(backendService)
client = newHttpClientBuilder(backendService.id())
.loadBalancer(busyConnectionStrategy(activeOrigins(backendService)))
.build
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class RetryHandlingSpec extends FunSuite with BeforeAndAfterAll with Matchers wi
.origins(unhealthyOriginOne, unhealthyOriginTwo, unhealthyOriginThree, healthyOriginTwo)
.build()

val client: StyxHttpClient = newHttpClientBuilder(backendService)
val client: StyxHttpClient = newHttpClientBuilder(backendService.id)
.retryPolicy(new RetryNTimes(3))
.loadBalancer(stickySessionStrategy(activeOrigins(backendService)))
.build
Expand All @@ -139,7 +139,7 @@ class RetryHandlingSpec extends FunSuite with BeforeAndAfterAll with Matchers wi
val backendService = new BackendService.Builder()
.origins(unhealthyOriginOne, unhealthyOriginTwo, unhealthyOriginThree)
.build()
val client: StyxHttpClient = newHttpClientBuilder(backendService)
val client: StyxHttpClient = newHttpClientBuilder(backendService.id)
.loadBalancer(stickySessionStrategy(activeOrigins(backendService)))
.retryPolicy(new RetryNTimes(2))
.build
Expand All @@ -155,7 +155,7 @@ class RetryHandlingSpec extends FunSuite with BeforeAndAfterAll with Matchers wi
.stickySessionConfig(StickySessionEnabled)
.build()

val client: StyxHttpClient = newHttpClientBuilder(backendService)
val client: StyxHttpClient = newHttpClientBuilder(backendService.id)
.retryPolicy(new RetryNTimes(3))
.loadBalancer(stickySessionStrategy(activeOrigins(backendService)))
.build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.hotels.styx.client

import java.nio.charset.Charset
import java.util.concurrent.TimeUnit

import com.github.tomakehurst.wiremock.client.WireMock.aResponse
import com.hotels.styx.api.HttpHeaderNames.CONTENT_LENGTH
Expand Down Expand Up @@ -97,9 +98,13 @@ class StickySessionSpec extends FunSuite with BeforeAndAfter with ShouldMatchers

def stickySessionStrategy(activeOrigins: ActiveOrigins) = new StickySessionLoadBalancingStrategy(activeOrigins, roundRobinStrategy(activeOrigins))


test("Responds with sticky session cookie when STICKY_SESSION_ENABLED=true") {
val client = newHttpClientBuilder(backendService)
val stickySessionConfig = StickySessionConfig.newStickySessionConfigBuilder().timeout(100, TimeUnit.SECONDS).build()

val client = newHttpClientBuilder(backendService.id)
.loadBalancer(stickySessionStrategy(activeOrigins(backendService)))
.stickySessionConfig(stickySessionConfig)
.build

val request: HttpRequest = HttpRequest.get("/")
Expand All @@ -113,10 +118,12 @@ class StickySessionSpec extends FunSuite with BeforeAndAfter with ShouldMatchers
cookie.path().get() should be("/")
cookie.httpOnly() should be(true)
cookie.maxAge().isPresent should be(true)

cookie.maxAge().get() should be (100L)
}

test("Responds without sticky session cookie when sticky session is not enabled") {
val client: StyxHttpClient = newHttpClientBuilder(backendService)
val client: StyxHttpClient = newHttpClientBuilder(backendService.id)
.loadBalancer(roundRobinStrategy(activeOrigins(backendService)))
.build

Expand All @@ -129,7 +136,7 @@ class StickySessionSpec extends FunSuite with BeforeAndAfter with ShouldMatchers
}

test("Routes to origins indicated by sticky session cookie.") {
val client: StyxHttpClient = newHttpClientBuilder(backendService)
val client: StyxHttpClient = newHttpClientBuilder(backendService.id)
.loadBalancer(stickySessionStrategy(activeOrigins(backendService)))
.build

Expand All @@ -147,7 +154,7 @@ class StickySessionSpec extends FunSuite with BeforeAndAfter with ShouldMatchers
}

test("Routes to origins indicated by sticky session cookie when other cookies are provided.") {
val client: StyxHttpClient = newHttpClientBuilder(backendService)
val client: StyxHttpClient = newHttpClientBuilder(backendService.id)
.loadBalancer(stickySessionStrategy(activeOrigins(backendService)))
.build

Expand All @@ -169,7 +176,7 @@ class StickySessionSpec extends FunSuite with BeforeAndAfter with ShouldMatchers
}

test("Routes to new origin when the origin indicated by sticky session cookie does not exist.") {
val client: StyxHttpClient = newHttpClientBuilder(backendService)
val client: StyxHttpClient = newHttpClientBuilder(backendService.id)
.loadBalancer(stickySessionStrategy(activeOrigins(backendService)))
.build

Expand All @@ -194,7 +201,7 @@ class StickySessionSpec extends FunSuite with BeforeAndAfter with ShouldMatchers
test("Routes to new origin when the origin indicated by sticky session cookie is no longer available.") {
server1.stop()

val client: StyxHttpClient = newHttpClientBuilder(backendService)
val client: StyxHttpClient = newHttpClientBuilder(backendService.id)
.loadBalancer(stickySessionStrategy(activeOrigins(backendService)))
.build

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void setUp() {
public void sendsRequestToHostChosenByLoadBalancer() {
StyxHostHttpClient hostClient = mockHostClient(just(response(OK).build()));

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService)
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.metricsRegistry(metricRegistry)
.loadBalancer(
mockLoadBalancer(
Expand All @@ -120,7 +120,7 @@ public void constructsRetryContextWhenLoadBalancerDoesNotFindAvailableOrigins()
RetryPolicy retryPolicy = mockRetryPolicy(true, true, true);
StyxHostHttpClient hostClient = mockHostClient(just(response(OK).build()));

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService)
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.metricsRegistry(metricRegistry)
.loadBalancer(
mockLoadBalancer(
Expand Down Expand Up @@ -158,7 +158,7 @@ public void retriesWhenRetryPolicyTellsToRetry() {

StyxHostHttpClient secondClient = mockHostClient(just(response(OK).build()));

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService)
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.metricsRegistry(metricRegistry)
.loadBalancer(
mockLoadBalancer(
Expand Down Expand Up @@ -198,7 +198,7 @@ public void stopsRetriesWhenRetryPolicyTellsToStop() {
StyxHostHttpClient secondClient = mockHostClient(Observable.error(new OriginUnreachableException(ORIGIN_2, new RuntimeException("An error occurred"))));
StyxHostHttpClient thirdClient = mockHostClient(Observable.error(new OriginUnreachableException(ORIGIN_2, new RuntimeException("An error occurred"))));

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService)
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.metricsRegistry(metricRegistry)
.loadBalancer(
mockLoadBalancer(
Expand Down Expand Up @@ -230,7 +230,7 @@ public void retriesAtMost3Times() {
StyxHostHttpClient thirdClient = mockHostClient(Observable.error(new OriginUnreachableException(ORIGIN_3, new RuntimeException("An error occurred"))));
StyxHostHttpClient fourthClient = mockHostClient(Observable.error(new OriginUnreachableException(ORIGIN_4, new RuntimeException("An error occurred"))));

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService)
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.metricsRegistry(metricRegistry)
.loadBalancer(
mockLoadBalancer(
Expand Down Expand Up @@ -262,7 +262,7 @@ public void retriesAtMost3Times() {
public void incrementsResponseStatusMetricsForBadResponse() {
StyxHostHttpClient hostClient = mockHostClient(just(response(BAD_REQUEST).build()));

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService)
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.metricsRegistry(metricRegistry)
.loadBalancer(
mockLoadBalancer(Optional.of(remoteHost(SOME_ORIGIN, toHandler(hostClient), hostClient))))
Expand All @@ -279,7 +279,7 @@ public void incrementsResponseStatusMetricsForBadResponse() {
public void incrementsResponseStatusMetricsFor401() {
StyxHostHttpClient hostClient = mockHostClient(just(response(UNAUTHORIZED).build()));

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService)
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.metricsRegistry(metricRegistry)
.loadBalancer(
mockLoadBalancer(Optional.of(remoteHost(SOME_ORIGIN, toHandler(hostClient), hostClient)))
Expand All @@ -297,7 +297,7 @@ public void incrementsResponseStatusMetricsFor401() {
public void incrementsResponseStatusMetricsFor500() {
StyxHostHttpClient hostClient = mockHostClient(just(response(INTERNAL_SERVER_ERROR).build()));

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService)
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.metricsRegistry(metricRegistry)
.loadBalancer(
mockLoadBalancer(Optional.of(remoteHost(SOME_ORIGIN, toHandler(hostClient), hostClient)))
Expand All @@ -315,7 +315,7 @@ public void incrementsResponseStatusMetricsFor500() {
public void incrementsResponseStatusMetricsFor501() {
StyxHostHttpClient hostClient = mockHostClient(just(response(NOT_IMPLEMENTED).build()));

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService)
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.metricsRegistry(metricRegistry)
.loadBalancer(
mockLoadBalancer(Optional.of(remoteHost(SOME_ORIGIN, toHandler(hostClient), hostClient)))
Expand All @@ -336,7 +336,7 @@ public void removesBadContentLength() {
.addHeader(TRANSFER_ENCODING, CHUNKED)
.build()));

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService)
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.metricsRegistry(metricRegistry)
.loadBalancer(
mockLoadBalancer(Optional.of(remoteHost(SOME_ORIGIN, toHandler(hostClient), hostClient)))
Expand All @@ -358,7 +358,7 @@ public void updatesCountersWhenTransactionIsCancelled() {
PublishSubject<HttpResponse> responseSubject = PublishSubject.create();
StyxHostHttpClient hostClient = mockHostClient(responseSubject);

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendWithOrigins(origin.host().getPort()))
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.loadBalancer(
mockLoadBalancer(Optional.of(remoteHost(origin, toHandler(hostClient), hostClient)))
)
Expand All @@ -382,7 +382,7 @@ public void prefersStickyOrigins() {

LoadBalancer loadBalancer = mockLoadBalancer(Optional.of(remoteHost(origin, toHandler(hostClient), hostClient)));

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendWithOrigins(origin.host().getPort()))
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.loadBalancer(loadBalancer)
.metricsRegistry(metricRegistry)
.build();
Expand All @@ -408,7 +408,7 @@ public void prefersRestrictedOrigins() {

LoadBalancer loadBalancer = mockLoadBalancer(Optional.of(remoteHost(origin, toHandler(hostClient), hostClient)));

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendWithOrigins(origin.host().getPort()))
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.loadBalancer(loadBalancer)
.metricsRegistry(metricRegistry)
.originsRestrictionCookieName("restrictedOrigin")
Expand All @@ -434,7 +434,7 @@ public void prefersRestrictedOriginsOverStickyOriginsWhenBothAreConfigured() {
StyxHostHttpClient hostClient = mockHostClient(just(response(OK).build()));
LoadBalancer loadBalancer = mockLoadBalancer(Optional.of(remoteHost(origin, toHandler(hostClient), hostClient)));

StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendWithOrigins(origin.host().getPort()))
StyxHttpClient styxHttpClient = new StyxHttpClient.Builder(backendService.id())
.originsRestrictionCookieName("restrictedOrigin")
.loadBalancer(loadBalancer)
.metricsRegistry(metricRegistry)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ public HttpClient createClient(BackendService backendService, OriginsInventory o
originRestrictionCookie
);

return new StyxHttpClient.Builder(backendService)
return new StyxHttpClient.Builder(backendService.id())
.loadBalancer(loadBalancingStrategy)
.stickySessionConfig(backendService.stickySessionConfig())
.metricsRegistry(environment.metricRegistry())
.retryPolicy(retryPolicy)
.enableContentValidation()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.hotels.styx
import java.nio.charset.StandardCharsets.UTF_8

import com.hotels.styx.api.HttpRequest.get
import com.hotels.styx.api.Id.id
import com.hotels.styx.api.extension.ActiveOrigins
import com.hotels.styx.api.extension.loadbalancing.spi.LoadBalancer
import com.hotels.styx.api.HttpResponseStatus._
Expand Down Expand Up @@ -67,7 +68,7 @@ class HttpResponseSpec extends FunSuite
origins = Origins(originOne),
responseTimeout = responseTimeout)

client = newHttpClientBuilder(backendService.asJava)
client = newHttpClientBuilder(id(backendService.appId))
.loadBalancer(busyConnectionStrategy(activeOrigins(backendService.asJava)))
.build
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class ExpiringConnectionSpec extends FunSpec
.origins(newOriginBuilder("localhost", styxServer.httpPort).build())
.build()

pooledClient = newHttpClientBuilder(backendService)
pooledClient = newHttpClientBuilder(backendService.id)
.loadBalancer(roundRobinStrategy(activeOrigins(backendService)))
.build
}
Expand Down
Loading