Best way to manage a pool of resources in Clojure -
i have a web service endpoint uses mutable resource java library. web service endpoint can receive multiple queries @ same time. (the endpoint implemented using ring/compojure). creating these resources costly, re-creating them every web service call inefficient.
what want create pool of resource populate when web service starts. each time endpoint called, takes resource pool, use processing, , push pool , wait next call happen.
i wondering best way in clojure? there "pool" clojure library me that?
i naively tried implement using vector in atom each item of vector resource. however, learned not work way.
this based on timothy pratley's idea of using refs:
(def pool (ref ['a 'b 'c])) (defn take' [pool] (dosync (let [[h & t] @pool] (ref-set pool (vec t)) h))) (defn put [pool x] (dosync (alter pool conj x) nil)) (take' pool) ;; => 'a (put pool 'a) ;; => nil (take' pool) ;; => 'a (take' pool) ;; => 'b (take' pool) ;; => 'c maybe not best way attack this. simplicity of it.
Comments
Post a Comment