Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to configure Netty http compression #4056

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import io.netty.channel.kqueue.{KQueue, KQueueEventLoopGroup, KQueueServerSocket
import io.netty.channel.nio.NioEventLoopGroup
import io.netty.channel.socket.nio.NioServerSocketChannel
import io.netty.channel.{ChannelHandler, ChannelPipeline, EventLoopGroup, ServerChannel}
import io.netty.handler.codec.http.HttpServerCodec
import io.netty.handler.codec.http.{HttpContentCompressor, HttpServerCodec}
import io.netty.handler.logging.LoggingHandler
import io.netty.handler.ssl.SslContext
import org.playframework.netty.http.HttpStreamsServerHandler
Expand Down Expand Up @@ -50,6 +50,13 @@ import scala.concurrent.duration._
*
* @param serverHeader
* If set, send this value in the 'Server' response header. If None, don't set the header.
*
* @param compressionEnabled
* If set to true, the server will compress responses.
*
* @param compressionContentSizeThreshold
* The minimum size of the response content that will be compressed. If the response content is smaller than this value, it will not be
* compressed.
*/
case class NettyConfig(
host: String,
Expand All @@ -68,7 +75,9 @@ case class NettyConfig(
initPipeline: NettyConfig => (ChannelPipeline, ChannelHandler) => Unit,
gracefulShutdownTimeout: Option[FiniteDuration],
idleTimeout: Option[FiniteDuration],
serverHeader: Option[String]
serverHeader: Option[String],
compressionEnabled: Boolean,
compressionContentSizeThreshold: Int
) {
def host(h: String): NettyConfig = copy(host = h)

Expand Down Expand Up @@ -109,6 +118,9 @@ case class NettyConfig(

def serverHeader(h: String): NettyConfig = copy(serverHeader = Some(h))

def withCompressionEnabled: NettyConfig = copy(compressionEnabled = true)
def compressionContentSizeThreshold(t: Int): NettyConfig = copy(compressionContentSizeThreshold = t)

def isSsl: Boolean = sslContext.isDefined
}

Expand All @@ -131,12 +143,15 @@ object NettyConfig {
eventLoopConfig = EventLoopConfig.auto,
socketConfig = NettySocketConfig.default,
initPipeline = cfg => defaultInitPipeline(cfg)(_, _),
serverHeader = Some(s"tapir/${buildinfo.BuildInfo.version}")
serverHeader = Some(s"tapir/${buildinfo.BuildInfo.version}"),
compressionEnabled = false,
compressionContentSizeThreshold = 0
)

def defaultInitPipeline(cfg: NettyConfig)(pipeline: ChannelPipeline, handler: ChannelHandler): Unit = {
cfg.sslContext.foreach(s => pipeline.addLast(s.newHandler(pipeline.channel().alloc())))
pipeline.addLast(ServerCodecHandlerName, new HttpServerCodec())
if (cfg.compressionEnabled) pipeline.addLast(new HttpContentCompressor(cfg.compressionContentSizeThreshold, Seq.empty: _*))
pipeline.addLast(new HttpStreamsServerHandler())
pipeline.addLast(handler)
if (cfg.addLoggingHandler) pipeline.addLast(new LoggingHandler())
Expand Down
Loading