ring

2024-06-21T14:04:53.691989Z

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?

✅ 1
Ben Sless 2024-06-21T15:14:08.151709Z

Does jetty let you mix sync and async endpoints?

2024-06-21T15:33:42.314829Z

well, compojure handles that for us

2024-06-21T16:41:57.596539Z

does it?

2024-06-21T17:01:50.843319Z

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

2024-06-21T17:07:31.706809Z

Sure doesn't look that way

2024-06-21T17:08:07.664009Z

That shows if you call the route with async it does the async thing, if you call it sync it does the sync thing

2024-06-21T17:09:06.044849Z

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.

2024-06-21T17:14:11.351329Z

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")))

➕ 1
2024-06-21T17:14:37.661369Z

so i had to go through all of our existing middleware and make sure that the branches either call handler or respond as necessary

weavejester 2024-06-27T11:57:16.242939Z

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.

👍 1