Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KTOR-7669 Add SSE plugin support in Jetty engine #4449

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package io.ktor.client.engine.jetty.jakarta

import io.ktor.client.engine.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.sse.*
import io.ktor.client.request.*
import io.ktor.util.*
import io.ktor.utils.io.*
Expand All @@ -17,7 +18,7 @@ internal class JettyHttp2Engine(
override val config: JettyEngineConfig
) : HttpClientEngineBase("ktor-jetty") {

override val supportedCapabilities = setOf(HttpTimeoutCapability)
override val supportedCapabilities = setOf(HttpTimeoutCapability, SSECapability)

/**
* Cache that keeps least recently used [HTTP2Client] instances. Set "0" to avoid caching.
Expand All @@ -28,7 +29,7 @@ internal class JettyHttp2Engine(
val callContext = callContext()
val jettyClient = getOrCreateClient(data)

return data.executeRequest(jettyClient, config, callContext)
return data.executeRequest(data, jettyClient, config, callContext)
}

/** Only for tests */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import java.net.*
import java.nio.*
import kotlin.coroutines.*

@OptIn(InternalAPI::class)
internal suspend fun HttpRequestData.executeRequest(
data: HttpRequestData,
client: HTTP2Client,
config: JettyEngineConfig,
callContext: CoroutineContext
Expand All @@ -47,13 +49,16 @@ internal suspend fun HttpRequestData.executeRequest(
sendRequestBody(jettyRequest, body, callContext)

val (status, headers) = responseListener.awaitHeaders()
val responseBody: Any = data.attributes.getOrNull(ResponseAdapterAttributeKey)
?.adapt(data, status, headers, responseChannel, data.body, callContext)
?: responseChannel

return HttpResponseData(
status,
requestTime,
headers,
HttpProtocolVersion.HTTP_2_0,
responseChannel,
responseBody,
callContext
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package io.ktor.client.engine.jetty

import io.ktor.client.engine.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.sse.*
import io.ktor.client.request.*
import io.ktor.util.*
import io.ktor.utils.io.*
Expand All @@ -17,7 +18,7 @@ internal class JettyHttp2Engine(
override val config: JettyEngineConfig
) : HttpClientEngineBase("ktor-jetty") {

override val supportedCapabilities = setOf(HttpTimeoutCapability)
override val supportedCapabilities = setOf(HttpTimeoutCapability, SSECapability)

/**
* Cache that keeps least recently used [HTTP2Client] instances. Set "0" to avoid caching.
Expand All @@ -28,7 +29,7 @@ internal class JettyHttp2Engine(
val callContext = callContext()
val jettyClient = getOrCreateClient(data)

return data.executeRequest(jettyClient, config, callContext)
return data.executeRequest(data, jettyClient, config, callContext)
}

/** Only for tests */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import java.net.*
import java.nio.*
import kotlin.coroutines.*

@OptIn(InternalAPI::class)
internal suspend fun HttpRequestData.executeRequest(
data: HttpRequestData,
client: HTTP2Client,
config: JettyEngineConfig,
callContext: CoroutineContext
Expand All @@ -47,13 +49,16 @@ internal suspend fun HttpRequestData.executeRequest(
sendRequestBody(jettyRequest, body, callContext)

val (status, headers) = responseListener.awaitHeaders()
val responseBody: Any = data.attributes.getOrNull(ResponseAdapterAttributeKey)
?.adapt(data, status, headers, responseChannel, data.body, callContext)
?: responseChannel

return HttpResponseData(
status,
requestTime,
headers,
HttpProtocolVersion.HTTP_2_0,
responseChannel,
responseBody,
callContext
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,8 @@ class ServerSentEventsTest : ClientLoader(timeoutSeconds = 120) {
}

test { client ->
println(client.engine)
throw IllegalArgumentException()
client.sse({
url("$TEST_SERVER/sse")
method = HttpMethod.Post
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ actual abstract class ClientLoader actual constructor(val timeoutSeconds: Int) {
block: suspend TestClientBuilder<HttpClientEngineConfig>.() -> Unit
) {
DebugProbes.install()
println("ENGINES: $engines")
for (engine in engines) {
if (shouldSkip(engine, skipEngines, onlyWithEngine)) {
continue
Expand Down