New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can rack.after_reply be added to the spec? #1777
Comments
Why not https://github.com/rack/rack/blob/master/lib/rack/events.rb instead? |
I didn't get to dive too deeply into why, but the |
(1) What is your use case for (2) How does it work with HTTP/2? |
The use-case at the moment is for deferring non-critical work done in a request/response to happen after a user has already received the response. This primarily includes work that can't be a background job, like request-specific metrics that need to be sent over the wire and cache writes that we know won't need to be read back during the same request. The biggest benefit is that this lets us write abstractions that move potentially expensive work outside of the request so users see (often significantly) faster response times. re: HTTP/2 I have to admit I'm not deeply familiar with http/2 and have only a basic understanding of it. I imagine it would be very similar to the existing implementations we have today. Some resource would be requested, we'd expose the ability to add lambdas to |
on_finish triggers after the response has been written to the client, which is the only level rack should work at. The connection shouldn't play a role here, as rack is for request/response management, that's what the servers are for. If this is what you need, then the callback being defined in the Webserver already makes sense, and shouldn't be ported here IMO. |
Apologies, but I'm not sure I completely understand. I'm not proposing that Rack adds this functionality directly, but that Rack adds |
I understood, I'm just challenging why this should be specced. First, after_reply is too broad. What is a reply, a response? Then the events middleware should be what you need. You mention "after the connection closes". What is a connection? If it's a tcp socket, then it's a layer below what rack should control. Also, what if it doesn't close? That's the case for persistent connections or even HTTP/2. IMO "rack.after_,reply" shouldn't be part of the spec. In fact, it already follows a known convention of prefixing keys with "rack.", a common practice for a multitude of known middleware which uses the rack env as a bucket for functionality and isn't part of the spec (such as warden and other several authentication Middlewares). |
For your use case, e.g.
Is it very different from:?
or even
It seems to me the only advantage would be the specific guarantee about when it happens but they would equally offload expensive processing. Regarding The next point is defining error handling semantics. What happens if the callback fails? What happens if the client bombs half way through writing the response body? What happens if it's a websocket request? Does this imply we'd want something like |
Those are great questions. A large part of the value of adding it as a spec (to me) is that we can answer those questions to help define consistent behavior across implementations.
Functionality wise they're not that different from one another. As you mentioned you lose some amount of predictability (i.e. running once per-request once the handler is complete) which is great to know and can help inform what is/isn't good to put in the queue. For example, one use-case is storing a set of data to be sent once the request is complete so that we can utilize batching functionality. Without Unfortunately in my situation the application isn't thread safe so we would need to be even more deliberate and cautious about what goes into the queue. I'm not sure if it's implied or not, but having the functionality coupled to a
I'm 👍 on changing the name to make the behavior more clear. Like mentioned above, I think a large part of the value of adding this as a spec is that we can define those semantics and have them implemented consistently in the rack ecosystem. I'd be happy to help with that as much as I can.
The existing error handling is currently a I agree with your statement about exposing too many implementation details being a less-than-great direction. I like |
@tenderlove I know you have some ideas around this, do you mind reviewing this request? |
I agree we should add this and I like |
@BlakeWilliams do you want to have a go at cutting the spec for this? Update |
Just linking for posterity - an excellent use of |
Definitely, I'll make some time to work on that. |
Just opened up a draft PR that has a few open questions here: #1802 |
Merged in #1952 |
Both Puma and Unicorn have support for
rack.after_reply
which has been extremely useful but it's not part of the official Rack spec.Given its usefulness and existing support, would it make sense to make this an official part of the spec to help encourage other rack based webservers to implement this functionality? If not, is there a path forward to making this an official part of the spec?
The text was updated successfully, but these errors were encountered: