@Test public void closesClientWhenBackendServicesAreRemoved() { BackendServiceClient firstClient = mock(BackendServiceClient.class); BackendServiceClient secondClient = mock(BackendServiceClient.class); ArgumentCaptor<OriginsInventory> originsInventory = forClass(OriginsInventory.class); BackendServiceClientFactory clientFactory = mock(BackendServiceClientFactory.class); when(clientFactory.createClient(any(BackendService.class), any(OriginsInventory.class), any(OriginStatsFactory.class))) .thenReturn(firstClient) .thenReturn(secondClient); BackendServicesRouter router = new BackendServicesRouter(clientFactory, environment, executor); BackendService bookingApp = appB(); router.onChange(added(bookingApp)); verify(clientFactory).createClient(eq(bookingApp), originsInventory.capture(), any(OriginStatsFactory.class)); router.onChange(removed(bookingApp)); assertThat(originsInventory.getValue().closed(), is(true)); }
@Override public void onChange(Registry.Changes<BackendService> changes) { changes.removed().forEach(backendService -> routes.remove(backendService.path()).close()); concat(changes.added(), changes.updated()).forEach(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); MetricRegistry originsMetrics = environment.metricRegistry().scope("origins"); OriginStatsFactory originStatsFactory = new CachingOriginStatsFactory(originsMetrics); 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()) .metricRegistry(originsMetrics) .build(); OriginHealthStatusMonitor healthStatusMonitor = healthStatusMonitor(backendService); OriginsInventory inventory = new OriginsInventory.Builder(backendService.id()) .eventBus(environment.eventBus()) .metricsRegistry(originsMetrics) .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()); }); }
BackendServicesRouter implements HttpRouter, Registry.ChangeListener<BackendService> { @Override public void onChange(Registry.Changes<BackendService> changes) { changes.removed().forEach(backendService -> routes.remove(backendService.path()).close()); concat(changes.added(), changes.updated()).forEach(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); MetricRegistry originsMetrics = environment.metricRegistry().scope("origins"); OriginStatsFactory originStatsFactory = new CachingOriginStatsFactory(originsMetrics); 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()) .metricRegistry(originsMetrics) .build(); OriginHealthStatusMonitor healthStatusMonitor = healthStatusMonitor(backendService); OriginsInventory inventory = new OriginsInventory.Builder(backendService.id()) .eventBus(environment.eventBus()) .metricsRegistry(originsMetrics) .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()); }); } }
BackendServicesRouter implements HttpRouter, Registry.ChangeListener<BackendService> { @Override public void onChange(Registry.Changes<BackendService> changes) { changes.removed().forEach(backendService -> routes.remove(backendService.path()).close()); concat(changes.added(), changes.updated()).forEach(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); MetricRegistry originsMetrics = environment.metricRegistry().scope("origins"); OriginStatsFactory originStatsFactory = new CachingOriginStatsFactory(originsMetrics); 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()) .metricRegistry(originsMetrics) .build(); OriginHealthStatusMonitor healthStatusMonitor = healthStatusMonitor(backendService); OriginsInventory inventory = new OriginsInventory.Builder(backendService.id()) .eventBus(environment.eventBus()) .metricsRegistry(originsMetrics) .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()); }); } BackendServicesRouter(BackendServiceClientFactory clientFactory,
                                 Environment environment,
                                 NettyExecutor executor); }
BackendServicesRouter implements HttpRouter, Registry.ChangeListener<BackendService> { @Override public void onChange(Registry.Changes<BackendService> changes) { changes.removed().forEach(backendService -> routes.remove(backendService.path()).close()); concat(changes.added(), changes.updated()).forEach(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); MetricRegistry originsMetrics = environment.metricRegistry().scope("origins"); OriginStatsFactory originStatsFactory = new CachingOriginStatsFactory(originsMetrics); 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()) .metricRegistry(originsMetrics) .build(); OriginHealthStatusMonitor healthStatusMonitor = healthStatusMonitor(backendService); OriginsInventory inventory = new OriginsInventory.Builder(backendService.id()) .eventBus(environment.eventBus()) .metricsRegistry(originsMetrics) .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()); }); } BackendServicesRouter(BackendServiceClientFactory clientFactory,
                                 Environment environment,
                                 NettyExecutor executor); @Override Optional<HttpHandler> route(LiveHttpRequest request, HttpInterceptor.Context ignore); @Override void onChange(Registry.Changes<BackendService> changes); }
BackendServicesRouter implements HttpRouter, Registry.ChangeListener<BackendService> { @Override public void onChange(Registry.Changes<BackendService> changes) { changes.removed().forEach(backendService -> routes.remove(backendService.path()).close()); concat(changes.added(), changes.updated()).forEach(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); MetricRegistry originsMetrics = environment.metricRegistry().scope("origins"); OriginStatsFactory originStatsFactory = new CachingOriginStatsFactory(originsMetrics); 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()) .metricRegistry(originsMetrics) .build(); OriginHealthStatusMonitor healthStatusMonitor = healthStatusMonitor(backendService); OriginsInventory inventory = new OriginsInventory.Builder(backendService.id()) .eventBus(environment.eventBus()) .metricsRegistry(originsMetrics) .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()); }); } BackendServicesRouter(BackendServiceClientFactory clientFactory,
                                 Environment environment,
                                 NettyExecutor executor); @Override Optional<HttpHandler> route(LiveHttpRequest request, HttpInterceptor.Context ignore); @Override void onChange(Registry.Changes<BackendService> changes);  }