Search in sources :

Example 1 with CachingOriginStatsFactory

use of com.hotels.styx.client.OriginStatsFactory.CachingOriginStatsFactory in project styx by ExpediaGroup.

the class StyxBackendServiceClientFactoryTest method usesTheOriginSpecifiedInTheOriginsRestrictionCookie.

@Test
public void usesTheOriginSpecifiedInTheOriginsRestrictionCookie() {
    MapBackedConfiguration config = new MapBackedConfiguration();
    config.set("originRestrictionCookie", ORIGINS_RESTRICTION_COOKIE);
    environment = new Environment.Builder().registry(new MicrometerRegistry(new SimpleMeterRegistry())).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(environment.centralisedMetrics(), 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(environment.centralisedMetrics()));
    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"));
}
Also used : BackendService(com.hotels.styx.api.extension.service.BackendService) MicrometerRegistry(com.hotels.styx.api.MicrometerRegistry) StyxConfig(com.hotels.styx.StyxConfig) StyxBackendServiceClient(com.hotels.styx.client.StyxBackendServiceClient) BackendServiceClient(com.hotels.styx.client.BackendServiceClient) CachingOriginStatsFactory(com.hotels.styx.client.OriginStatsFactory.CachingOriginStatsFactory) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) OriginsInventory.newOriginsInventoryBuilder(com.hotels.styx.client.OriginsInventory.newOriginsInventoryBuilder) Origin.newOriginBuilder(com.hotels.styx.api.extension.Origin.newOriginBuilder) BackendService.newBackendServiceBuilder(com.hotels.styx.api.extension.service.BackendService.newBackendServiceBuilder) StickySessionConfig.newStickySessionConfigBuilder(com.hotels.styx.api.extension.service.StickySessionConfig.newStickySessionConfigBuilder) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) MapBackedConfiguration(com.hotels.styx.api.configuration.Configuration.MapBackedConfiguration) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 2 with CachingOriginStatsFactory

use of com.hotels.styx.client.OriginStatsFactory.CachingOriginStatsFactory in project styx by ExpediaGroup.

the class StyxBackendServiceClientFactoryTest method usesTheOriginSpecifiedInTheStickySessionCookie.

