Achieving concurrency in Ktor
Looking back at the code we’ve written in this chapter, you may be under the impression that the Ktor code is not concurrent at all. However, this couldn’t be further from the truth.
All Ktor functions used in this chapter are built upon coroutines and the concept of suspending functions. For each incoming request, Ktor initiates a new coroutine to handle it. This functionality is inherently supported by the CIO server engine, which is fundamentally based on coroutines. Ktor’s concurrency model is designed to be efficient yet unobtrusive, a crucial aspect of its architecture.
Furthermore, the routing blocks, which define our endpoints, have access to CoroutineScope
. This allows us to invoke suspending functions within these blocks. An example of such a suspending function is call.respond()
, frequently used in our examples. Suspending functions offer opportunities for context switching and concurrent execution of other code...