-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
9199c4f
3aa1710
ea5b7a3
8ba25cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
||
```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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nicer: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, what a great idea! ❤️ |
||
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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
There was a problem hiding this comment.
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().
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the following goals:
I guess we have the following options for this PR here:
(Doing B currently feels way above my understanding/skills.)