perf: 使用 cio 替换 netty

This commit is contained in:
lisonge 2023-11-22 17:14:02 +08:00
parent e0f48aaa0d
commit 17093a8458
2 changed files with 7 additions and 7 deletions

View File

@ -8,9 +8,9 @@ import io.ktor.http.CacheControl
import io.ktor.serialization.kotlinx.json.json
import io.ktor.server.application.call
import io.ktor.server.application.install
import io.ktor.server.cio.CIO
import io.ktor.server.cio.CIOApplicationEngine
import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
import io.ktor.server.netty.NettyApplicationEngine
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
import io.ktor.server.request.receive
import io.ktor.server.request.receiveText
@ -63,8 +63,8 @@ class HttpService : CompositionService({
)
val httpSubsRawFlow = MutableStateFlow<SubscriptionRaw?>(null)
fun createServer(port: Int): NettyApplicationEngine {
return embeddedServer(Netty, port, configure = { tcpKeepAlive = true }) {
fun createServer(port: Int): CIOApplicationEngine {
return embeddedServer(CIO, port) {
install(KtorCorsPlugin)
install(KtorErrorPlugin)
install(ContentNegotiation) { json(keepNullJson) }
@ -129,7 +129,7 @@ class HttpService : CompositionService({
}
}
var server: NettyApplicationEngine? = null
var server: CIOApplicationEngine? = null
scope.launchTry(Dispatchers.IO) {
storeFlow.map(scope) { s -> s.httpServerPort }.collect { port ->
server?.stop()

View File

@ -136,10 +136,10 @@ dependencyResolutionManagement {
library("junit", "junit:junit:4.13.2")
val ktorVersion = "2.3.5"
val ktorVersion = "2.3.6"
// 请注意,当 client 和 server 版本不一致时, 会报错 socket hang up
library("ktor.server.core", "io.ktor:ktor-server-core:$ktorVersion")
library("ktor.server.netty", "io.ktor:ktor-server-netty:$ktorVersion")
library("ktor.server.cio", "io.ktor:ktor-server-cio:$ktorVersion")
library("ktor.server.cors", "io.ktor:ktor-server-cors:$ktorVersion")
library(
"ktor.server.content.negotiation", "io.ktor:ktor-server-content-negotiation:$ktorVersion"