Skip to content

Commit

Permalink
Th2 5090 added gzipCompressionLevel option (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-Smirnov-Exactpro authored Sep 29, 2023
1 parent 0cae5ef commit 504dc15
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)"
Expand All @@ -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"
}
}
}

Expand Down
21 changes: 8 additions & 13 deletions src/main/kotlin/com/exactpro/th2/lwdataprovider/http/HttpServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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}" }
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 504dc15

Please sign in to comment.