Provide a better way to modify `CoroutineContext` in `RequestContext`
See original GitHub issueJust 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
withCoroutineContextProvider
…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:
- Created a year ago
- Reactions:1
- Comments:6 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@ikhoon The approach you’ve suggested will prevent unnecessary confusion, thanks! I will send a PR to add a new method to
CoroutineContexts
.Yeah, if there’s another decorator that we can set a
CoroutineContext
, we don’t need theCoroutineContextService
decorator. But we do needCoroutineContextService
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-L55I agree with you on this. 👍 What I meant was that we need a way to set/add a
CoroutinContext
toRequestContext
in a decorator. It doesn’t have to beCoroutineContexts#get/set
if there are better ones. And the design looks nice. Are you sending a PR for that? 😆That’s a possible scenario. 👍
We didn’t have to stick to
CoroutineContextService
andCoroutineContextProvider
from the first. 😆 It’s just a way to set theCoroutineContext
. We can use another decorator and setCoroutineContext
directly in it./cc @okue