代码之家  ›  专栏  ›  技术社区  ›  Nicholas DiPiazza

如何使用ring web应用程序中的“clj http”连接池?

  •  1
  • Nicholas DiPiazza  · 技术社区  · 7 年前

    我使用clojure web应用程序作为代理web服务器。

    我的所有请求都进入这个clojure-ring web应用程序,然后我使用 clj-http 将请求发送到最终目的地。

    所以到目前为止我有一个简单的解决方案 clj-http/request 每一个请求。 This code is extremely similar to what I am doing 是的。

    但这还不够好,因为每次发出请求时,都会初始化一个新的http客户端。我需要连接池以便正确地重用http客户端。

    这个 clj-http documentation about persistent connections 声明您重复使用这样的连接:

    (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"))
    

    也许我对clojure还不够好,但是有人会怎么处理所有的请求呢? https://github.com/tailrecursion/ring-proxy/blob/master/src/tailrecursion/ring_proxy.clj#L40 有这个连接池吗?

    1 回复  |  直到 7 年前
        1
  •  2
  •   pete23    7 年前

    实现一个将连接管理器添加到请求映射中的中间件。

    您需要自己处理连接管理器的生命周期,而不是with-form—请参阅关于持久连接的CLJHTTP文档的最后一部分。

    推荐文章