I'm trying to introduce an async endpoint to my synchronous jetty-adapter server. i have a number of custom middleware, and even tho i think i've implemented the async arity for each of them correctly, the server has an async timeout instead of responding correctly. there aren't many/any docs on how to properly write async middleware, so has anyone had experience with this? any tips or ideas for how I can solve the issue?
Does jetty let you mix sync and async endpoints?
well, compojure handles that for us
does it?
the compojure GET etc routes build both synchronous and asynchronous arities of the handler function, both of which use the normal sync version. however, you can specify your own 3-arity function to make the endpoint async
Sure doesn't look that way
https://github.com/weavejester/compojure/blob/master/src/compojure/core.clj#L187
That shows if you call the route with async it does the async thing, if you call it sync it does the sync thing
https://github.com/weavejester/compojure/blob/22c56a627522f3343026c3a773630713e1e00eae/src/compojure/core.clj#L155-L160 is used by make-route which is used by compile-route which is the basis for GET etc.
i think i figured out my original issue, which is that I missed that async middleware ignores the return value, so you can't just say (if (pred req) (handler req respond raise) (error-response "Not good"), you have to say (if (pred req) (handler ...) (respond (error-response "not good")))
so i had to go through all of our existing middleware and make sure that the branches either call handler or respond as necessary
Yes, that's correct: in async mode, the return value is ignored, for both handlers and middleware. If you ever want to modify the response, you have to wrap the respond argument.