@Test public void usesTheOriginSpecifiedInTheOriginsRestrictionCookie() { MapBackedConfiguration config = new MapBackedConfiguration(); config.set("originRestrictionCookie", ORIGINS_RESTRICTION_COOKIE); environment = new Environment.Builder() .configuration(new StyxConfig(config)) .build(); BackendService backendService = newBackendServiceBuilder() .origins( newOriginBuilder("localhost", 9091).id("x").build(), newOriginBuilder("localhost", 9092).id("y").build(), newOriginBuilder("localhost", 9093).id("z").build()) .build(); BackendServiceClient styxBackendServiceClient = new StyxBackendServiceClientFactory(environment) .createClient( backendService, newOriginsInventoryBuilder(backendService) .hostClientFactory((pool) -> { if (pool.getOrigin().id().equals(id("x"))) { return hostClient(response(OK).header("X-Origin-Id", "x").build()); } else if (pool.getOrigin().id().equals(id("y"))) { return hostClient(response(OK).header("X-Origin-Id", "y").build()); } else { return hostClient(response(OK).header("X-Origin-Id", "z").build()); } }) .build(), new CachingOriginStatsFactory(new CodaHaleMetricRegistry())); LiveHttpRequest requestz = get("/some-req").cookies(requestCookie(ORIGINS_RESTRICTION_COOKIE, id("z").toString())).build(); LiveHttpRequest requestx = get("/some-req").cookies(requestCookie(ORIGINS_RESTRICTION_COOKIE, id("x").toString())).build(); LiveHttpRequest requesty = get("/some-req").cookies(requestCookie(ORIGINS_RESTRICTION_COOKIE, id("y").toString())).build(); LiveHttpResponse responsez = Mono.from(styxBackendServiceClient.sendRequest(requestz, requestContext())).block(); LiveHttpResponse responsex = Mono.from(styxBackendServiceClient.sendRequest(requestx, requestContext())).block(); LiveHttpResponse responsey = Mono.from(styxBackendServiceClient.sendRequest(requesty, requestContext())).block(); assertThat(responsex.header("X-Origin-Id").get(), is("x")); assertThat(responsey.header("X-Origin-Id").get(), is("y")); assertThat(responsez.header("X-Origin-Id").get(), is("z")); }
@Override public BackendServiceClient createClient(BackendService backendService, OriginsInventory originsInventory, OriginStatsFactory originStatsFactory) { Configuration styxConfig = environment.configuration(); String originRestrictionCookie = styxConfig.get("originRestrictionCookie").orElse(null); boolean stickySessionEnabled = backendService.stickySessionConfig().stickySessionEnabled(); RetryPolicy retryPolicy = loadRetryPolicy(styxConfig, environment, "retrypolicy.policy.factory", RetryPolicy.class) .orElseGet(() -> defaultRetryPolicy(environment)); LoadBalancer configuredLbStrategy = loadLoadBalancer( styxConfig, environment, "loadBalancing.strategy.factory", LoadBalancer.class, originsInventory) .orElseGet(() -> new BusyConnectionsStrategy(originsInventory)); originsInventory.addOriginsChangeListener(configuredLbStrategy); LoadBalancer loadBalancingStrategy = decorateLoadBalancer( configuredLbStrategy, stickySessionEnabled, originsInventory, originRestrictionCookie ); return new StyxBackendServiceClient.Builder(backendService.id()) .loadBalancer(loadBalancingStrategy) .stickySessionConfig(backendService.stickySessionConfig()) .metricsRegistry(environment.metricRegistry()) .retryPolicy(retryPolicy) .rewriteRules(backendService.rewrites()) .originStatsFactory(originStatsFactory) .originsRestrictionCookieName(originRestrictionCookie) .originIdHeader(environment.styxConfig().styxHeaderConfig().originIdHeaderName()) .build(); }
StyxBackendServiceClientFactory implements BackendServiceClientFactory { @Override public BackendServiceClient createClient(BackendService backendService, OriginsInventory originsInventory, OriginStatsFactory originStatsFactory) { Configuration styxConfig = environment.configuration(); String originRestrictionCookie = styxConfig.get("originRestrictionCookie").orElse(null); boolean stickySessionEnabled = backendService.stickySessionConfig().stickySessionEnabled(); RetryPolicy retryPolicy = loadRetryPolicy(styxConfig, environment, "retrypolicy.policy.factory", RetryPolicy.class) .orElseGet(() -> defaultRetryPolicy(environment)); LoadBalancer configuredLbStrategy = loadLoadBalancer( styxConfig, environment, "loadBalancing.strategy.factory", LoadBalancer.class, originsInventory) .orElseGet(() -> new BusyConnectionsStrategy(originsInventory)); originsInventory.addOriginsChangeListener(configuredLbStrategy); LoadBalancer loadBalancingStrategy = decorateLoadBalancer( configuredLbStrategy, stickySessionEnabled, originsInventory, originRestrictionCookie ); return new StyxBackendServiceClient.Builder(backendService.id()) .loadBalancer(loadBalancingStrategy) .stickySessionConfig(backendService.stickySessionConfig()) .metricsRegistry(environment.metricRegistry()) .retryPolicy(retryPolicy) .rewriteRules(backendService.rewrites()) .originStatsFactory(originStatsFactory) .originsRestrictionCookieName(originRestrictionCookie) .originIdHeader(environment.styxConfig().styxHeaderConfig().originIdHeaderName()) .build(); } }
StyxBackendServiceClientFactory implements BackendServiceClientFactory { @Override public BackendServiceClient createClient(BackendService backendService, OriginsInventory originsInventory, OriginStatsFactory originStatsFactory) { Configuration styxConfig = environment.configuration(); String originRestrictionCookie = styxConfig.get("originRestrictionCookie").orElse(null); boolean stickySessionEnabled = backendService.stickySessionConfig().stickySessionEnabled(); RetryPolicy retryPolicy = loadRetryPolicy(styxConfig, environment, "retrypolicy.policy.factory", RetryPolicy.class) .orElseGet(() -> defaultRetryPolicy(environment)); LoadBalancer configuredLbStrategy = loadLoadBalancer( styxConfig, environment, "loadBalancing.strategy.factory", LoadBalancer.class, originsInventory) .orElseGet(() -> new BusyConnectionsStrategy(originsInventory)); originsInventory.addOriginsChangeListener(configuredLbStrategy); LoadBalancer loadBalancingStrategy = decorateLoadBalancer( configuredLbStrategy, stickySessionEnabled, originsInventory, originRestrictionCookie ); return new StyxBackendServiceClient.Builder(backendService.id()) .loadBalancer(loadBalancingStrategy) .stickySessionConfig(backendService.stickySessionConfig()) .metricsRegistry(environment.metricRegistry()) .retryPolicy(retryPolicy) .rewriteRules(backendService.rewrites()) .originStatsFactory(originStatsFactory) .originsRestrictionCookieName(originRestrictionCookie) .originIdHeader(environment.styxConfig().styxHeaderConfig().originIdHeaderName()) .build(); } StyxBackendServiceClientFactory(Environment environment); }
StyxBackendServiceClientFactory implements BackendServiceClientFactory { @Override public BackendServiceClient createClient(BackendService backendService, OriginsInventory originsInventory, OriginStatsFactory originStatsFactory) { Configuration styxConfig = environment.configuration(); String originRestrictionCookie = styxConfig.get("originRestrictionCookie").orElse(null); boolean stickySessionEnabled = backendService.stickySessionConfig().stickySessionEnabled(); RetryPolicy retryPolicy = loadRetryPolicy(styxConfig, environment, "retrypolicy.policy.factory", RetryPolicy.class) .orElseGet(() -> defaultRetryPolicy(environment)); LoadBalancer configuredLbStrategy = loadLoadBalancer( styxConfig, environment, "loadBalancing.strategy.factory", LoadBalancer.class, originsInventory) .orElseGet(() -> new BusyConnectionsStrategy(originsInventory)); originsInventory.addOriginsChangeListener(configuredLbStrategy); LoadBalancer loadBalancingStrategy = decorateLoadBalancer( configuredLbStrategy, stickySessionEnabled, originsInventory, originRestrictionCookie ); return new StyxBackendServiceClient.Builder(backendService.id()) .loadBalancer(loadBalancingStrategy) .stickySessionConfig(backendService.stickySessionConfig()) .metricsRegistry(environment.metricRegistry()) .retryPolicy(retryPolicy) .rewriteRules(backendService.rewrites()) .originStatsFactory(originStatsFactory) .originsRestrictionCookieName(originRestrictionCookie) .originIdHeader(environment.styxConfig().styxHeaderConfig().originIdHeaderName()) .build(); } StyxBackendServiceClientFactory(Environment environment); @Override BackendServiceClient createClient(BackendService backendService, OriginsInventory originsInventory, OriginStatsFactory originStatsFactory); }
StyxBackendServiceClientFactory implements BackendServiceClientFactory { @Override public BackendServiceClient createClient(BackendService backendService, OriginsInventory originsInventory, OriginStatsFactory originStatsFactory) { Configuration styxConfig = environment.configuration(); String originRestrictionCookie = styxConfig.get("originRestrictionCookie").orElse(null); boolean stickySessionEnabled = backendService.stickySessionConfig().stickySessionEnabled(); RetryPolicy retryPolicy = loadRetryPolicy(styxConfig, environment, "retrypolicy.policy.factory", RetryPolicy.class) .orElseGet(() -> defaultRetryPolicy(environment)); LoadBalancer configuredLbStrategy = loadLoadBalancer( styxConfig, environment, "loadBalancing.strategy.factory", LoadBalancer.class, originsInventory) .orElseGet(() -> new BusyConnectionsStrategy(originsInventory)); originsInventory.addOriginsChangeListener(configuredLbStrategy); LoadBalancer loadBalancingStrategy = decorateLoadBalancer( configuredLbStrategy, stickySessionEnabled, originsInventory, originRestrictionCookie ); return new StyxBackendServiceClient.Builder(backendService.id()) .loadBalancer(loadBalancingStrategy) .stickySessionConfig(backendService.stickySessionConfig()) .metricsRegistry(environment.metricRegistry()) .retryPolicy(retryPolicy) .rewriteRules(backendService.rewrites()) .originStatsFactory(originStatsFactory) .originsRestrictionCookieName(originRestrictionCookie) .originIdHeader(environment.styxConfig().styxHeaderConfig().originIdHeaderName()) .build(); } StyxBackendServiceClientFactory(Environment environment); @Override BackendServiceClient createClient(BackendService backendService, OriginsInventory originsInventory, OriginStatsFactory originStatsFactory);  }