-
Notifications
You must be signed in to change notification settings - Fork 166
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
Provide a way to propagate a custom context to CoroutineContext #66
Comments
An alternative instead of a method override would be for the constructor parameter to accept a supplier of contexts instead of context, but the method seems a bit cleaner. The key point here is context should often be scoped to a request, so having a constant parameter passed to the stub itself, which is essentially a Singleton, doesn't allow for modeling request-scoped contexts. |
I don't follow why, if you want context scoped to a request, you would not actively prefer to just write
...which seems like a fairly standard approach. I don't follow your use case yet, as to why that wouldn't be appropriate here? |
That technically works but is a lot of boilerplate if it has to happen for every method - we have created an appropriate context in our framework and would like to use it in a generic way. Otherwise we add this same code to every service method.
With Java we'd probably use interceptors instead for something similar but unfortunately a Java interceptor can't work with coroutines. |
If the boilerplate is getting to be a lot, you can factor it out:
and then just write It makes sense that you might want this behavior for this case where you have a context you're generating the same way for every RPC method, but I'm not certain that's actually a common thing, and I feel like it'd seem strange to any project that didn't have that precise practice? One possibility might be to look into adding Kotlin-specific interceptors which would be more general, and could let you intercept the coroutines appropriately. |
Does this mean users (not an RPC framework but those who write business logic) have to wrap their logic with |
Technically, it is hard to use That means coroutine stub should run under the context-aware executor that is request-scoped, so could not be created by the constructor. If gRPC Kotlin supports Kotlin-specific interceptors, that sounds good to me. 😀 |
We can not use decorate all RPC methods with
|
I think this is not a rare case. It would be happy for them if they can write like the following: // https://github.com/Kotlin/kotlinx.coroutines/tree/master/integration/kotlinx-coroutines-slf4j
import kotlinx.coroutines.slf4j.MDCContext
class XxxService: XxxCoroutineImplBase() {
override val context: CoroutineContext
get() = MDCContext(requestAwareMdc())
// gets a logging context from thread local or gRPC context
private fun requestAwareMdc() = ...
...
} |
Flows would certainly have to use To organize my thoughts: I remain surprised that the
...in short, a singleton, which works just fine with gRPC-Kotlin's current API. If these maps tended to be large and rebuilding them was expensive -- or for any other reason -- you might instead create an interceptor which did the work once and stored it in the gRPC Context, and then your singleton would get them pre-made from the gRPC context and install them. |
One possible API: a gRPC Context element to store a
|
Using suggested |
This approach is pretty common I'd think. |
I'm moving forward on a design for an interceptor-based solution, where interceptors can inject |
Merged a mechanism to introduce |
Thanks for your consideration and solve it. 🙇♂️ |
Currently, CoroutineContext could be customized with
AbstractCoroutineServerImpl
constructor.grpc-kotlin/stub/src/main/java/io/grpc/kotlin/AbstractCoroutineServerImpl.kt
Line 29 in 2f3d86b
And it is combined with
GrpcContextElement
that captures a context from the current thread and propagates it.grpc-kotlin/stub/src/main/java/io/grpc/kotlin/ServerCalls.kt
Line 199 in 2f3d86b
However, a
CoroutinContext
injected by users could not propagate any context from the current thread, because it was instantiated when the Coroutine stub is created.We can use a hack that was suggested by @anuraaga, line/armeria#2669 (comment)
We don't think this is a nice approach. If
AbstractCoroutineServerImpl
getCoroutineContext
from a method then constructor parameter, gRPC-Kotlin users can easily propagate their context to CoroutineContext.Do you think this change makes sense, I could make a PR. 😀
/cc @anuraaga
The text was updated successfully, but these errors were encountered: