Skip to content

Instantly share code, notes, and snippets.

@noisesmith
Created May 8, 2017 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noisesmith/3aa431f238df82f05a159f4ff8ac1816 to your computer and use it in GitHub Desktop.
Save noisesmith/3aa431f238df82f05a159f4ff8ac1816 to your computer and use it in GitHub Desktop.
one of these gives a cryptic eastwood error
;; OK
(defn launch-loop
[semaphor pool urgent defer]
(>/go-loop
[]
(when-not (realized? semaphor)
(let [task (or (trigger-queue urgent)
(trigger-queue defer))
response (>/chan)
wait-for (if task
(pool-thread response pool task)
(>/timeout 1000))]
(>/<! wait-for)
(recur)))))
(defrecord OfferQueue [thread-count]
component/Lifecycle
(start [component]
(let [urgent (ref queue)
defer (ref queue)
pool (pool/threadpool thread-count)
semaphor (delay ::done)]
(dotimes [_ thread-count]
(launch-loop semaphor pool urgent defer))
(assoc component
:shutdown-key semaphor
:pool pool
:now (fn [f] (f))
:urgent (fn [f] (dosync (alter urgent conj f)))
:defer (fn [f] (dosync (alter defer conj f))))))
(stop [component]
(let [{:keys [pool shutdown-key]} component]
(force shutdown-key)
(.shutdown pool)
(dissoc component :shutdown-key :pool :now :urgent :defer))))
;; NOT OK
(defrecord OfferQueue [thread-count]
component/Lifecycle
(start [component]
(let [urgent (ref queue)
defer (ref queue)
pool (pool/threadpool thread-count)
semaphor (delay ::done)]
(dotimes [_ thread-count]
(>/go-loop
[]
(when-not (realized? semaphor)
(let [task (or (trigger-queue urgent)
(trigger-queue defer))
response (>/chan)
wait-for (if task
(pool-thread response pool task)
(>/timeout 1000))]
(>/<! wait-for)
(recur)))))
(assoc component
:shutdown-key semaphor
:pool pool
:now (fn [f] (f))
:urgent (fn [f] (dosync (alter urgent conj f)))
:defer (fn [f] (dosync (alter defer conj f))))))
(stop [component]
(let [{:keys [pool shutdown-key]} component]
(force shutdown-key)
(.shutdown pool)
(dissoc component :shutdown-key :pool :now :urgent :defer))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment