-
Notifications
You must be signed in to change notification settings - Fork 37
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
Implement co-routine based streams for beast-based HTTP server #513
Merged
Merged
Changes from 27 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
6c31b66
Implement coroutine based body for HTTP
RobinTF d029977
Format code according to clang-format
RobinTF 63557d7
Fix issue skipping last iteration
RobinTF 61a9a69
Drop iterator class
RobinTF eac847f
Restructure header files
RobinTF 3060c1a
Fix clang config
RobinTF 857c3c2
Revert "Fix clang config"
RobinTF dd6ba21
Use old namespace macro
RobinTF c780d1a
Add "polyfill" like replacement for std::ostringstream
RobinTF ab0d5de
Reformat code
RobinTF 3ba14a2
Add copyright header
RobinTF 7408b97
Document and simplify yield_value
RobinTF 38c073a
Add back binary export
RobinTF 13b647a
Use code conventions and add comments
RobinTF 0d4bef7
Remove \r
RobinTF f2e3d76
Add plenty of comments documenting the classes
RobinTF 4138017
Apply formatter
RobinTF 64c0759
Incorporate PR comments
RobinTF 737fa45
Add Unit Tests
RobinTF e268fcc
Introduce buffer size template parameter
RobinTF 8ae61a7
Add test case for edge case
RobinTF 101396d
Add static_assert message
RobinTF 7eed556
Introduce _ prefix to member variable
RobinTF ee1809a
Simplify template arguments
RobinTF b8fbac4
Use consistent hasNext() check in next()
RobinTF 207ff00
Use larger buffer size threshold
RobinTF 9058176
Simplify tests
RobinTF 4de2ef0
Fix generator problem
RobinTF File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Copyright 2021, University of Freiburg, | ||
// Chair of Algorithms and Data Structures. | ||
// Author: Robin Textor-Falconi (textorr@informatik.uni-freiburg.de) | ||
|
||
#pragma once | ||
|
||
#include <exception> | ||
|
||
#include "../Log.h" | ||
#include "../streamable_generator.h" | ||
#include "./beast.h" | ||
|
||
namespace ad_utility::httpUtils::httpStreams { | ||
|
||
/** | ||
* A message body represented by a stream_generator. This allows to use a | ||
* generator function to dynamically create a response. | ||
* Example usage: | ||
* http::response<streamable_body> response; | ||
* // generatorFunction returns a ad_utility::stream_generator::stream_generator | ||
* response.body() = generatorFunction(); | ||
RobinTF marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* response.prepare_payload(); | ||
*/ | ||
struct streamable_body { | ||
// Algorithm for retrieving buffers when serializing. | ||
class writer; | ||
|
||
// The type of the message::body member. | ||
// This determines which type response<streamable_body>::body() returns | ||
using value_type = ad_utility::stream_generator::stream_generator; | ||
}; | ||
|
||
/** | ||
* Algorithm for retrieving buffers when serializing. | ||
* | ||
* Objects of this type are created during serialization | ||
* to extract the buffers representing the body. | ||
*/ | ||
class streamable_body::writer { | ||
value_type& _body; | ||
|
||
public: | ||
// The type of buffer sequence returned by `get`. | ||
using const_buffers_type = boost::asio::const_buffer; | ||
|
||
/** | ||
* `h` holds the headers of the message we are | ||
* serializing, while `b` holds the body. | ||
* | ||
* The BodyWriter concept allows the writer to choose | ||
* whether to take the message by const reference or | ||
* non-const reference. Depending on the choice, a | ||
* serializer constructed using that body type will | ||
* require the same const or non-const reference to | ||
* construct. | ||
* | ||
* Readers which accept const messages usually allow | ||
* the same body to be serialized by multiple threads | ||
* concurrently, while readers accepting non-const | ||
* messages may only be serialized by one thread at | ||
* a time. | ||
* | ||
* We need the non-const case here, because the one-shot stream_generator | ||
* conceptually can't allow const access. | ||
*/ | ||
template <bool isRequest, class Fields> | ||
writer([[maybe_unused]] boost::beast::http::header<isRequest, Fields>& h, | ||
value_type& b) | ||
: _body{b} {} | ||
|
||
/** | ||
* This is called before the body is serialized and | ||
* gives the writer a chance to do something that might | ||
* need to return an error code. | ||
*/ | ||
void init(boost::system::error_code& ec) noexcept { | ||
// Set the error code to "no error" (default value). | ||
ec = {}; | ||
} | ||
|
||
/** | ||
* This function is called zero or more times to | ||
* retrieve buffers. A return value of `boost::none` | ||
* means there are no more buffers. Otherwise, | ||
* the contained pair will have the next buffer | ||
* to serialize, and a `bool` indicating whether | ||
* or not there may be additional buffers. | ||
* | ||
* Our strategy is to iterate over the generator to get the data step by step. | ||
*/ | ||
boost::optional<std::pair<const_buffers_type, bool>> get( | ||
boost::system::error_code& ec) { | ||
// Return the buffer to the caller. | ||
// | ||
// The second element of the pair indicates whether or | ||
// not there is more data. As long as there is some | ||
// unread bytes, there will be more data. Otherwise, | ||
// we set this bool to `false` so we will not be called | ||
// again. | ||
// | ||
try { | ||
std::string_view view = _body.next(); | ||
ec = {}; | ||
// we can safely pass away the data() pointer because | ||
// it's just referencing the memory inside the generator's promise | ||
// it won't be modified until the next call to _body.next() | ||
return {{ | ||
const_buffers_type{view.data(), view.size()}, | ||
_body.hasNext() // `true` if there are more buffers. | ||
}}; | ||
} catch (const std::exception& e) { | ||
ec = {EPIPE, boost::system::generic_category()}; | ||
LOG(ERROR) << "Failed to generate response:\n" << e.what() << '\n'; | ||
return boost::none; | ||
} | ||
} | ||
}; | ||
|
||
static_assert(boost::beast::http::is_body<streamable_body>::value, | ||
"Body type requirements not met"); | ||
|
||
} // namespace ad_utility::httpUtils::httpStreams |
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.
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.
Is this still needed by some legacy APIs, which are not
Server.h
?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.
The only remaining usage can be found here:
qlever/src/SparqlEngineMain.cpp
Lines 217 to 218 in 8a9465c