Tomer Abramovich
02/09/2022, 6:54 AMsuspend fun myLibraryFunction(query : Query) : QueryResult{
val requestContext: RequestContext = currentCoroutineContext()[RequestContext]!! //expecting a dedicated context element
val tenantId = requestContext.tenantId
do something with tenantId
}
suspend callerFunction () {
val requestContext = RequestContext(tenantId)
CoroutineScope(requestContext).async { myLibraryFunction(query) }.await()
}
^^^
This is what i came up with, but it seems redundant to create a new coroutine (i.e async)
i just want the user to be able to set the requestContext on the current context and that the called function will inherit that
something like
suspend fun callerFunction () : QueryResult {
val requestContext = RequestContext(tenantId)
currentCoroutineContext().plus(requestContext) //i know it's immutable so won't work
myLibraryFunction(query)
}
bezrukov
02/09/2022, 6:57 AMwithContext(requestContext) { myLibFun(...) }
Tomer Abramovich
02/09/2022, 7:00 AMbezrukov
02/09/2022, 9:22 AMi assume that async/await also uses a dispatcher behind the scenes though.. is there a better option than this "withContext"?As I said, it's unclear what wrong with dispatcher stuff in your code.
withContext
is a default way to modify context of execution (it takes your current context and adds requestContext to it)
Problem here
It doesn't follow structured concurrency, cancellation of callerFunction won't cancel myLibraryFunction because you called it in completely different context. This context doesn't inherit Job/Dispatcher etc. If you by some reason want to stick with await/async (❗I do not recommend that for this usecase), you need to useCoroutineScope(requestContext).async { myLibraryFunction(query) }.await()
coroutineScope { }
builder:
fun caller() = coroutineScope {
async(newContextElements) { innerFun() }.await()
}
// OR
fun caller() = coroutineScope {
launch(newContextElements) { innerFun() }
}
But again, these are just examples, they are not perfect for your usecase. Best option is
fun caller() = withContext(newContextElements) {
inner()
}
Tomer Abramovich
02/09/2022, 9:43 AM