    :result [true]})
 
 (defn validate [[dsp res]]
-  (let [{:keys [status body error]} @res
-        body (and body (json/read-str body :key-fn keyword))
+  (let [{:keys [status body]} @res
+        body (and body (json/read-str body :key-fn keyword :eof-error? false))
         {:keys [id bidPrice advertiserId]} body
         ret (cond
               (= status 204) {:nobid "no bid"}

(ns s7p.core
  (:require [clojure.data.json :as json]
            [org.httpkit.client :as http]
            [clojure.core.async :refer [go go-loop chan close! <! <!! >!]]))

(def options {:timeout 100
              :keepalive 3000})

(def dsps (atom
           [{:id "1"
             :url "http://dsp-no-bid.compe"
             :winnotice "http://dsp-no-bid.compe"}
            {:id "2"
             :url "http://dsp-no-bid.compe"
             :winnotice "http://dsp-no-bid.compe"}
            {:id "3"
             :url "http://dsp-no-bid.compe"
             :winnotice "http://dsp-no-bid.compe"}
            {:id "4"
             :url "http://dsp-no-bid.compe"
             :winnotice "http://dsp-no-bid.compe"}
            {:id "5"
             :url "http://dsp-no-bid.compe"
             :winnotice "http://dsp-no-bid.compe"}
            {:id "6"
             :url "http://dsp-no-bid.compe"
             :winnotice "http://dsp-no-bid.compe"}
            {:id "7"
             :url "http://dsp-no-bid.compe"
             :winnotice "http://dsp-no-bid.compe"}]))

(def advertisers [{:id "1"} {:id "2"} {:id "3"} {:id "4"}])


  ;; TODO: log
  (println response)

  (http/post  (:winnotice dsp)
             ;; FIXME: options
             {:id (:advertiserId response)
              :price (+ 1 second-price)
              :isClick (click? result response)}))

(defn gen-request [n]
  (let [c (chan)]
    (thread
      (loop [i n]
        (>!! c request)
        (if (< 0 i)
          (recur (- i 1))
          (close! c))))
    c))

(defn process [c]
  (thread
    (loop []
      (when-let [{:keys [req result]} (<!! c)]
        (let [xf (comp
                  (map (fn [dsp] [dsp (http/post (:url dsp) (assoc options :body (json/write-str req)))]))
                  (map validate)
                  ;; TODO: log validated responses
                  (filter succeed?)
                  (map (fn [[_ res]] (:valid res))))]
          (some->> (sequence xf @dsps)
                   (pick-winner-and-second-price (:floorPrice req))
                   (winnotice req result)
                   )
          (recur))))))


(defn -main [& args]
  (time (let [c (gen-request 1000)]
          (doseq [sig (vec (map (fn [_] (process c)) (range 64)))]
            (<!! sig)))))
