Skip to content
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

Introduce output_stream writing shugar #2431

Merged
merged 3 commits into from
Sep 17, 2024

Conversation

xemul
Copy link
Contributor

@xemul xemul commented Sep 13, 2024

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

    try {
        for (...) {
           co_await out.write()
        }
        co_await out.flush()
    } catch () {
        ex = current_exception()
    }
    co_await out.close()
    if (ex) {
        throw ex
    }

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.

@xemul xemul requested a review from nyh September 13, 2024 10:33
The one accepts a callable that writes data into stream, then flushes
and closes it in a safe manner.

Signed-off-by: Pavel Emelyanov <[email protected]>
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 <[email protected]>
@xemul
Copy link
Contributor Author

xemul commented Sep 17, 2024

upd:

  • new helper name is write_to_stream_and_close
  • added documentation to that one

@avikivity avikivity closed this in ebb0385 Sep 17, 2024
@avikivity avikivity merged commit ebb0385 into scylladb:master Sep 17, 2024
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants