Sergey Chikin
01/16/2018, 11:53 AMoverride val route: Routing.() -> Unit
get() = {
get("healthz") {
val dbState = asyncGetDatabaseState()
var res = HealthModel(
status = Server.status,
database = dbState.await(),
redis = RedisHealth("null", 0)
)
call.respond(res)
}
}
suspend fun getDatabaseState(): DatabaseHealth {
delay(500) //sleep for 500ms
return DatabaseHealth(
host = Config.config[DatabaseConfig.host],
database = Config.config[DatabaseConfig.database],
latencyMs = 500
)
}
fun asyncGetDatabaseState() = async {
getDatabaseState()
}
But I keep getting Caused by: io.ktor.server.engine.BaseApplicationResponse$ResponseAlreadySentException: Response has already been sent
even though actual content is sent back to client with HTTP 200. What am I doing wrong?orangy
HealthModel
is being correctly sent, and then something else is attempting to send another respond? We would need more information to help…Sergey Chikin
01/16/2018, 12:07 PMHealthModel
is being correctly sent but in the log I can see the exception. I don't think there's something else trying to respondSergey Chikin
01/16/2018, 12:36 PMBaseApplicationResponse
is intercepting the call after it was responded:
Caused by: io.ktor.server.engine.BaseApplicationResponse$ResponseAlreadySentException: Response has already been sent
at io.ktor.server.engine.BaseApplicationResponse$$special$$inlined$apply$lambda$1.doResume(BaseApplicationResponse.kt:30)
Sergey Chikin
01/16/2018, 2:51 PMnetty
to jetty
helped. It seems like with netty
even this https://github.com/ktorio/ktor/tree/master/ktor-samples/ktor-samples-async is logging the same exception I hadorangy
Sergey Chikin
01/16/2018, 2:52 PMorangy
Sergey Chikin
01/16/2018, 3:07 PM0.9.1-alpha-8
. I'll try to run the sample but at least my code works ok with jetty and throws exception with nettySergey Chikin
01/16/2018, 3:11 PMnetty
. now I'm completely out of ideas what's wrong about my codeorangy
Sergey Chikin
01/16/2018, 3:32 PMsuspend fun testSuspend(): Int {
delay(500)
val random = java.util.Random()
return random.nextInt(100)
}
fun main(args : Array<String>) {
embeddedServer(Netty, 9000) {
install(Routing) {
get("/healthz") {
val start = System.currentTimeMillis()
val calcResult = async { testSuspend() }.await()
val end = System.currentTimeMillis()
call.respond("Runtime ${end - start}ms Result ${calcResult}")
}
}
}.start(wait = true)
}
orangy
Sergey Chikin
01/16/2018, 4:21 PMcall.respondText
works ok thoughorangy
noncom
01/16/2018, 7:16 PMorangy
orangy
orangy
Sergey Chikin
01/16/2018, 9:46 PMcall.respond
to call.respondText
is resolving this problem. but in my case I need it to responding with JSON so need call.respond(Any)
orangy
Sergey Chikin
01/16/2018, 10:01 PMorangy
orangy
Sergey Chikin
01/16/2018, 10:10 PMSergey Chikin
01/16/2018, 10:10 PMSergey Chikin
01/16/2018, 10:24 PMCannot find local variable: name = this
I guess I'm doing something wrongorangy
orangy
Sergey Chikin
01/16/2018, 10:48 PMorangy
Sergey Chikin
01/16/2018, 11:09 PMSergey Chikin
01/16/2018, 11:09 PMorangy
orangy
<http://localhost:9000/healthz>
in browser