Jacob Schwartz
03/28/2024, 7:52 PMGlobalScope.launch
when launching the request coroutine and it seems the parent context is lost.
Is anyone aware of any project/plugin that does this? Or of a way to get the parent job's context (the parent job does seem to be maintained). Or if I'm barking up the completely wrong tree?Aleksei Tirman [JB]
03/28/2024, 8:00 PMJacob Schwartz
03/28/2024, 8:35 PMAleksei Tirman [JB]
03/28/2024, 9:22 PMJacob Schwartz
03/28/2024, 9:23 PMJacob Schwartz
03/28/2024, 9:36 PMAleksei Tirman [JB]
03/28/2024, 9:41 PMHttpRequestBuilder
's block for each request. Here is an example:
fun main() {
val client = HttpClient(CIO)
embeddedServer(Netty, port = 3333) {
routing {
get("/a") {
client.get("<https://example.com>") {
addStandardHeaders(call.request)
}
}
get("/b") {
<http://client.post|client.post>("<https://example.com>") {
addStandardHeaders(call.request)
}
}
}
}.start(wait = true)
}
fun HttpRequestBuilder.addStandardHeaders(request: ApplicationRequest) {
if (request.headers.contains("custom")) {
headers.append("custom", request.headers["custom"]!!)
}
}
Aleksei Tirman [JB]
03/28/2024, 9:42 PMJacob Schwartz
03/28/2024, 9:48 PMhttpClient.get()
will need to directly invoke addStandardHeaders and the callers will need to have access to the call.
I'm looking for a way to handle this more transparently by using something like the BaseApplicationPlugin and the client hooks.Jacob Schwartz
03/28/2024, 9:50 PMx-b3-traceid
is forwarded from ingress to egress requests without the caller needing to manually pass the fields around. I'm not sure there's something like that available for ktorJacob Schwartz
04/02/2024, 4:51 PMCLOVIS
04/09/2024, 5:14 AMJacob Schwartz
04/09/2024, 5:08 PMsuspend inline
functions, where for some reason the coroutine not maintained and a new Job is used. Turns out we also had some folks misunderstand that routes are already processed as suspend functions so they were adding runBlocking
to some calls.
I'm investigating this further as a potential solution, but it is very fragile.CLOVIS
04/10/2024, 4:54 AMrunBlocking
calls… especially if you're using ktor, there are very few legitimate reasons to have one, and they will cause this kind of problemsCLOVIS
04/10/2024, 4:55 AMAs it turns out, the coroutine context is maintained except in cases where there are calls intoThis seems very strange to me. There is probably something else going on than justfunctions, where for some reason the coroutine not maintained and a new Job is used.suspend inline
suspend inline
.Jacob Schwartz
04/10/2024, 5:51 PMI recommend going through your entire codebase and removing allYes, that's the plan. We had some folks unfamiliar with Ktor (and really Kotlin) working on these services for a whilerunBlocking
Jacob Schwartz
04/10/2024, 5:52 PMThis seems very strange to me. There is probably something else going on than justIt was the only thing I changed for that function to see it start maintaining the context.suspend inline
CLOVIS
04/11/2024, 3:46 AMJacob Schwartz
04/11/2024, 3:58 PM