NettyEpollServerAdapter.kt

  1. package com.hexagonkt.http.server.netty.epoll

  2. import com.hexagonkt.core.Jvm
  3. import com.hexagonkt.http.server.HttpServerPort
  4. import com.hexagonkt.http.server.netty.NettyServerAdapter
  5. import io.netty.bootstrap.ServerBootstrap
  6. import io.netty.channel.Channel
  7. import io.netty.channel.ChannelOption
  8. import io.netty.channel.MultithreadEventLoopGroup
  9. import io.netty.channel.epoll.EpollChannelOption
  10. import io.netty.channel.epoll.EpollEventLoopGroup
  11. import io.netty.channel.epoll.EpollServerSocketChannel

  12. /**
  13.  * Implements [HttpServerPort] using Netty Epoll [Channel].
  14.  */
  15. class NettyEpollServerAdapter(
  16.     bossGroupThreads: Int = 1,
  17.     workerGroupThreads: Int = 0,
  18.     executorThreads: Int = Jvm.cpuCount * 2,
  19.     private val soBacklog: Int = 4 * 1_024,
  20.     private val soReuseAddr: Boolean = true,
  21.     private val soKeepAlive: Boolean = true,
  22.     shutdownQuietSeconds: Long = 0,
  23.     shutdownTimeoutSeconds: Long = 0,
  24.     keepAliveHandler: Boolean = true,
  25.     httpAggregatorHandler: Boolean = true,
  26.     chunkedHandler: Boolean = true,
  27.     enableWebsockets: Boolean = true,
  28. ) : NettyServerAdapter(
  29.     bossGroupThreads,
  30.     workerGroupThreads,
  31.     executorThreads,
  32.     soBacklog,
  33.     soReuseAddr,
  34.     soKeepAlive,
  35.     shutdownQuietSeconds,
  36.     shutdownTimeoutSeconds,
  37.     keepAliveHandler,
  38.     httpAggregatorHandler,
  39.     chunkedHandler,
  40.     enableWebsockets,
  41. ) {

  42.     constructor() :
  43.         this(1, 0, Jvm.cpuCount * 2, 4 * 1_024, true, true, 0, 0, true, true, true, true)

  44.     override fun groupSupplier(it: Int): MultithreadEventLoopGroup =
  45.         EpollEventLoopGroup(it)

  46.     override fun serverBootstrapSupplier(
  47.         bossGroup: MultithreadEventLoopGroup,
  48.         workerGroup: MultithreadEventLoopGroup,
  49.     ): ServerBootstrap =
  50.         ServerBootstrap().group(bossGroup, workerGroup)
  51.             .channel(EpollServerSocketChannel::class.java)
  52.             .option(EpollChannelOption.SO_REUSEPORT, true)
  53.             .option(ChannelOption.SO_BACKLOG, soBacklog)
  54.             .option(ChannelOption.SO_REUSEADDR, soReuseAddr)
  55.             .childOption(ChannelOption.SO_KEEPALIVE, soKeepAlive)
  56.             .childOption(ChannelOption.SO_REUSEADDR, soReuseAddr)
  57. }