Closed wfhartford closed 5 years ago
One aspect I'm not completely sure about on this PR is weather or not the coroutineContext
field of the ...ImplBase
class should also be passed to launch
. The new ContextCoroutineContextElement
can be combined with the coroutineContext
as follows:
launch (coroutineContext + ContextCoroutineContextElement()) {
}
LGTM!
gRPC uses a io.grpc.Context class to make metadata about a call available to implementation code. The context of a call is stored in a ThreadLocal allowing it to be easily accessed within the service method call. Kotlin coroutines do not run on a single, predictable thread, making this storage insufficient. Fortunately, Kotlin provides an easy way to adapt this thread local pattern to a CoroutineContext, which is implemented in this commit.