cch1 / http.async.client

Async Http Client - Clojure
http://cch1.github.com/http.async.client
267 stars 40 forks source link

Boolean options are handled incorrectly in client/create-client #11

Closed vgeshel closed 13 years ago

vgeshel commented 13 years ago

The problem is that

(when option ...

Doesn't distinguish between a nil (meaning don't change the default) and false.

Here is the fix. It also adds the ability to configure a couple more things in the underlying library: asynchronous connection and the executor service

diff --git a/src/clj/http/async/client.clj b/src/clj/http/async/client.clj
index ca3ee1e..b4188f3 100644
--- a/src/clj/http/async/client.clj
+++ b/src/clj/http/async/client.clj
@@ -20,7 +20,8 @@
   (:use [http.async.client request headers util]
         clojure.template)
   (:import (java.io ByteArrayOutputStream)
-           (com.ning.http.client AsyncHttpClient AsyncHttpClientConfig$Builder)))
+           (com.ning.http.client AsyncHttpClient AsyncHttpClientConfig$Builder)
+           (com.ning.http.client.providers.netty NettyAsyncHttpProviderConfig)))

 (defn create-client
   "Creates new Async Http Client.
@@ -47,18 +48,26 @@
              proxy
              realm
              request-timeout
-             user-agent]}]
+             user-agent
+             async-connect
+             executor-service]}]
   (AsyncHttpClient.
    (.build
     (let [b (AsyncHttpClientConfig$Builder.)]
-      (when compression-enabled (.setCompressionEnabled b compression-enabled))
+      (when (not (nil? compression-enabled)) (.setCompressionEnabled b compression-enabled))
       (when connection-timeout (.setConnectionTimeoutInMs b connection-timeout))
-      (when follow-redirects (.setFollowRedirects b follow-redirects))
-      (when idle-in-pool-timeout (.setIdleConnectionInPoolTimeoutInMs b idle-in-pool-timeout))
-      (when keep-alive (.setKeepAlive b keep-alive))
+      (when (not (nil? follow-redirects)) (.setFollowRedirects b follow-redirects))
+      (when idle-in-pool-timeout (.setConnectionTimeoutInMs b idle-in-pool-timeout))
+      (when (not (nil? keep-alive)) (.setAllowPoolingConnection b keep-alive))
       (when max-conns-per-host (.setMaximumConnectionsPerHost b max-conns-per-host))
       (when max-conns-total (.setMaximumConnectionsTotal b max-conns-total))
       (when max-redirects (.setMaximumNumberOfRedirects b max-redirects))
+      (when async-connect
+        (let [provider-config (doto (NettyAsyncHttpProviderConfig.)
+                                (.removeProperty NettyAsyncHttpProviderConfig/USE_BLOCKING_IO)
+                                (.addProperty NettyAsyncHttpProviderConfig/EXECUTE_ASYNC_CONNECT true))]
+          (.setAsyncHttpClientProviderConfig b provider-config)))
+      (when executor-service (.setExecutorService b executor-service))
       (set-proxy proxy b)
       (when realm
         (set-realm realm b))
neotyk commented 13 years ago

Thank you for you feedback. I've merged your branch. Happy New Year!