On 3 September 2014 09:35, Laurens Van Houtven <_...@lvh.io> wrote: > > > (defn handler [req] > > (httpkit/with-channel req http-ch > > (let [resp-ch (async-handler! req)] > > (httpkit/on-close (fn [_] (a/close! resp-ch))) > > (a/take! resp-ch (fn [resp] (httpkit/send! http-ch resp) > (httpkit/close http-ch)))))) > > That’s what I wanted to do, but then I saw the warning on http-kit’s > website at http://http-kit.org/server.html#channel , which states: > > Data is sent directly to the client, NO RING MIDDLEWARE IS APPLIED. > > … which leads me to believe that the send! API just totally sidesteps any > middleware, and it’s not so much up to the middleware whether or not it > supports asynchronous operation. Unless of course it actually works fine, > and that’s simply some stale documentation? >
It means that any middleware wrapping the handler won't affect the response. For instance, assume you have an async handler that returns a channel. (defn async-handler! [req] (let [ch (a/chan)] (go (<! ch {:status 200, :headers {}, :body "Hello World"}) (a/close! ch)) ch)) The normal way of applying middleware is to pass the handler to a wrap-whatever function: (def async-handler! (wrap-foo (fn [req] (let [ch (a/chan)] (go (<! ch {:status 200, :headers {}, :body "Hello World"}) (a/close! ch)) ch)) The middleware function expects the handler to return a map, not a channel, so this won't work. However, Ring supplies foo-request and foo-response functions for all the standard middleware, so you can write: (defn async-handler! [req] (let [ch (a/chan)] (go (<! ch (foo-response {:status 200, :headers {}, :body "Hello World"})) (a/close! ch)) ch)) Using the standard wrap-foo middleware is obviously preferable the vast majority of the time, as it's less complected. - James -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com 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 clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.