Observing a response is successfully written #2490
-
SummaryThis seems rather plain but appears complicated 😅. We run a service where we intentionally reply close to the client side connection timeout. So I'd be happy to either notice when a connection is closed prematurely by the client or the server has sent all it wanted. I dug down to hyper for the former. Seems there is some 100 line way of doing it over there if you happen to be using hyper directly. So then I thought maybe instead of connection closed, which seems complicated for low level reasons, we could detect the last byte is written to lower layers. Having the TCP connection close successfully would be ideal, but something like last body byte written to a socket could be close enough. I have again found nothing but am hopeful someone here knows how to achieve that. As a minimum, I found this discussion where a guard is used to detect the invoked handler fn got dropped, you could combine this with a flag set right before the return. That way you could check there whether a response was returned, or the connection closed before. Decent. Curious if anyone sees a way to do better. I also tried pulling back TraceLayer to see if it could be helpful. It can log chunks of body written. Still not ideal. Or a successful response being passed off by the handler. Still pretty far from "have the bytes been successfully sent to the client". For context, client based solutions are out. We don't control the client. We get one request, the client will close the connection after a timeout, we want to check if we responded in time. axum version0.6.20 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
axum intentionally doesn't implement that actual transport layer, that is what hyper is for. So you have to rely on hyper's low-level API to do this. |
Beta Was this translation helpful? Give feedback.
axum intentionally doesn't implement that actual transport layer, that is what hyper is for. So you have to rely on hyper's low-level API to do this.