https://kotlinlang.org logo
#http4k
Title
# http4k
j

janvladimirmostert

11/28/2020, 9:42 AM
http4k looks very interesting, I like how it focuses on just http instead of trying to do everything few questions, when you do
Copy code
val app = { request: Request ->

}
val server = app.asServer(Netty(9000)).start()
is the inside of that lambda a single thread or using some kind of event loop or is that server dependent? when load testing (with
Netty(9000)
), I'm seeing nioEventLoopGroup-2-1 and nioEventLoopGroup-3-1..24, I'm assuming those 25 threads are from Netty? how would one execute suspendable functions (or even blocking functions) inside that lambda?
Copy code
val app = { request: Request ->
  	GlobalScope.launch {
		suspendableFunctionThatReturnsResponse()
	}
}
val server = app.asServer(Netty(9000)).start() // obviously doesn't compile since that app lambda doesn't return a Response
s

s4nchez

11/28/2020, 11:14 AM
The lambda is just a plain Kotlin function. Its execution will depend on the context (i.e. backend where it runs).
👍 1
We currently don't support coroutines. See this GitHub issue for the discussion around that topic.
j

janvladimirmostert

11/28/2020, 11:16 AM
I saw that issue, just wondering if it can be retrofitted, something like this maybe
Copy code
val app = { request: Request ->
			runBlocking {
				GlobalScope.async {
					delay(1000)
					Response(OK).body("DONE!!")
				}.await()
			}
s

s4nchez

11/28/2020, 11:20 AM
In your example,
app
is an
HttpHandler
so it should work fine.
For making the actual
HttpHandler
suspendable, on the other hand, would cause all sort of issues with both clients and server backends, and we just haven't seen a good reason to do it yet.
j

janvladimirmostert

11/28/2020, 11:24 AM
Once you start making things suspendable, it spreads and then everything all of a sudden needs to be suspendable I like the simplicity of htt4k, if somebody wants to use CompletableFutures, this framework doesn't dictate that they have to use suspendable functions I'm by no means an expert on Coroutines or Multithreading, would be nice to have a short section in the docs on what the recommended way is to work with either, maybe something for the backlog. Once I've gaine enough experience with Coroutines, I'd be happy to write such a section
👍 1
r

Razvan

12/02/2020, 6:57 PM
Not sure at this point it worth the hustle to move to coroutines. Project Loom will make it by default async so just wait for it will be easier... meanwhile I also sometimes miss the support especially when I want to run background tasks
57 Views