Skip to content

Commit 9e10451

Browse files
committed
upgrade to Ktor 3
1 parent ed8dd3e commit 9e10451

File tree

6 files changed

+22
-28
lines changed

6 files changed

+22
-28
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ ktor-http-cio = { module = "io.ktor:ktor-http-cio", version.ref = "ktor-version"
9292
ktor-utils = { module = "io.ktor:ktor-utils", version.ref = "ktor-version" }
9393
ktor-io = { module = "io.ktor:ktor-io", version.ref = "ktor-version" }
9494
ktor-server-netty = { module = "io.ktor:ktor-server-netty", version.ref = "ktor-version" }
95-
ktor-server-jetty = { module = "io.ktor:ktor-server-jetty", version.ref = "ktor-version" }
95+
ktor-server-jetty-jakarta = { module = "io.ktor:ktor-server-jetty-jakarta", version.ref = "ktor-version" }
9696
ktor-server-cio = { module = "io.ktor:ktor-server-cio", version.ref = "ktor-version" }
9797
ktor-network-tls-certificates = { module = "io.ktor:ktor-network-tls-certificates", version.ref = "ktor-version" }
9898

runtime/protocol/http-client-engines/http-client-engine-okhttp/jvm/src/aws/smithy/kotlin/runtime/http/engine/okhttp/ConnectionIdleMonitor.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
package aws.smithy.kotlin.runtime.http.engine.okhttp
66

7-
import aws.smithy.kotlin.runtime.telemetry.logging.Logger
87
import aws.smithy.kotlin.runtime.telemetry.logging.logger
98
import kotlinx.coroutines.CoroutineName
109
import kotlinx.coroutines.CoroutineScope
@@ -63,19 +62,20 @@ internal class ConnectionIdleMonitor(val pollInterval: Duration) : ConnectionLis
6362
val connId = System.identityHashCode(connection)
6463
val context = call.callContext()
6564
val scope = CoroutineScope(context)
66-
val logger = context.logger<ConnectionIdleMonitor>()
6765
val monitor = scope.launch(CoroutineName("okhttp-conn-monitor-for-$connId")) {
68-
doMonitor(connection, logger)
66+
doMonitor(connection)
6967
}
70-
logger.trace { "Launched coroutine $monitor to monitor $connection" }
68+
context.logger<ConnectionIdleMonitor>().trace { "Launched coroutine $monitor to monitor $connection" }
7169

7270
// Non-locking map access is okay here because this code will only execute synchronously as part of a
7371
// `connectionReleased` event and will be complete before any future `connectionAcquired` event could fire for
7472
// the same connection.
7573
monitors[connection] = monitor
7674
}
7775

