 ;(STEP 5 "Non blocking http")
 
 
-(defn chan-http-get [url c]
-  (let [start (System/currentTimeMillis)]
-    (http/get url {} #(async/put! c [(- (System/currentTimeMillis) start)
-                                     (= 200 (:status %))]))))
+(defn async-http-get [url c]
+  (let [now   #(System/currentTimeMillis)
+        start (now)]
+    (go
+      (http/get url {} #(put! c [(- (now) start)
+                                 (= 200 (:status %))])))))
 
-(defn run-non-blocking [number-of-users url]
-  (let [cs (repeatedly number-of-users async/chan)]
+(defn run-non-blocking [concurrency url]
+  (let [cs (repeatedly concurrency async/chan)]
     (doseq [c cs]
-        (go (chan-http-get url c)))
-    (repeatedly number-of-users #(collect-result cs))))
+      (go (async-http-get url c)))
+    (repeatedly concurrency #(first (alts!! cs)))))
 
 
 ; (STEP 6 "With timeout")
 
-(defn chan-http-get-with-timeout [url timeout result-channel]
-  (let [
-          now     #(System/currentTimeMillis)
-        start   (now)
+(defn async-http-get-with-timeout [url timeout result-channel]
+  (let [now      #(System/currentTimeMillis)
+        start    (now)
         response (async/chan)]
     (go
-      (http/get url {} #(async/put! response [(- (now) start) (= 200 (:status %))]))
-      (let [[result c] (async/alts! [response (async/timeout timeout)])]
+      (http/get url {} #(put! response [(- (now) start) (= 200 (:status %))]))
+      (let [[result c] (alts! [response (async/timeout timeout)])]
         (if (= c response)
           (>! result-channel result)
           (>! result-channel [timeout false]))))))
 
-(defn run-non-blocking-with-timeout [number-of-users timeout url]
-  (let [cs (repeatedly number-of-users async/chan)]
+(defn run-non-blocking-with-timeout [concurrency timeout url]
+  (let [cs (repeatedly concurrency async/chan)]
     (doseq [c cs]
-        (go (chan-http-get-with-timeout url timeout c)))
-    (repeatedly number-of-users #(collect-result cs))))
+        (go (async-http-get-with-timeout url timeout c)))
+    (repeatedly concurrency #(first (alts!! cs)))))
 
 
 ; (STEP 7 "Constantly")
 
-(defn run-constantly [number-of-users number-of-requests timeout url]
-  (let [cs       (repeatedly number-of-users async/chan)
+(defn run-constantly [concurrency number-of-requests timeout url]
+  (let [cs       (repeatedly concurrency async/chan)
         results  (async/chan)]
     (doseq [c cs]
-        (chan-http-get-with-timeout url timeout c))
+        (async-http-get-with-timeout url timeout c))
     (go-loop [i 0]
-      (let [[result c] (async/alts! cs)]
+      (let [[result c] (alts! cs)]
         (chan-http-get-with-timeout url timeout c)
         (>! results result)
         (when (<= i number-of-requests)

        result (function)]
    [(- (System/currentTimeMillis) start) result]))

(defn run-with-bench [number-of-users step]
  (let [cs (repeatedly number-of-users async/chan)]
    (doseq [c cs]
      (go (>! c (bench step))))
    (repeatedly number-of-users #(collect-result cs))))




  (fn []
    (= 200 (:status @(http/get url)))))

(defn run-with-url [concurrency url]
  (let [cs (repeatedly concurrency async/chan)]
    (doseq [c cs]
      (go (>! c (bench (http-get url)))))
    (repeatedly concurrency #(first (alts!! cs)))))



