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

Quy D X Nguyen

05/31/2020, 4:32 PM
Also, is
ApacheClient
safe to reuse in a handler?
s

s4nchez

05/31/2020, 4:36 PM
If you mean having a single instance of the client that is used across multiple requests by one or more handlers, then yes it is.
q

Quy D X Nguyen

05/31/2020, 4:45 PM
Ok, that's weird because I got a deadlock somewhere calling it
s

s4nchez

05/31/2020, 4:47 PM
If you can provide an example we can help troubleshooting. I’ve never seen deadlocks, but running out of threads in the client pool (default is 2) is relatively common
q

Quy D X Nguyen

05/31/2020, 4:50 PM
Oh that's probably it. I only get it when I refresh a bunch of times.
Do I have to close the response I get from the Client?
Copy code
fun main() {
    val client = ApacheClient(responseBodyMode = BodyMode.Stream, client = HttpClients.custom()
            .setDefaultRequestConfig(RequestConfig.custom()
                    .setCookieSpec(CookieSpecs.IGNORE_COOKIES)
                    .build()).build())

    val app = { request: Request ->
        val query = Request(Method.GET, "<http://google.com>${request.uri}")
        val response = client(query)
        println(response.status)
        Response(response.status)
    }


    app.asServer(Netty(8081)).start().block()
}
Here's an example. F5-ing a lot will cause the server to be unresponsive.
s

s4nchez

05/31/2020, 5:09 PM
You’re not using the response body you get from the client. That’s why it’s not releasing the connection
You normally don’t need to close it, but it expects you to use it ;)
q

Quy D X Nguyen

05/31/2020, 5:49 PM
Copy code
Response(Status.OK).header("Content-Length", length.header("Content-Type", type).body(response.body.stream, length.toLong())
So I don't need to close this because I'm using it
But I do need to close the other one
s

s4nchez

05/31/2020, 5:50 PM
Correct
3 Views