nilTheDev
10/28/2021, 10:51 AMget("/{...}") {
val startTime = System.currentTimeMillis()
// this is a suspend function
call.respondHandlingLongCalculation(random, delayProvider, startTime)
}
In this code, call.respondHandlingLongCalculation(...)
is a suspend function and it is called without using any launch
block. It seems that there is always a coroutine scope lying around in the ktor application.
Q1. How can I use coroutines in a ktor application? If there is an implicit scope then what are the best practices to use it? Is there any guidelines?
Q2. The scope of this question is little bit broad. How exactly a ktor server handle client connections? Does it launch a whole new instance of the application in a coroutine and it lives until the connection is closed? If not then how it manages concurrent connections? Let's say user A and user B connected to the server simultaneously. Though servers should be as stateless as possible but suppose I made a list of something for serving user A. Something like that,
// this is a global variable
var list: List<Int>? = null
fun ... (){
get("..."){
call.respond(list)
}
}
Is there any chance that user A's data can also get served to user B if they hit the same endpoint?Kamil Kalisz
10/28/2021, 11:42 AMnilTheDev
10/28/2021, 12:29 PMget
or post
endpoint then it won't be shared right? So, even if I invoke an function from get
that has local variables it wouldn't be shared too. Basically I am trying to understand each coroutines, fired for each connection, will maintain its own stack or not.
I'm not sure whether the second question make sense.Kamil Kalisz
10/28/2021, 1:30 PMnilTheDev
10/28/2021, 1:37 PM