Skip to content

Commit

Permalink
perf: 使用 cio 替换 netty
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Nov 22, 2023
1 parent e0f48aa commit 17093a8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
10 changes: 5 additions & 5 deletions app/src/main/java/li/songe/gkd/debug/HttpService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) }
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 17093a8

Please sign in to comment.