Nicholas DiPiazza
Nicholas DiPiazza

Reputation: 10585

How to use `clj-http` connection pool from a ring web application?

I am using a clojure web application as a proxy web server.

All of my requests are coming into this clojure ring web application then I use clj-http to send the request on to the final destination.

So I have this working as a naive solution so far that simply calls clj-http/request for each request. This code is extremely similar to what I am doing.

But this isn't good enough yet because each time a request is made, a new http client is initialized. I need connection pooling so that http clients are re-used properly.

The clj-http documentation about persistent connections states that you re-use connections like this:

(with-connection-pool {:timeout 5 :threads 4 :insecure? false :default-per-route 10}
  (get "http://example.org/1")
  (post "http://example.org/2")
  (get "http://example.org/3")
  ...
  (get "http://example.org/999"))

Perhaps I am just not good enough with clojure yet, but how would someone surround all requests coming into https://github.com/tailrecursion/ring-proxy/blob/master/src/tailrecursion/ring_proxy.clj#L40 with this connection pool?

Upvotes: 1

Views: 655

Answers (1)

pete23
pete23

Reputation: 2280

Implement a middleware that adds the connection manager into the request map.

You will need to handle the lifecycle of the connection manager yourself rather than the with- form - see the final part of the clj-http documentation on persistent connections.

Upvotes: 2

Related Questions