Is there any way to know when (and handle) a user closes the connection? #1493
Replies: 2 comments
-
Yes, but this will require you do some lower level work. You can use the You can see this code snippet from our streaming response code to see how this is done. If this is something that might have a larger utility, we could potentially add a utility to do this - although i am not sure how the interface for this will look. |
Beta Was this translation helpful? Give feedback.
-
Basically what I have is class DataAPIControllerV1(Controller):
path = "/api/v1/data"
@post()
async def get_data(self, data: DataRequest) -> DataResponse:
httpx.make_async_req_to_get_some_data()
return DataResponse(...) Now if user makes a req to this endpoint and the http req to make_async_req_to_get_some_data() takes too much time and the user closes the req, then I want to catch somehow the event so that the get_data handler can cancel the req to make_async_req_to_get_some_data() and not wait for it to finish when there's no more client to deliver the response to anymore. |
Beta Was this translation helpful? Give feedback.
-
So for example we have something like: Client -> Middleware (using Litestar) -> some other external API that takes a bit of time to reply.
Let's say a req takes 2s due to middleware waiting for a response from that other API and the client closes the connection.
Is there any way using Litestar to listen for this event and handle it so that the middleware can further abort the connection to the external API?
Beta Was this translation helpful? Give feedback.
All reactions