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

docs: show a coroutine-based implementation of the echo server in the tutorial #2173

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -1778,6 +1778,40 @@ It is often a mistake to silently ignore an exception, so if the future we're ig

The ```handle_connection()``` function itself is straightforward --- it repeatedly calls ```read()``` read on the input stream, to receive a ```temporary_buffer``` with some data, and then moves this temporary buffer into a ```write()``` call on the output stream. The buffer will eventually be freed, automatically, when the ```write()``` is done with it. When ```read()``` eventually returns an empty buffer signifying the end of input, we stop ```repeat```'s iteration by returning a ```stop_iteration::yes```.

Re-written using C++20's coroutines, the above becomes this:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it should replace the original. We encourage coroutines now (except in one case - when the function usually resolves with a ready future and it is very performance sensitive). The original is very outdated, no one should use keep_doing().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think eventually we need to go over the tutorial and reorganize/rewrite it in a way that gives more explanations and examples using coroutines - and only later in a separate section introduces the finer points of continuations, how to use them, when to use them, and so on. In the continuations section we'll need some examples, including of keep_doing, but it doesn't need to be this specific server.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds great! Shall I remove the continuation-based implementation of the echo server and adjust the explanation to match the new coroutines-based one, or would you prefer to do this yourselves?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the following goals:

  • A: Remove the continuation-based version of the echo server and adjust the explanation to match the coroutine-based version.
  • B: Convert the whole tutorial from continuations to coroutines.

I guess we have the following options for this PR here:

  • Discard this PR (close unmerged).
  • Merge it as it is (keep A and B for later)
  • I do A in this PR.

(Doing B currently feels way above my understanding/skills.)

```cpp
seastar::future<> handle_connection(seastar::connected_socket s) {
try {
auto out = s.output();
auto in = s.input();
while (true) {
auto buf = co_await in.read();
if (buf) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicer: while (auto buf = co_await in.read()) {, makes the exist condition clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, what a great idea! ❤️
I just committed this change. ✔️

co_await out.write(std::move(buf));
co_await out.flush();
} else {
break;
}
}
co_await out.close();
}
catch (const std::exception &ex) {
fmt::print(stderr, "Could not handle connection: {}\n", ex);
}
}

seastar::future<> service_loop_3() {
seastar::listen_options lo;
lo.reuse_address = true;
auto listener = seastar::listen(seastar::make_ipv4_address({1234}), lo);
while (true) {
auto res = co_await listener.accept();
(void) handle_connection(std::move(res.connection));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignoring futures is bad practice in a real server it will lead to running out of memory, or to problems during shutdown. I'll accept it since it's so in the original, but at least add comments about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the remark. Just added the comment. ✔️

}
}
```

# Sharded services

In the previous section we saw that a Seastar application usually needs to run its code on all available CPU cores. We saw that the `seastar::smp::submit_to()` function allows the main function, which initially runs only on the first core, to start the server's code on all `seastar::smp::count` cores.
Expand Down