From 6d79acdd707c0cfbae069ec66a8bfbbf93370532 Mon Sep 17 00:00:00 2001 From: Mikko Karjalainen Date: Tue, 7 Jan 2020 11:51:15 +0000 Subject: [PATCH] Remove ProxyServerBuilder class. --- .../{proxy => }/ProxyConnectorFactory.java | 4 +- .../styx/{proxy => }/ResponseInfoFormat.java | 8 +- .../main/java/com/hotels/styx/StyxServer.java | 31 +++++- .../hotels/styx/proxy/ProxyServerBuilder.java | 101 ------------------ .../{proxy => }/ResponseInfoFormatTest.java | 4 +- .../styx/plugins/ErrorMetricsSpec.scala | 3 +- 6 files changed, 36 insertions(+), 115 deletions(-) rename components/proxy/src/main/java/com/hotels/styx/{proxy => }/ProxyConnectorFactory.java (98%) rename components/proxy/src/main/java/com/hotels/styx/{proxy => }/ResponseInfoFormat.java (90%) delete mode 100644 components/proxy/src/main/java/com/hotels/styx/proxy/ProxyServerBuilder.java rename components/proxy/src/test/java/com/hotels/styx/{proxy => }/ResponseInfoFormatTest.java (95%) diff --git a/components/proxy/src/main/java/com/hotels/styx/proxy/ProxyConnectorFactory.java b/components/proxy/src/main/java/com/hotels/styx/ProxyConnectorFactory.java similarity index 98% rename from components/proxy/src/main/java/com/hotels/styx/proxy/ProxyConnectorFactory.java rename to components/proxy/src/main/java/com/hotels/styx/ProxyConnectorFactory.java index 59e9ada55b..f37efa9414 100644 --- a/components/proxy/src/main/java/com/hotels/styx/proxy/ProxyConnectorFactory.java +++ b/components/proxy/src/main/java/com/hotels/styx/ProxyConnectorFactory.java @@ -13,11 +13,13 @@ See the License for the specific language governing permissions and limitations under the License. */ -package com.hotels.styx.proxy; +package com.hotels.styx; import com.codahale.metrics.Histogram; import com.hotels.styx.api.HttpHandler; import com.hotels.styx.api.MetricRegistry; +import com.hotels.styx.proxy.HttpCompressor; +import com.hotels.styx.proxy.ServerProtocolDistributionRecorder; import com.hotels.styx.proxy.encoders.ConfigurableUnwiseCharsEncoder; import com.hotels.styx.server.ConnectorConfig; import com.hotels.styx.server.HttpErrorStatusListener; diff --git a/components/proxy/src/main/java/com/hotels/styx/proxy/ResponseInfoFormat.java b/components/proxy/src/main/java/com/hotels/styx/ResponseInfoFormat.java similarity index 90% rename from components/proxy/src/main/java/com/hotels/styx/proxy/ResponseInfoFormat.java rename to components/proxy/src/main/java/com/hotels/styx/ResponseInfoFormat.java index b6dca6dbe1..a7c986a64a 100644 --- a/components/proxy/src/main/java/com/hotels/styx/proxy/ResponseInfoFormat.java +++ b/components/proxy/src/main/java/com/hotels/styx/ResponseInfoFormat.java @@ -1,5 +1,5 @@ /* - Copyright (C) 2013-2018 Expedia Inc. + Copyright (C) 2013-2019 Expedia Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -13,9 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package com.hotels.styx.proxy; - -import com.hotels.styx.Environment; +package com.hotels.styx; import static com.hotels.styx.StyxConfig.NO_JVM_ROUTE_SET; import com.hotels.styx.api.LiveHttpRequest; @@ -23,7 +21,7 @@ /** * Formats response info into a string. */ -public class ResponseInfoFormat { +class ResponseInfoFormat { private final String format; ResponseInfoFormat(Environment environment) { diff --git a/components/proxy/src/main/java/com/hotels/styx/StyxServer.java b/components/proxy/src/main/java/com/hotels/styx/StyxServer.java index 42dce37fef..807961310e 100644 --- a/components/proxy/src/main/java/com/hotels/styx/StyxServer.java +++ b/components/proxy/src/main/java/com/hotels/styx/StyxServer.java @@ -29,10 +29,14 @@ import com.hotels.styx.api.extension.service.spi.StyxService; import com.hotels.styx.config.schema.SchemaValidationException; import com.hotels.styx.infrastructure.MemoryBackedRegistry; -import com.hotels.styx.proxy.ProxyServerBuilder; import com.hotels.styx.proxy.plugin.NamedPlugin; import com.hotels.styx.server.ConnectorConfig; import com.hotels.styx.server.HttpServer; +import com.hotels.styx.server.ServerEventLoopFactory; +import com.hotels.styx.server.netty.NettyServerBuilder; +import com.hotels.styx.server.netty.NettyServerConfig; +import com.hotels.styx.server.netty.ServerConnector; +import com.hotels.styx.server.netty.eventloop.PlatformAwareServerEventLoopFactory; import com.hotels.styx.startup.StyxServerComponents; import io.netty.util.ResourceLeakDetector; import org.jetbrains.annotations.NotNull; @@ -50,6 +54,8 @@ import static com.hotels.styx.infrastructure.logging.LOGBackConfigurer.initLogging; import static com.hotels.styx.infrastructure.logging.LOGBackConfigurer.shutdownLogging; +import static com.hotels.styx.proxy.encoders.ConfigurableUnwiseCharsEncoder.ENCODE_UNWISECHARS; +import static com.hotels.styx.server.netty.eventloop.ServerEventLoopFactories.memoize; import static com.hotels.styx.startup.CoreMetrics.registerCoreMetrics; import static io.netty.util.ResourceLeakDetector.Level.DISABLED; import static java.lang.Runtime.getRuntime; @@ -224,10 +230,29 @@ public InetSocketAddress adminHttpAddress() { return adminServer.inetAddress(); } + // This function will be removed in near future: + private static ServerEventLoopFactory serverEventLoopFactory(String name, NettyServerConfig serverConfig) { + return memoize(new PlatformAwareServerEventLoopFactory(name, serverConfig.bossThreadsCount(), serverConfig.workerThreadsCount())); + } + private static HttpServer httpServer(Environment environment, ConnectorConfig connectorConfig, HttpHandler styxDataPlane) { - return new ProxyServerBuilder(environment) + CharSequence styxInfoHeaderName = environment.configuration().styxHeaderConfig().styxInfoHeaderName(); + ResponseInfoFormat responseInfoFormat = new ResponseInfoFormat(environment); + + ServerConnector proxyConnector = new ProxyConnectorFactory( + environment.configuration().proxyServerConfig(), + environment.metricRegistry(), + environment.errorListener(), + environment.configuration().get(ENCODE_UNWISECHARS).orElse(""), + (builder, request) -> builder.header(styxInfoHeaderName, responseInfoFormat.format(request)), + environment.configuration().get("requestTracking", Boolean.class).orElse(false)) + .create(connectorConfig); + + return NettyServerBuilder.newBuilder() + .setMetricsRegistry(environment.metricRegistry()) + .setServerEventLoopFactory(serverEventLoopFactory("Proxy", environment.configuration().proxyServerConfig())) + .setProtocolConnector(proxyConnector) .handler(styxDataPlane) - .connectorConfig(connectorConfig) .build(); } diff --git a/components/proxy/src/main/java/com/hotels/styx/proxy/ProxyServerBuilder.java b/components/proxy/src/main/java/com/hotels/styx/proxy/ProxyServerBuilder.java deleted file mode 100644 index 2da718885f..0000000000 --- a/components/proxy/src/main/java/com/hotels/styx/proxy/ProxyServerBuilder.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - Copyright (C) 2013-2019 Expedia Inc. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - */ -package com.hotels.styx.proxy; - -import com.hotels.styx.Environment; -import com.hotels.styx.api.HttpHandler; -import com.hotels.styx.api.LiveHttpRequest; -import com.hotels.styx.api.LiveHttpResponse; -import com.hotels.styx.api.MetricRegistry; -import com.hotels.styx.server.ConnectorConfig; -import com.hotels.styx.server.HttpErrorStatusListener; -import com.hotels.styx.server.HttpServer; -import com.hotels.styx.server.ServerEventLoopFactory; -import com.hotels.styx.server.netty.NettyServerBuilder; -import com.hotels.styx.server.netty.NettyServerConfig; -import com.hotels.styx.server.netty.eventloop.PlatformAwareServerEventLoopFactory; -import org.slf4j.Logger; - -import static com.hotels.styx.proxy.encoders.ConfigurableUnwiseCharsEncoder.ENCODE_UNWISECHARS; -import static com.hotels.styx.server.netty.eventloop.ServerEventLoopFactories.memoize; -import static org.slf4j.LoggerFactory.getLogger; - -/** - * A builder for a ProxyServer. - */ -public final class ProxyServerBuilder { - private static final Logger LOG = getLogger(ProxyServerBuilder.class); - - private final ResponseInfoFormat responseInfoFormat; - private final CharSequence styxInfoHeaderName; - private final ProxyServerConfig serverConfig; - private final String unwiseCharacters; - private final Boolean requestTracking; - private final MetricRegistry metricRegistry; - private final HttpErrorStatusListener errorListener; - - private HttpHandler handler; - private ConnectorConfig connectorConfig; - - public ProxyServerBuilder(Environment environment) { - this.serverConfig = environment.configuration().proxyServerConfig(); - this.errorListener = environment.errorListener(); - this.metricRegistry = environment.metricRegistry(); - this.requestTracking = environment.configuration().get("requestTracking", Boolean.class).orElse(false); - this.unwiseCharacters = environment.styxConfig().get(ENCODE_UNWISECHARS).orElse(""); - this.responseInfoFormat = new ResponseInfoFormat(environment); - this.styxInfoHeaderName = environment.styxConfig().styxHeaderConfig().styxInfoHeaderName(); - } - - private ServerEventLoopFactory serverEventLoopFactory(String name, NettyServerConfig serverConfig) { - return memoize(new PlatformAwareServerEventLoopFactory(name, serverConfig.bossThreadsCount(), serverConfig.workerThreadsCount())); - } - - public HttpServer build() { - LOG.info("connectors={} name={}", this.serverConfig.connectors(), "Proxy"); - - HttpServer builder = NettyServerBuilder.newBuilder() - .setMetricsRegistry(this.metricRegistry) - .setServerEventLoopFactory(serverEventLoopFactory("Proxy", this.serverConfig)) - .setProtocolConnector( - new ProxyConnectorFactory( - this.serverConfig, - this.metricRegistry, - this.errorListener, - this.unwiseCharacters, - this::addInfoHeader, - this.requestTracking) - .create(connectorConfig)) - .handler(handler) - .build(); - - return builder; - } - - private LiveHttpResponse.Transformer addInfoHeader(LiveHttpResponse.Transformer responseBuilder, LiveHttpRequest request) { - return responseBuilder.header(styxInfoHeaderName, responseInfoFormat.format(request)); - } - - public ProxyServerBuilder handler(HttpHandler handler) { - this.handler = handler; - return this; - } - - public ProxyServerBuilder connectorConfig(ConnectorConfig connectorConfig) { - this.connectorConfig = connectorConfig; - return this; - } -} diff --git a/components/proxy/src/test/java/com/hotels/styx/proxy/ResponseInfoFormatTest.java b/components/proxy/src/test/java/com/hotels/styx/ResponseInfoFormatTest.java similarity index 95% rename from components/proxy/src/test/java/com/hotels/styx/proxy/ResponseInfoFormatTest.java rename to components/proxy/src/test/java/com/hotels/styx/ResponseInfoFormatTest.java index 46c32f2f03..33fdd75709 100644 --- a/components/proxy/src/test/java/com/hotels/styx/proxy/ResponseInfoFormatTest.java +++ b/components/proxy/src/test/java/com/hotels/styx/ResponseInfoFormatTest.java @@ -13,10 +13,8 @@ See the License for the specific language governing permissions and limitations under the License. */ -package com.hotels.styx.proxy; +package com.hotels.styx; -import com.hotels.styx.Environment; -import com.hotels.styx.StyxConfig; import com.hotels.styx.api.LiveHttpRequest; import com.hotels.styx.api.configuration.Configuration; import com.hotels.styx.api.configuration.Configuration.MapBackedConfiguration; diff --git a/system-tests/e2e-suite/src/test/scala/com/hotels/styx/plugins/ErrorMetricsSpec.scala b/system-tests/e2e-suite/src/test/scala/com/hotels/styx/plugins/ErrorMetricsSpec.scala index 0989ff5f81..9ff4071ef5 100644 --- a/system-tests/e2e-suite/src/test/scala/com/hotels/styx/plugins/ErrorMetricsSpec.scala +++ b/system-tests/e2e-suite/src/test/scala/com/hotels/styx/plugins/ErrorMetricsSpec.scala @@ -1,5 +1,5 @@ /* - Copyright (C) 2013-2019 Expedia Inc. + Copyright (C) 2013-2020 Expedia Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -29,7 +29,6 @@ import com.hotels.styx.api.HttpResponseStatus.{BAD_GATEWAY, INTERNAL_SERVER_ERRO import com.hotels.styx.api.extension.service.BackendService import com.hotels.styx.infrastructure.{MemoryBackedRegistry, RegistryServiceAdapter} import com.hotels.styx.support.ImplicitStyxConversions -import com.hotels.styx.support.backends.FakeHttpServer import com.hotels.styx.support.backends.FakeHttpServer.HttpStartupConfig import com.hotels.styx.support.configuration import com.hotels.styx.support.configuration._