Skip to content

Commit

Permalink
Add new virtual method to ignore request bodies
Browse files Browse the repository at this point in the history
A new virtual method could be reimplemented to
ignore the request body storage under some
conditions, in order to speed up the performance
with high load.

Issue: #4
  • Loading branch information
testillano committed Jul 9, 2022
1 parent 8720344 commit de1112e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
22 changes: 18 additions & 4 deletions include/ert/http2comm/Http2Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,30 @@ class Http2Server
*/
virtual bool checkHeaders(const nghttp2::asio_http2::server::request& req) = 0;

/**
* As possible optimization, our server could ignore the request body, so nghttp2 'on_data'
* could be lightened skipping the request body copy into internal stringstream. This is
* useful when huge requests are received and they are not actually needed (for example,
* a dummy server could mock valid static responses regardless the content received).
*
* @param req nghttp2-asio request structure.
*
* @return Return the boolean about ignoring request body copy. Default implementation returns 'false'.
*/
virtual bool ignoreRequestBody(const nghttp2::asio_http2::server::request& req) const {
return false;
}

/**
* Virtual reception callback. Implementation is mandatory.
*
* @param req nghttp2-asio request structure.
* @param requestBody request body received.
* @param receptionTimestampUs microseconds timestamp of reception.
* @param statusCode response status code to be filled by reference.
* @param headers reponse headers to be filled by reference.
* @param headers response headers to be filled by reference.
* @param responseBody response body to be filled by reference.
* @param responseDelayMs reponse delay in milliseconds to be filled by reference.
* @param responseDelayMs response delay in milliseconds to be filled by reference.
*/
virtual void receive(const nghttp2::asio_http2::server::request& req,
std::shared_ptr<std::stringstream> requestBody,
Expand All @@ -220,9 +234,9 @@ class Http2Server
* </pre>
*
* @param req nghttp2-asio request structure.
* @param requestBody request body received.
* @param requestBody request body received (not used in default implementation, and probably never used).
* @param statusCode response status code to be filled by reference.
* @param headers reponse headers to be filled by reference.
* @param headers response headers to be filled by reference.
* @param responseBody response body to be filled by reference.
* @param error error pair given by tuple code/description, i.e.: <415,'UNSUPPORTED_MEDIA_TYPE'>.
* If no description provided, empty json document ({}) is sent in the default implementation.
Expand Down
5 changes: 5 additions & 0 deletions include/ert/http2comm/Stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ class Stream : public std::enable_shared_from_this<Stream>
~Stream() = default;
Stream& operator=(const Stream&) = delete;

// nghttp2-asio request structure
const nghttp2::asio_http2::server::request& getReq() const {
return req_;
}

// Process reception
void process();

Expand Down
7 changes: 6 additions & 1 deletion src/Http2Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,12 @@ nghttp2::asio_http2::server::request_cb Http2Server::handler()
{
if (len > 0)
{
std::copy(data, data + len, std::ostream_iterator<uint8_t>(*requestBody));
if (!ignoreRequestBody(stream->getReq())) {
// https://github.com/testillano/h2agent/issues/6 is caused when this is enabled, on high load and broke client connections:
// (mutexes does not solves the problem, and does not matter is shared_ptr requestBody is replaced by static type like
// stringstream; it seems that data is not correctly protected on lower layers)
std::copy(data, data + len, std::ostream_iterator<uint8_t>(*requestBody));
}
}
else
{
Expand Down

0 comments on commit de1112e

Please sign in to comment.