Introduce output_stream writing shugar#2431
Merged
avikivity merged 3 commits intoscylladb:masterfrom Sep 17, 2024
Merged
Conversation
avikivity
reviewed
Sep 15, 2024
avikivity
reviewed
Sep 15, 2024
The one accepts a callable that writes data into stream, then flushes and closes it in a safe manner. Signed-off-by: Pavel Emelyanov <xemul@scylladb.com>
Both classes have .write_body() method that accepts body writer function. This function should write data into its output_stream argument, flush it and close properly. This pattern is pretty common and takes some care to make it right (e.g. -- not to bail out with exception leaving the stream not closed). The new overloads are aimed at making the request/response users life a bit simpler. Signed-off-by: Pavel Emelyanov <xemul@scylladb.com>
Signed-off-by: Pavel Emelyanov <xemul@scylladb.com>
3ec7fe5 to
59d82c4
Compare
Contributor
Author
|
upd:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When putting data into output_stream() a common pattern exists. Data is written into stream, then flushed and the end and the stream is closed. Any exception that may pop up in the middle shouldn't leave stream not closed, so in the code it looks smth like
This is very typical to http client and handlers code that use request::write_bod() and response::write_body() overloads that call user-provided lambda giving it chunked or content-length output_stream. The caller then implements the paterns described above on its own.
This PR suggest a pair of helpers -- one is the util::write_to_stream() function that looks literally like above; the other one is request and response .write_body() overloads that accept a callback doing only the out.write() (for the loop) part of the above, flushing and closing the stream on its own.