question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide a better way to modify `CoroutineContext` in `RequestContext`

See original GitHub issue

Just an idea.

Currently, Armeria provides following ways to get & set CoroutineContext that will later be used to run a coroutine-based AnnotatedService:

  • CoroutineContexts#get, CoroutineContexts#set
  • CoroutineContextService with CoroutineContextProvider

…but I find CoroutineContextService and CoroutineContextProvider a bit redundant as what can be done with them can also be done with CoroutineContexts#get/set while the opposite is not true.

And non of those APIs provide a convenient way to combine(+) a new CoroutineContext with the one added earlier as CoroutineContexts#set just overwrites the whole CoroutineContext with the new one.

We may want to provide cleaner APIs to get/set CoroutineContext in a RequestContext by adding following extension functions & property, if there’s no good reason to write those APIs in Java.

fun RequestContext.addCoroutineContext(ctx: CoroutineContext): Unit

fun RequestContext.removeCoroutineContext(ctx: CoroutineContext): Unit

var RequestContext.coroutineContext: CoroutineContext?
    get() = ...
    set(value) = ...

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ks-yimcommented, Oct 28, 2022

@ikhoon The approach you’ve suggested will prevent unnecessary confusion, thanks! I will send a PR to add a new method to CoroutineContexts.

1reaction
minwooxcommented, Oct 11, 2022

We often just do the following in an Authorizer<HttpRequest> to add authenticated user context to the coroutine

Yeah, if there’s another decorator that we can set a CoroutineContext, we don’t need the CoroutineContextService decorator. But we do need CoroutineContextService if there’s no decorator like this example: https://github.com/line/armeria/blob/master/examples/annotated-http-service-kotlin/src/main/kotlin/example/armeria/server/annotated/kotlin/DecoratingService.kt#L53-L55

but I am suggesting the same functionality can be provided with more kotlin-friend APIs by leveraging kotlin’s extension function & property (refer to the code written in my initial comment).

I agree with you on this. 👍 What I meant was that we need a way to set/add a CoroutinContext to RequestContext in a decorator. It doesn’t have to be CoroutineContexts#get/set if there are better ones. And the design looks nice. Are you sending a PR for that? 😆

More specifically, let’s say we want to store 1) AuthN/Z context and 2) DB’s shard-routine context within 2 different decorators

That’s a possible scenario. 👍

this wouldn’t be an issue if I stick strictly to CoroutineContextService & CoroutineContextProvider approach, but then

We didn’t have to stick to CoroutineContextService and CoroutineContextProvider from the first. 😆 It’s just a way to set the CoroutineContext. We can use another decorator and set CoroutineContext directly in it.

/cc @okue

Read more comments on GitHub >

github_iconTop Results From Across the Web

Provide a way to propagate a custom context to ... - GitHub
Currently, CoroutineContext could be customized with AbstractCoroutineServerImpl constructor.
Read more >
Different options for using Coroutine Scope/Context?
I am currently developing a backend endpoint that will do the following: take a request; save the request context to a database; launch...
Read more >
Emulating request scoped objects with Kotlin Coroutines
The preferred way to store values that needs to be available for the duration of a Kotlin coroutine is to extend the current...
Read more >
Isolating global variables with a coroutine context manager in ...
To setup an easy solution to this global variable problem, we can use the coroutine context to switch between and isolate global variables ......
Read more >
Pass web request context in Ktor : r/Kotlin - Reddit
In Go it is possible to pass the context of a web request to functions and go routines. This context may contain data...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found