@Test
public void usesTheOriginSpecifiedInTheStickySessionCookie() {
    BackendService backendService = newBackendServiceBuilder().origins(newOriginBuilder("localhost", 9091).id("x").build(), newOriginBuilder("localhost", 9092).id("y").build(), newOriginBuilder("localhost", 9093).id("z").build()).stickySessionConfig(newStickySessionConfigBuilder().enabled(true).build()).build();
    BackendServiceClient styxBackendServiceClient = new StyxBackendServiceClientFactory(environment).createClient(backendService, newOriginsInventoryBuilder(environment.centralisedMetrics(), 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(environment.centralisedMetrics()));
    LiveHttpRequest requestz = get("/some-req").cookies(requestCookie(STICKY_COOKIE, id("z").toString())).build();
    LiveHttpRequest requestx = get("/some-req").cookies(requestCookie(STICKY_COOKIE, id("x").toString())).build();
    LiveHttpRequest requesty = get("/some-req").cookies(requestCookie(STICKY_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"));
}
Also used : BackendService(com.hotels.styx.api.extension.service.BackendService) StyxBackendServiceClient(com.hotels.styx.client.StyxBackendServiceClient) BackendServiceClient(com.hotels.styx.client.BackendServiceClient) CachingOriginStatsFactory(com.hotels.styx.client.OriginStatsFactory.CachingOriginStatsFactory) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 3 with CachingOriginStatsFactory

use of com.hotels.styx.client.OriginStatsFactory.CachingOriginStatsFactory in project styx by ExpediaGroup.

the class BackendServicesRouter method onChange.

@Override
public void onChange(Registry.Changes<BackendService> changes) {
    changes.removed().forEach(backendService -> routes.remove(backendService.path()).close());
    concatenatedForEach(changes.added(), changes.updated(), backendService -> {
        ProxyToClientPipeline pipeline = routes.get(backendService.path());
        if (pipeline != null) {
            pipeline.close();
        }
        boolean requestLoggingEnabled = environment.styxConfig().get("request-logging.outbound.enabled", Boolean.class).orElse(false);
        boolean longFormat = environment.styxConfig().get("request-logging.outbound.longFormat", Boolean.class).orElse(false);
        OriginStatsFactory originStatsFactory = new CachingOriginStatsFactory(environment.centralisedMetrics());
        ConnectionPoolSettings poolSettings = backendService.connectionPoolConfig();
        Connection.Factory connectionFactory = connectionFactory(backendService, requestLoggingEnabled, longFormat, originStatsFactory, poolSettings.connectionExpirationSeconds());
        ConnectionPool.Factory connectionPoolFactory = new SimpleConnectionPoolFactory.Builder().connectionFactory(connectionFactory).connectionPoolSettings(backendService.connectionPoolConfig()).metrics(environment.centralisedMetrics()).build();
        OriginHealthStatusMonitor healthStatusMonitor = healthStatusMonitor(backendService);
        OriginsInventory inventory = new OriginsInventory.Builder(backendService.id()).eventBus(environment.eventBus()).metrics(environment.centralisedMetrics()).connectionPoolFactory(connectionPoolFactory).originHealthMonitor(healthStatusMonitor).initialOrigins(backendService.origins()).hostClientFactory(StyxHostHttpClient::create).build();
        pipeline = new ProxyToClientPipeline(newClientHandler(backendService, inventory, originStatsFactory), () -> {
            inventory.close();
            healthStatusMonitor.stop();
        });
        routes.put(backendService.path(), pipeline);
        LOG.info("added path={} current routes={}", backendService.path(), routes.keySet());
    });
}
Also used : ConnectionPool(com.hotels.styx.client.connectionpool.ConnectionPool) CachingOriginStatsFactory(com.hotels.styx.client.OriginStatsFactory.CachingOriginStatsFactory) OriginsInventory(com.hotels.styx.client.OriginsInventory) HttpConfig.newHttpConfigBuilder(com.hotels.styx.client.HttpConfig.newHttpConfigBuilder) Builder.httpRequestOperationFactoryBuilder(com.hotels.styx.client.HttpRequestOperationFactory.Builder.httpRequestOperationFactoryBuilder) Connection(com.hotels.styx.client.Connection) OriginHealthStatusMonitor(com.hotels.styx.client.healthcheck.OriginHealthStatusMonitor) CachingOriginStatsFactory(com.hotels.styx.client.OriginStatsFactory.CachingOriginStatsFactory) OriginStatsFactory(com.hotels.styx.client.OriginStatsFactory) ConnectionPoolSettings(com.hotels.styx.api.extension.service.ConnectionPoolSettings)

Aggregations

CachingOriginStatsFactory (com.hotels.styx.client.OriginStatsFactory.CachingOriginStatsFactory)3 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)2 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)2 BackendService (com.hotels.styx.api.extension.service.BackendService)2 BackendServiceClient (com.hotels.styx.client.BackendServiceClient)2 StyxBackendServiceClient (com.hotels.styx.client.StyxBackendServiceClient)2 Test (org.junit.jupiter.api.Test)2 StyxConfig (com.hotels.styx.StyxConfig)1 MicrometerRegistry (com.hotels.styx.api.MicrometerRegistry)1 MapBackedConfiguration (com.hotels.styx.api.configuration.Configuration.MapBackedConfiguration)1 Origin.newOriginBuilder (com.hotels.styx.api.extension.Origin.newOriginBuilder)1 BackendService.newBackendServiceBuilder (com.hotels.styx.api.extension.service.BackendService.newBackendServiceBuilder)1 ConnectionPoolSettings (com.hotels.styx.api.extension.service.ConnectionPoolSettings)1 StickySessionConfig.newStickySessionConfigBuilder (com.hotels.styx.api.extension.service.StickySessionConfig.newStickySessionConfigBuilder)1 Connection (com.hotels.styx.client.Connection)1 HttpConfig.newHttpConfigBuilder (com.hotels.styx.client.HttpConfig.newHttpConfigBuilder)1 Builder.httpRequestOperationFactoryBuilder (com.hotels.styx.client.HttpRequestOperationFactory.Builder.httpRequestOperationFactoryBuilder)1 OriginStatsFactory (com.hotels.styx.client.OriginStatsFactory)1 OriginsInventory (com.hotels.styx.client.OriginsInventory)1 OriginsInventory.newOriginsInventoryBuilder (com.hotels.styx.client.OriginsInventory.newOriginsInventoryBuilder)1