-
Notifications
You must be signed in to change notification settings - Fork 577
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
Let user know when Plug.Conn.register_before_chunk/2
sends the last chunk
#1157
Comments
I am not sure this is possible because we don't have a callback that notifies the chunk is done. So you would need to provide such a mechanism yourself. You can store a callback in assigns and invoke it when you are done. |
I'm curious how @feynmanliang intends to use
Indeed, and I don't understand how plug notifies the adapter streaming is over 🤔 Cowboy's adapter for instance expects a |
Would be great to have! We currently rely on timeouts / a special chunk indicating end of messages. |
@tanguilp do you mind sharing where you found that |
@feynmanliang It was not clear in my message but I was talking about chunked transfer encoding, not SSE events. My use cache is HTTP caching. I want to cache chunks these responses by chunk and recombine the chunks afterwards. To do this I need to know when the last chunk is sent (otherwise there's no way there won't be an additional chunk). I'm curious what you use case is. |
We do the exact same thing: collect a SSE stream and recombine it into a full response for caching. In our particular use case, we know in advance the SSE streams are not long-lived and so we can either rely on a timeout or the protocol specifying a sentinel end-of-stream message. I found https://www.rfc-editor.org/rfc/rfc6202 quite helpful, specifically:
answers my question about the terminating chunk for HTTP/1.1 streaming
I could be wrong, but I believe this is what we rely on in our implementation. |
I have reverted register_before_chunk for now, in case we need to modify it so it considers closing as well. |
I'm trying to figure out how to determine when
Plug.Conn.register_before_chunk/2
sends the last chunk.My use-case is the following: I want to cache chunks and possibly recombine them to cache the full response. It's possible to do even without knowing the total length of the of the response, but eventually knowing the content length has some benefits:
range
header allows syntax likerange: bytes=200-
(from bytes 200 to the end) andrange: bytes=-500
(the last 500 bytes), which require knowing the total lengthcontent-length
header, which can be useful to clients(
content-length
must not be set whentransfer-encoding
is set.)I can think of some unreliable ways of doing it by using the process dictionary to store current sent length and by assuming that when the process dies the full message is sent. However I'm wondering if it's possible to implement it in some way in
Plug.Conn.register_before_chunk/2
.The text was updated successfully, but these errors were encountered: