-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Dobiasd
wants to merge
4
commits into
scylladb:master
Choose a base branch
from
Dobiasd:patch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9199c4f
docs: show a coroutine-based implementation of the echo server in the…
Dobiasd 3aa1710
docs: add comment about ignoring the future retured by handle_connection
Dobiasd ea5b7a3
docs: simplify handle_connection body by co_await-ing in the loop con…
Dobiasd 8ba25cf
Remove old version of echo server and adjust the explanation
Dobiasd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
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)); | ||
Dobiasd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
``` | ||
|
||
# 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. | ||
|
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.
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.)