Netty-based HTTP server engine for Ktor framework providing high-performance asynchronous server capabilities
—
Primary entry points for creating and configuring Netty-based HTTP servers with comprehensive options for performance tuning and protocol support.
The main factory object for creating NettyApplicationEngine instances with custom configuration.
/**
* ApplicationEngineFactory providing a Netty-based ApplicationEngine
*/
object Netty : ApplicationEngineFactory<NettyApplicationEngine, NettyApplicationEngine.Configuration> {
/**
* Create a configuration instance with the provided configuration block
* @param configure Configuration block to customize engine settings
* @return Configured NettyApplicationEngine.Configuration instance
*/
fun configuration(
configure: NettyApplicationEngine.Configuration.() -> Unit
): NettyApplicationEngine.Configuration
/**
* Create a NettyApplicationEngine instance with the specified parameters
* @param environment Application environment
* @param monitor Event monitoring system
* @param developmentMode Development mode flag
* @param configuration Engine configuration
* @param applicationProvider Function that provides the Ktor Application
* @return NettyApplicationEngine instance
*/
fun create(
environment: ApplicationEnvironment,
monitor: Events,
developmentMode: Boolean,
configuration: NettyApplicationEngine.Configuration,
applicationProvider: () -> Application
): NettyApplicationEngine
}Usage Examples:
import io.ktor.server.engine.*
import io.ktor.server.netty.*
// Basic server creation
val server = embeddedServer(Netty, port = 8080) {
// Application configuration
}
// Server with custom Netty configuration
val server = embeddedServer(Netty, port = 8080) {
// Configure Netty-specific settings
runningLimit = 64
shareWorkGroup = true
responseWriteTimeoutSeconds = 30
tcpKeepAlive = true
enableHttp2 = true
}Comprehensive configuration options for fine-tuning Netty server behavior, performance, and protocol support.
/**
* Configuration for the NettyApplicationEngine
*/
class NettyApplicationEngine.Configuration : BaseApplicationEngine.Configuration() {
/**
* Number of threads for connection group (inherited from BaseApplicationEngine.Configuration)
* Default: 1
*/
var connectionGroupSize: Int
/**
* Number of threads for worker group (inherited from BaseApplicationEngine.Configuration)
* Default: Available processors
*/
var workerGroupSize: Int
/**
* Number of threads for call group (inherited from BaseApplicationEngine.Configuration)
* Default: Available processors
*/
var callGroupSize: Int
/**
* Number of concurrently running requests from the same http pipeline
* Default: 32
*/
var runningLimit: Int
/**
* Do not create separate call event group and reuse worker group for processing calls
* Default: false
*/
var shareWorkGroup: Boolean
/**
* User-provided function to configure Netty's ServerBootstrap
* Default: {} (empty function)
*/
var configureBootstrap: ServerBootstrap.() -> Unit
/**
* Timeout in seconds for sending responses to client
* Default: 10 seconds
*/
var responseWriteTimeoutSeconds: Int
/**
* Timeout in seconds for reading requests from client, "0" is infinite
* Default: 0 (infinite)
*/
var requestReadTimeoutSeconds: Int
/**
* If set to true, enables TCP keep alive for connections so all
* dead client connections will be discarded
* Default: false
*/
var tcpKeepAlive: Boolean
/**
* The url limit including query parameters
* Default: HttpObjectDecoder.DEFAULT_MAX_INITIAL_LINE_LENGTH
*/
var maxInitialLineLength: Int
/**
* The maximum length of all headers.
* If the sum of the length of each header exceeds this value, a TooLongFrameException will be raised
* Default: HttpObjectDecoder.DEFAULT_MAX_HEADER_SIZE
*/
var maxHeaderSize: Int
/**
* The maximum length of the content or each chunk
* Default: HttpObjectDecoder.DEFAULT_MAX_CHUNK_SIZE
*/
var maxChunkSize: Int
/**
* If set to true, enables HTTP/2 protocol for Netty engine
* Default: true
*/
var enableHttp2: Boolean
/**
* User-provided function to configure Netty's HttpServerCodec
* Default: Creates HttpServerCodec with maxInitialLineLength, maxHeaderSize, and maxChunkSize
*/
var httpServerCodec: () -> HttpServerCodec
/**
* User-provided function to configure Netty's ChannelPipeline
* Default: {} (empty function)
*/
var channelPipelineConfig: ChannelPipeline.() -> Unit
}Usage Examples:
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.netty.bootstrap.ServerBootstrap
import io.netty.channel.ChannelOption
// Advanced configuration example
val server = embeddedServer(Netty, port = 8080) {
// Thread pool configuration
connectionGroupSize = 1
workerGroupSize = 16
callGroupSize = 32
// Performance tuning
runningLimit = 128
shareWorkGroup = true
// Timeout configuration
responseWriteTimeoutSeconds = 60
requestReadTimeoutSeconds = 30
// Connection settings
tcpKeepAlive = true
// HTTP protocol limits
maxInitialLineLength = 8192
maxHeaderSize = 16384
maxChunkSize = 8192
// Protocol support
enableHttp2 = true
// Custom bootstrap configuration
configureBootstrap = {
option(ChannelOption.SO_BACKLOG, 1024)
childOption(ChannelOption.SO_KEEPALIVE, true)
}
// Custom pipeline configuration
channelPipelineConfig = {
// Add custom handlers to the pipeline
}
}
// Start the server
server.start(wait = true)Internal extension function for loading configuration from ApplicationConfig (used in EngineMain).
/**
* Load configuration from ApplicationConfig
* @param config ApplicationConfig instance containing deployment settings
*/
internal fun NettyApplicationEngine.Configuration.loadConfiguration(config: ApplicationConfig)This function is used internally by EngineMain to load configuration from application.conf files or command line arguments. It maps configuration properties like:
ktor.deployment.runningLimit → runningLimitktor.deployment.shareWorkGroup → shareWorkGroupktor.deployment.responseWriteTimeoutSeconds → responseWriteTimeoutSecondsktor.deployment.requestReadTimeoutSeconds → requestReadTimeoutSecondsktor.deployment.tcpKeepAlive → tcpKeepAlivektor.deployment.maxInitialLineLength → maxInitialLineLengthktor.deployment.maxHeaderSize → maxHeaderSizektor.deployment.maxChunkSize → maxChunkSizeInstall with Tessl CLI
npx tessl i tessl/maven-io-ktor--ktor-server-netty-jvm