-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Optimize chunked write path and add tryWrite #1922
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
Open
GetThatCookie
wants to merge
10
commits into
uNetworking:master
Choose a base branch
from
GetThatCookie:master
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.
+219
−59
Open
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ce79deb
Optimize chunked write path and add tryWrite
cabf5aa
Move tryWrite smoke coverage into dedicated SmokeTest
b9d52dc
suffix for Windows SmokeTest process
GetThatCookie 8635150
Align tryWrite follow-up with existing example-based repo structure
a5c168c
Clarify tryWrite chunk continuation semantics
a16c068
Tighten tryWrite example and chunk header handling
8c1da31
Rename chunk formatter to reflect emitted CRLF framing
11212b7
Optimize BackPressure compaction and trim policy
c5c47ea
Drop AsyncSocketData changes from tryWrite follow-up
b14a54d
Optimize final chunked tryWrite via AsyncSocket::write2
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| #include "App.h" | ||
|
|
||
| /* This example demonstrates a large chunked response streamed with tryWrite. */ | ||
|
|
||
| #include <cstdint> | ||
| #include <memory> | ||
| #include <string> | ||
|
|
||
| namespace { | ||
|
|
||
| const std::string payload(16 * 1024 * 1024, 'x'); | ||
|
|
||
| struct ResponseState { | ||
| uintmax_t baseOffset = 0; | ||
| bool aborted = false; | ||
| }; | ||
|
|
||
| template <bool SSL> | ||
| bool tryWriteLoop(uWS::HttpResponse<SSL> *res, ResponseState *state) { | ||
| if (state->aborted) { | ||
| return true; | ||
| } | ||
|
|
||
| uintmax_t sent = res->getWriteOffset() - state->baseOffset; | ||
| std::string_view remaining = payload; | ||
| remaining.remove_prefix((size_t) sent); | ||
| if (res->tryWrite(remaining)) { | ||
| res->end(); | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| int main() { | ||
|
|
||
| uWS::SSLApp({ | ||
| .key_file_name = "misc/key.pem", | ||
| .cert_file_name = "misc/cert.pem", | ||
| .passphrase = "1234" | ||
| }).get("/*", [](auto *res, auto */*req*/) { | ||
| auto state = std::make_shared<ResponseState>(); | ||
| state->baseOffset = res->getWriteOffset(); | ||
|
GetThatCookie marked this conversation as resolved.
Outdated
|
||
|
|
||
| res->writeHeader("Content-Type", "application/octet-stream"); | ||
| res->onAborted([state]() { | ||
| state->aborted = true; | ||
| }); | ||
|
|
||
| if (!tryWriteLoop(res, state.get())) { | ||
| res->onWritable([res, state](uintmax_t) { | ||
| return tryWriteLoop(res, state.get()); | ||
| }); | ||
| } | ||
| }).listen(3000, [](auto *listen_socket) { | ||
| if (listen_socket) { | ||
| std::cout << "Listening on port " << 3000 << std::endl; | ||
| } | ||
| }).run(); | ||
|
|
||
| std::cout << "Failed to listen on port 3000" << std::endl; | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.