Skip to content

Introduce output_stream writing shugar#2431

Merged
avikivity merged 3 commits intoscylladb:masterfrom
xemul:br-iostream-sugar
Sep 17, 2024
Merged

Introduce output_stream writing shugar#2431
avikivity merged 3 commits intoscylladb:masterfrom
xemul:br-iostream-sugar

Conversation

@xemul
Copy link
Copy Markdown
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
Comment thread include/seastar/util/iostream.hh
Comment thread include/seastar/util/iostream.hh Outdated
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>
@xemul
Copy link
Copy Markdown
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
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