1

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?

Nicholas DiPiazza
  • 10,029
  • 11
  • 83
  • 152

1 Answers1

2

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.

pete23
  • 2,204
  • 23
  • 28
  • 1
    Hey pete, yeah that's what a colleague of mine said too when they saw this question. i'm not that great with clojure, do you know of any examples that might show this in action? It's too bad this section of `clj-http`'s documentation https://github.com/dakrone/clj-http#custom-middleware doesn't provide an example. i will create a pr of it if i can. And what did you mean by `You will need to handle the lifecycle of the connection manager yourself rather than the with- form - ` i didn't understand this – Nicholas DiPiazza Jul 09 '18 at 01:24
  • 1
    oh shit i see it now. i missed the last section of the persistent connections area just like you said. got it! – Nicholas DiPiazza Jul 09 '18 at 01:38