From 504dc154bdb925de9e383f6726d23fb89de14bd7 Mon Sep 17 00:00:00 2001 From: Nikita Smirnov <46124551+Nikita-Smirnov-Exactpro@users.noreply.github.com> Date: Fri, 29 Sep 2023 10:45:24 +0400 Subject: [PATCH] Th2 5090 added gzipCompressionLevel option (#69) --- README.md | 7 ++++++- gradle.properties | 2 +- .../configuration/Configuration.kt | 5 +++++ .../th2/lwdataprovider/http/HttpServer.kt | 21 +++++++------------ 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 755265ce..2f059a32 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Lightweight data provider (2.2.1) +# Lightweight data provider (2.3.0) # Overview This component serves as a data provider for [th2-data-services](https://github.com/th2-net/th2-data-services). It will connect to the cassandra database via [cradle api](https://github.com/th2-net/cradleapi) and expose the data stored in there as REST resources. @@ -118,6 +118,11 @@ spec: # codecUsePinAttributes: true # send raw message to specified codec (true) or send to all codecs (false) # responseFormats: string list # resolve data for selected formats only. (allowed values: BASE_64, PARSED) # flushSseAfter: 0 # number of SSE emitted before flushing data to the output stream. 0 means flush after each event +# gzipCompressionLevel: -1 # integer value of gzip compression level. This option is used when user requests data via HTTP with enabled commpression. +# * -1: default compression level +# * 0: no compression +# * 1: best speed +# * 9: best compression pins: # pins are used to communicate with codec components to parse message data - name: to_codec diff --git a/gradle.properties b/gradle.properties index 303d2b64..a1773c17 100644 --- a/gradle.properties +++ b/gradle.properties @@ -16,7 +16,7 @@ kotlin.code.style=official -release_version=2.2.1 +release_version=2.3.0 description='th2 Lightweight data provider component' vcs_url=https://github.com/th2-net/th2-lw-data-provider docker_image_name= diff --git a/src/main/kotlin/com/exactpro/th2/lwdataprovider/configuration/Configuration.kt b/src/main/kotlin/com/exactpro/th2/lwdataprovider/configuration/Configuration.kt index 7eadff03..ed80f6ab 100644 --- a/src/main/kotlin/com/exactpro/th2/lwdataprovider/configuration/Configuration.kt +++ b/src/main/kotlin/com/exactpro/th2/lwdataprovider/configuration/Configuration.kt @@ -43,6 +43,7 @@ class CustomConfigurationClass( val listOfMessageAsSingleMessage: Boolean? = null, val useTransportMode: Boolean? = null, val flushSseAfter: Int? = null, + val gzipCompressionLevel: Int? = null, ) class Configuration(customConfiguration: CustomConfigurationClass) { @@ -69,6 +70,7 @@ class Configuration(customConfiguration: CustomConfigurationClass) { val listOfMessageAsSingleMessage: Boolean = VariableBuilder.getVariable(customConfiguration::listOfMessageAsSingleMessage, true) val useTransportMode: Boolean = VariableBuilder.getVariable(customConfiguration::useTransportMode, false) val flushSseAfter: Int = VariableBuilder.getVariable(customConfiguration::flushSseAfter, 0) + val gzipCompressionLevel: Int = VariableBuilder.getVariable(customConfiguration::gzipCompressionLevel, -1) init { require(bufferPerQuery <= maxBufferDecodeQueue) { "buffer per queue ($bufferPerQuery) must be less or equal to the total buffer size ($maxBufferDecodeQueue)" @@ -86,6 +88,9 @@ class Configuration(customConfiguration: CustomConfigurationClass) { require(flushSseAfter >= 0) { "flushSseAfter must be positive integer or zero" } + require(gzipCompressionLevel <= 9 && gzipCompressionLevel >= -1) { + "gzipCompressionLevel must be integer in the [-1, 9] range" + } } } diff --git a/src/main/kotlin/com/exactpro/th2/lwdataprovider/http/HttpServer.kt b/src/main/kotlin/com/exactpro/th2/lwdataprovider/http/HttpServer.kt index 9ad097c8..1492b323 100644 --- a/src/main/kotlin/com/exactpro/th2/lwdataprovider/http/HttpServer.kt +++ b/src/main/kotlin/com/exactpro/th2/lwdataprovider/http/HttpServer.kt @@ -55,7 +55,6 @@ import org.eclipse.jetty.util.compression.CompressionPool import org.eclipse.jetty.util.compression.DeflaterPool import org.eclipse.jetty.util.thread.ThreadPool.SizedThreadPool import java.time.Instant -import java.util.zip.Deflater import kotlin.math.pow class HttpServer(private val context: Context) { @@ -156,7 +155,9 @@ class HttpServer(private val context: Context) { handler.setup(this, javalinContext) } setupExceptionHandlers(this) - jettyServer()?.server()?.insertHandler(createGzipHandler()) + jettyServer()?.server()?.let { server -> + server.insertHandler(createGzipHandler(server, configuration.gzipCompressionLevel)) + } }.start(configuration.hostname, configuration.port) logger.info { "serving on: http://${configuration.hostname}:${configuration.port}" } @@ -261,18 +262,12 @@ class HttpServer(private val context: Context) { } } -private fun configureGzip(server: Server) { - // copied from GzipHandler.doStart - val capacity = server.getBean(SizedThreadPool::class.java)?.maxThreads - ?: CompressionPool.DEFAULT_CAPACITY - - val pool = DeflaterPool(capacity, Deflater.BEST_SPEED, true) - server.addBean(pool, true) - server.insertHandler(createGzipHandler()) -} - -private fun createGzipHandler(): GzipHandler { +private fun createGzipHandler(server: Server, gzipCompressionLevel: Int): GzipHandler { return GzipHandler().apply { + // copied from DeflaterPool.ensurePool method + val capacity = server.getBean(SizedThreadPool::class.java)?.maxThreads ?: CompressionPool.DEFAULT_CAPACITY + + deflaterPool = DeflaterPool(capacity, gzipCompressionLevel, true) setExcludedMimeTypes(*excludedMimeTypes.asSequence() .filter { it != "text/event-stream" } .toList().toTypedArray())