Also, is `ApacheClient` safe to reuse in a handler...
# http4k
q
Also, is
ApacheClient
safe to reuse in a handler?
s
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
Ok, that's weird because I got a deadlock somewhere calling it
s
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
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
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
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
Correct