Kotlin SDK every requests block the current thread

Hello,

We used to use Reactive Java SDK for our Kotlin applications to gain non-blocking io operations(we made some operations on it to support native Kotlin coroutines). In Kotlin SDK as I see, there are no operations like “suspendCoroutine” or “suspendCancellableCoroutine” to prevent the thread from being idle.
If every operation suspends the current coroutine(release the current thread) and continues when the io operation is done, It would be great for us to scale perspective.

Thanks.

Hi Mehmet,

Welcome to the forum!

If every operation suspends the current coroutine(release the current thread) and continues when the io operation is done, It would be great for us to scale perspective.

The methods in the Kotlin SDK that do IO are all suspend functions, and do indeed release the current thread until the operation completes.

The Kotlin SDK uses the same asynchronous core library as the Java SDK. The core library uses Reactor for efficient async IO. The Kotlin SDK uses kotlinx-coroutines-jdk8 and kotlinx-coroutines-reactive to suspend the current coroutine while the IO completes.

For example, let’s look at this code for executing a KV request. The core library starts doing the IO asynchronously when send(request) is called. The request.response() method returns a CompletableFuture that completes when the IO is done. The CompletableFuture.await() extension function from kotlinx-coroutines-jdk8 suspends the current coroutine until the IO is complete and the response is ready.

tldr; The Kotlin SDK doesn’t call suspendCoroutine or suspendCancellableCoroutine because the core library is already asynchronous.

Thanks,
David

Hello David,

Thank you very much for your detailed explanation. I totally missed the await method in library code, as you said it also suspends the current coroutine.

Thanks.