Clojure core.async HTTP request
Requests 20 snippets from fssnip.net sequentially and in parallel, profiles using timbre and outputs the results.
(ns batchreq.core
(:refer-clojure :exclude [map reduce into partition partition-by take merge])
(:require [clojure.core.async :refer :all :as async]
[clj-http.client :as client])
(:use taoensso.timbre
taoensso.timbre.profiling))
(defnp http-get [id]
(client/get (format "http://fssnip.net/%d" id))
id)
(defnp sequentially [rn res]
(doseq [i rn]
; sequential http-get for each item in the range
(swap! res conj (http-get i))))
(defnp in-parallel [rn res]
(let [channel (chan)]
(doseq [i rn]
; send the http-get requests to the channel (non blocking)
(go (>! channel (http-get i))))
(doseq [i rn]
; take result from the channel (blocks if nothing is available)
(swap! res conj (<!! channel)))))
(defn request-snippets [f]
(let [rn (range 10 30)
res (atom [])]
(profile :info :Arithmetic (f rn res))
(info @res)
@res))