Hi! I'm pretty knew to Kotlin coroutines and gRPC, my server code is generated with
grpc/grpc-kotlin and I struggle a bit to understand how I can even use
the first coroutine explained in Kotlin's documentation in my gRPC server.
Say I have the following server method (
from the Kotlin gRPC Quick start):
private class HelloWorldService : GreeterGrpcKt.GreeterCoroutineImplBase() {
override suspend fun sayHello(request: HelloRequest) = helloReply {}
}
And I want to write the following code at the beginning of this method (
from Kotlin's documentation):
launch {
delay(1000L)
println("World!")
}
println("Hello")
If I simply copy-paste this code in my method,
launch
isn't recognized as a valid method because it should by used on a coroutine scope.
If I wrap my code with a
coroutineScope
like this:
override suspend fun sayHello(request: HelloRequest) = coroutineScope {
launch {
delay(1000L)
println("World!")
}
println("Hello")
helloReply {}
}
It builds, the code is even executed when I make a call from the client, however the server returns an
UNKNOWN
code before the
launch
job can even finish and the
HelloReply
is not used.
So I think I shouldn't create my own coroutine scope and I should use a scope already defined by the server, but I don't find anything related to a "default gRPC coroutine scope"
in the API reference.
I must be missing something really obvious, can someone help me run this really basic code please? 🙏