78-
private suspend fun doMonitor(conn: Connection, logger: Logger) {
76+
private suspend fun doMonitor(conn: Connection) {
77+
val logger = coroutineContext.logger<ConnectionIdleMonitor>()
78+
7979
val socket = conn.socket()
8080
val source = try {
8181
socket.source()
@@ -93,16 +93,20 @@ internal class ConnectionIdleMonitor(val pollInterval: Duration) : ConnectionLis
9393

9494
while (coroutineContext.isActive) {
9595
try {
96+
logger.trace { "Polling socket for $conn" }
9697
source.readByte() // Blocking read; will take up to READ_TIMEOUT_MS to complete
9798
} catch (_: SocketTimeoutException) {
99+
logger.trace { "Socket still alive for $conn" }
98100
// Socket is still alive
99101
} catch (_: EOFException) {
100-
logger.trace { "Socket for $conn was closed remotely" }
102+
logger.trace { "Socket closed remotely for $conn" }
101103
socket.closeQuietly()
102104
resetTimeout = false
103105
return
104106
}
105107
}
108+
109+
logger.trace { "Monitoring coroutine has been cancelled. Ending polling loop." }
106110
} catch (e: Throwable) {
107111
logger.warn(e) { "Failed to poll $conn. Ending polling loop. Connection may be unstable now." }
108112
} finally {

runtime/protocol/http-client-engines/test-suite/build.gradle.kts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ kotlin {
2424

2525
jvmMain {
2626
dependencies {
27-
implementation(libs.ktor.server.jetty)
27+
implementation(libs.ktor.server.jetty.jakarta)
2828
implementation(libs.ktor.network.tls.certificates)
2929

3030
implementation(project(":runtime:protocol:http-client-engines:http-client-engine-default"))
@@ -52,6 +52,8 @@ kotlin {
5252
implementation("org.bouncycastle:bcpkix-jdk18on:1.78") // https://github.com/docker-java/docker-java/pull/2326
5353

5454
implementation(libs.docker.transport.zerodep)
55+
56+
implementation(project(":runtime:observability:telemetry-defaults"))
5557
}
5658
}
5759

runtime/protocol/http-client-engines/test-suite/jvm/src/aws/smithy/kotlin/runtime/http/test/suite/Connections.kt

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,16 @@
55
package aws.smithy.kotlin.runtime.http.test.suite
66

77
import io.ktor.server.application.Application
8-
import io.ktor.server.application.call
9-
import io.ktor.server.jetty.JettyApplicationCall
108
import io.ktor.server.response.respondText
11-
import io.ktor.server.routing.RoutingApplicationCall
129
import io.ktor.server.routing.post
1310
import io.ktor.server.routing.route
1411
import io.ktor.server.routing.routing
15-
import io.ktor.util.InternalAPI
16-
import io.ktor.utils.io.close
17-
import kotlinx.coroutines.delay
18-
import kotlinx.coroutines.launch
19-
import kotlin.time.Duration.Companion.seconds
2012

2113
internal fun Application.connectionTests() {
2214
routing {
2315
route("connectionDrop") {
24-
@OptIn(InternalAPI::class)
2516
post {
26-
val routingCall = call as RoutingApplicationCall
27-
val jettyCall = routingCall.engineCall as JettyApplicationCall
28-
29-
launch {
30-
delay(4.seconds) // Close the connection ~4 seconds after the call ends
31-
jettyCall.response.responseChannel().close()
32-
}
33-
34-
jettyCall.respondText("Bar")
17+
call.respondText("Bar")
3518
}
3619
}
3720
}

runtime/protocol/http-client-engines/test-suite/jvm/src/aws/smithy/kotlin/runtime/http/test/util/TestServers.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ import aws.smithy.kotlin.runtime.http.test.suite.uploadTests
1313
import io.ktor.server.application.*
1414
import io.ktor.server.engine.*
1515
import io.ktor.server.jetty.*
16+
import io.ktor.server.jetty.jakarta.Jetty
17+
import io.ktor.server.jetty.jakarta.JettyApplicationEngineBase
1618
import redirectTests
1719
import java.io.Closeable
1820
import java.nio.file.Paths
1921
import java.util.concurrent.TimeUnit
22+
import kotlin.time.Duration.Companion.seconds
2023

2124
private data class TestServer(
2225
val port: Int,
@@ -94,7 +97,7 @@ private fun tlsServer(instance: TestServer, sslConfig: SslConfig): EmbeddedServe
9497
val rootConfig = serverConfig {
9598
module(instance.initializer)
9699
}
97-
val engineConfig: ApplicationEngine.Configuration.() -> Unit = {
100+
val engineConfig: JettyApplicationEngineBase.Configuration.() -> Unit = {
98101
when (instance.type) {
99102
ConnectorType.HTTP -> connector { port = instance.port }
100103

@@ -109,6 +112,8 @@ private fun tlsServer(instance: TestServer, sslConfig: SslConfig): EmbeddedServe
109112
enabledProtocols = instance.protocolName?.let(::listOf)
110113
}
111114
}
115+
116+
idleTimeout = 3.seconds // Required for ConnectionTest.testShortLivedConnections
112117
}
113118

114119
return try {

runtime/protocol/http-client-engines/test-suite/jvm/test/aws/smithy/kotlin/runtime/http/test/ConnectionTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class ConnectionTest : AbstractEngineTest() {
109109
val initialResp = initialCall.response.body.toByteStream()?.decodeToString()
110110
assertEquals("Bar", initialResp)
111111

112-
delay(5.seconds) // Shorter than the client-side timeout
112+
delay(5.seconds) // Longer than the service side timeout, shorter than the client-side timeout
113113

114114
val subsequentReq = HttpRequest {
115115
testSetup()

0 commit comments

Comments
 (0)