On Wednesday, 27 March 2013 17:54:01 UTC+5:30, John Hume wrote:
>
> On Mar 27, 2013 1:56 AM, "Shantanu Kumar" <[email protected]<javascript:>>
> wrote:
> >
> > Sorry, in the last illustration, the (binding [*deps* deps] ...) cannot
> be useful for Compojure route handlers because dynamic vars are bound at a
> thread-local level; you will probably have to `alter-var-root` it to some
> var and have the handlers use that static var instead. In the code I write
> personally, I use a static var that I `alter-var-root` so I couldn't see
> the error in dynamic var immediately.
>
> If that thread-local binding is done in a middleware, that should work
> fine for ring, which handles each request synchronously on a thread.
> (Whereas alter-var-root would be visible across threads and defeat the OP's
> goal of running tests concurrently.)
>
I consider application initialization would be an expensive operation
(reading the config, setting up dependencies etc) so I wouldn't recommend
to carry that out in the handler. Rather the initialization should be done
only once while setting up the middleware, and simply used by the HTTP
request threads. As shown below:
(defn ensure-init [handler]
(let [app-deps (init-deps)]
(alter-var-root #'deps (constantly app-deps)))
handler) ; handler is unchanged
The `deps` var should be used only during prod/staging etc, not for unit
testing where you want to mock certain things out. I know the
initialization can made to work with dynamic var too, but the dynamic var
lookup is not buying us anything here. I WOULD NOT recommend the following:
(defn ensure-init [handler]
(let [app-deps (init-deps)]
(fn [request]
(binding [*deps* app-deps]
(handler request)))))
Shantanu
>
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.