rouzwawi / grpc-kotlin

gRPC with Kotlin Coroutines
https://gitter.im/gRPC-Kotlin/Lobby
Apache License 2.0
218 stars 22 forks source link

gRPC context should be propagated to service code #22

Closed wfhartford closed 5 years ago

wfhartford commented 5 years ago

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.

wfhartford commented 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()) {
}
rouzwawi commented 5 years ago

LGTM!