Permalink
Browse files

Request timeout configurable per request.

  • Loading branch information...
1 parent ccdb938 commit 2972cbef320e2add3bcd5d929c70bc728d94af85 @neotyk neotyk committed Sep 15, 2010
View
@@ -19,3 +19,11 @@
[[http://neotyk.github.com/http.async.client/autodoc/http.async.client-api.html#http.async.client/create-client][*http.async.client/create-client*]] and [[http://neotyk.github.com/http.async.client/autodoc/http.async.client-api.html#http.async.client/with-ahc][*http.async.client/with-ahc*]].
** Keyworded maps in API
Like *:options* in [[http://neotyk.github.com/http.async.client/autodoc/http.async.client-api.html#http.async.client/GET][*http.async.client/GET*]].
+* Release 0.2.1
+** Canceling request
+ *http.async.client/cancel* and *http.async.client/canceled?*
+** Request timeout
+ Per request configuration of request timeout in ms.
+ #+BEGIN_SRC clojure
+ (GET url :timeout 100)
+ #+END_SRC
View
@@ -3,13 +3,14 @@
:namespaces [http.async.client]
:dependencies [[org.clojure/clojure "1.2.0"]
[org.clojure/clojure-contrib "1.2.0"]
- [com.ning/async-http-client "1.1.0"]]
+ [com.ning/async-http-client "1.2.0-SNAPSHOT"]]
:dev-dependencies [[swank-clojure "1.2.1"]
[autodoc "0.7.1"]
[org.clojars.neotyk/lein-javac "1.0.1"]
[lein-difftest "1.2.2"]
[org.eclipse.jetty/jetty-server "7.1.4.v20100610"]
- [org.danlarkin/clojure-json "1.1"]]
+ [org.danlarkin/clojure-json "1.1"]
+ [lein-run "1.0.0"]]
:autodoc {:name "http.async.client"
:web-src-dir "http://github.com/neotyk/http.async.client/blob/"
:web-home "http://neotyk.github.com/http.async.client/autodoc/"
@@ -2,6 +2,7 @@
import java.io.InputStream;
import com.ning.http.client.Realm;
import com.ning.http.client.RequestBuilder;
+import com.ning.http.client.PerRequestConfig;
import com.ning.http.client.ProxyServer;
import com.ning.http.client.Cookie;
public class RequestBuilderWrapper {
@@ -34,4 +35,7 @@ public RequestBuilderWrapper addCookie(Cookie cookie) {
public RequestBuilderWrapper setRealm(Realm realm) {
rb.setRealm(realm);
return this;}
+ public RequestBuilderWrapper setPerRequestConfig(PerRequestConfig perRequestConfig) {
+ rb.setPerRequestConfig(perRequestConfig);
+ return this;}
public RequestBuilder getRequestBuilder() { return rb; }}
@@ -26,6 +26,7 @@
FluentCaseInsensitiveStringsMap
HttpResponseStatus HttpResponseHeaders
HttpResponseBodyPart
+ PerRequestConfig
Realm$RealmBuilder Realm$AuthScheme
Request RequestBuilder
RequestType ProxyServer)
@@ -119,14 +120,16 @@
:type - either :basic or :digest
:user - user name to be used
:password - password to be used
- :realm - realm name to authenticate in"
+ :realm - realm name to authenticate in
+ :timeout - request timeout in ms"
{:tag Request}
[method #^String url & {headers :headers
query :query
body :body
cookies :cookies
proxy :proxy
- auth :auth}]
+ auth :auth
+ timeout :timeout}]
;; RequestBuilderWrapper is needed for now, until RequestBuilder
;; is able to be used directly from Clojure.
(let [#^RequestBuilderWrapper rbw
@@ -185,6 +188,11 @@
;; proxy
(if proxy
(.setProxyServer rbw (ProxyServer. (:host proxy) (:port proxy))))
+ ;; request timeout
+ (if timeout
+ (let [prc (PerRequestConfig.)]
+ (.setRequestTimeoutInMs prc timeout)
+ (.setPerRequestConfig rbw prc)))
;; fine
(.. (.getRequestBuilder rbw) (setUrl url) (build))))
@@ -83,7 +83,8 @@
:type - either :basic or :digest
:user - user name to be used
:password - password to be used
- :realm - realm name to authenticate in")]
+ :realm - realm name to authenticate in
+ :timeout - request timeout in ms")]
`(defn ~fn-name ~fn-doc [#^String ~'url & {:as ~'options}]
(apply r/execute-request
(apply r/prepare-request ~method# ~'url (apply concat ~'options))
@@ -28,7 +28,9 @@
(org.eclipse.jetty.server.handler AbstractHandler)
(org.eclipse.jetty.continuation Continuation ContinuationSupport)
(javax.servlet.http HttpServletRequest HttpServletResponse Cookie)
- (java.io ByteArrayOutputStream)))
+ (java.io ByteArrayOutputStream)
+ (java.util.concurrent TimeoutException)))
+(set! *warn-on-reflection* true)
;; test suite setup
(def default-handler
@@ -91,6 +93,7 @@
(if (= auth "Basic YmVhc3RpZTpib3lz")
200
401)))
+ "/timeout" (Thread/sleep 200)
(doseq [n (enumeration-seq (.getParameterNames hReq))]
(doseq [v (.getParameterValues hReq n)]
(.addHeader hResp n v))))
@@ -359,6 +362,12 @@
(is (true? (cancel resp)))
(is (true? (cancelled? resp)))))
+(deftest reqeust-timeout
+ (let [resp (GET "http://localhost:8123/timeout" :timeout 100)]
+ (await resp)
+ (is (true? (failed? resp)))
+ (is (instance? TimeoutException (error resp)))))
+
;;(deftest profile-get-stream
;; (let [gets (repeat (GET "http://localhost:8123/stream"))
;; seqs (repeat (stream-seq :get "http://localhost:8123/stream"))

0 comments on commit 2972cbe

Please sign in to comment.