All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.icerockdev.webserver.NettyEngine.kt Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/*
 * Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
 */

package com.icerockdev.webserver

import io.ktor.config.ApplicationConfig
import io.ktor.server.engine.commandLineEnvironment
import io.ktor.server.engine.loadCommonConfiguration
import io.ktor.server.netty.NettyApplicationEngine
import io.netty.handler.codec.http.HttpObjectDecoder
import io.netty.handler.codec.http.HttpServerCodec

/**
 * Netty engine
 *
 * Idea of configuration from
 * @see io.ktor.server.netty.EngineMain
 */
object NettyEngine {
    private lateinit var engine: NettyApplicationEngine

    fun start(args: Array) {
        val applicationEnvironment = commandLineEnvironment(args)
        engine = NettyApplicationEngine(applicationEnvironment) { loadConfiguration(applicationEnvironment.config) }
        engine.start(wait = false)
    }

    private fun NettyApplicationEngine.Configuration.loadConfiguration(config: ApplicationConfig) {
        val deploymentConfig = config.config("ktor.deployment")
        loadCommonConfiguration(deploymentConfig)
        deploymentConfig.propertyOrNull("requestQueueLimit")?.getString()?.toInt()?.let {
            requestQueueLimit = it
        }
        deploymentConfig.propertyOrNull("shareWorkGroup")?.getString()?.toBoolean()?.let {
            shareWorkGroup = it
        }
        deploymentConfig.propertyOrNull("responseWriteTimeoutSeconds")?.getString()?.toInt()?.let {
            responseWriteTimeoutSeconds = it
        }
        deploymentConfig.propertyOrNull("requestReadTimeoutSeconds")?.getString()?.toInt()?.let {
            requestReadTimeoutSeconds = it
        }
        deploymentConfig.propertyOrNull("runningLimit")?.getString()?.toInt()?.let {
            runningLimit = it
        }
        deploymentConfig.propertyOrNull("tcpKeepAlive")?.getString()?.toBoolean()?.let {
            tcpKeepAlive = it
        }

        if (deploymentConfig.propertyOrNull("httpServer") != null) {
            val httpServerConfig = deploymentConfig.config("httpServer")

            val maxInitialLineLength = httpServerConfig.propertyOrNull("maxInitialLineLength")?.getString()?.toInt()
                ?: HttpObjectDecoder.DEFAULT_MAX_INITIAL_LINE_LENGTH
            val maxHeaderSize = httpServerConfig.propertyOrNull("maxHeaderSize")?.getString()?.toInt()
                ?: HttpObjectDecoder.DEFAULT_MAX_HEADER_SIZE
            val maxChunkSize = httpServerConfig.propertyOrNull("maxChunkSize")?.getString()?.toInt()
                ?: HttpObjectDecoder.DEFAULT_MAX_CHUNK_SIZE

            httpServerCodec = {
                HttpServerCodec(maxInitialLineLength, maxHeaderSize, maxChunkSize)
            }
        }
    }

    fun stop() {
        stop(3000L, 5000L)
    }

    fun stop(gracePeriod: Long, timeout: Long) {
        engine.stop(gracePeriod, timeout)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy