Skip to content

Commit

Permalink
Add support for connection limits. Safely close input streams to allo…
Browse files Browse the repository at this point in the history
…w cleanup.
  • Loading branch information
rnewman committed Jul 31, 2010
1 parent 87a8ed5 commit 17d0a79
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/com/twinql/clojure/http.clj
Expand Up @@ -7,6 +7,7 @@
(:import
(java.lang Exception)
(java.net URI)
(java.io InputStream)
(org.apache.http
HttpHost ; For proxy.
HttpRequest
Expand Down Expand Up @@ -47,14 +48,18 @@
BasicScheme)
(org.apache.http.conn.ssl
SSLSocketFactory)
(org.apache.http.conn.params
ConnPerRoute
ConnManagerPNames)
(org.apache.http.conn.scheme
PlainSocketFactory
SchemeRegistry
Scheme)
(java.security
KeyStore)
(org.apache.http.params
BasicHttpParams)
BasicHttpParams
HttpParams)
(org.apache.http.conn
ClientConnectionManager)
(org.apache.http.impl.conn
Expand Down Expand Up @@ -190,22 +195,27 @@
(defmethod entity-as nil
[entity as status] entity)

;; Client is responsible for cleanup.
(defmethod entity-as :stream [#^HttpEntity entity as status]
(.getContent entity))

;; Client is responsible for cleanup.
(defmethod entity-as :reader [#^HttpEntity entity as status]
(io/reader (.getContent entity)))

(defmethod entity-as :string [#^HttpEntity entity as status]
(io/slurp* (.getContent entity)))
(with-open [#^InputStream stream (.getContent entity)]
(io/slurp* stream)))

;;; JSON handling.
;;; We prefer keywordizing.
(defmethod entity-as :json [#^HttpEntity entity as status]
(clojure.contrib.json/read-json (io/reader (.getContent entity)) true))
(with-open [#^InputStream stream (.getContent entity)]
(clojure.contrib.json/read-json (io/reader stream) true)))

(defmethod entity-as :json-string-keys [#^HttpEntity entity as status]
(clojure.contrib.json/read-json (io/reader (.getContent entity)) false))
(with-open [#^InputStream stream (.getContent entity)]
(clojure.contrib.json/read-json (io/reader stream) false)))


;;; To avoid overhead in shutting down a ClientConnectionManager,
Expand Down Expand Up @@ -236,15 +246,25 @@
;; The HTTP params go away in 4.1.
(SingleClientConnManager. (BasicHttpParams.) registry)))

(defn #^BasicHttpParams connection-limits
"Return HTTP Parameters for the provided connection limit."
[#^Integer max-total-connections]
(doto (BasicHttpParams.)
(.setParameter ConnManagerPNames/MAX_TOTAL_CONNECTIONS max-total-connections)))

(defn #^ClientConnectionManager thread-safe-connection-manager
"Produce a new ThreadSafeClientConnManager with http and https, or
the provided registry."
([]
(thread-safe-connection-manager (scheme-registry true)))

([#^SchemeRegistry registry]
;; The HTTP params go away in 4.1.
;; BasicHttpParams isn't thread-safe...!
(ThreadSafeClientConnManager. (BasicHttpParams.) registry)))
(thread-safe-connection-manager (BasicHttpParams.) registry))

([#^SchemeRegistry registry #^HttpParams params]
(ThreadSafeClientConnManager. params registry)))

(defn shutdown-connection-manager
[#^ClientConnectionManager ccm]
Expand Down

0 comments on commit 17d0a79

Please sign in to comment.