This question arose after extensive discussion with a coworker,
The issue is the following:
what I was doing before calling a synchronous method inside a suspendCoroutine block with continuation:
private suspend fun <T : Any> request(queryRequest: GraphQLQueryRequest<T>): T? = suspendCoroutine { continuation ->
val response = graphqlClient.executeQuery(queryRequest.query, queryRequest.variables, executor)
val item = response.extractValueAsObject("data.${queryRequest.pathAccessor}", queryRequest.type.java)
continuation.resume(item)
}
He claimed that my code was not preventing the thread from being blocked and the whole process would have to wait my coroutine resume to release the thread.
What he proposed:
private suspend fun <T : Any> request(queryRequest: GraphQLQueryRequest<T>): T? {
val response = graphqlClient.reactiveExecuteQuery(queryRequest.query, queryRequest.variables, executor)
return response.awaitSingle().extractValueAsObject("data.${queryRequest.pathAccessor}", queryRequest.type.java)
}
reactiveExecuteQuery() will return WebFlux Mono, then we call .awaitSingle()
to convert Mono into a suspend function, he claims that this method unlike the first one will not block the thread and everything will work just fine.
according to documentation for suspendCoroutine
Obtains the current continuation instance inside suspend functions and suspends the currently running coroutine.
Does anyone with more insight into Kotlin coroutines know if those solution really provide different results? in terms of blocking of no blocking threads?
If that is not a use case for suspendCoroutine what would it be?
Thank you in advance for any replies :)