diff options
| author | Stefan Boberg <[email protected]> | 2021-10-06 13:59:18 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-10-06 13:59:18 +0200 |
| commit | fa48ebf89e06edc9d3bdd26b119417df20902bdd (patch) | |
| tree | 2ea8c3e06282ff537d5985b94f8dc129bd60e9e8 /zenserver/testing | |
| parent | Added option to specify path to logfile. (diff) | |
| download | zen-fa48ebf89e06edc9d3bdd26b119417df20902bdd.tar.xz zen-fa48ebf89e06edc9d3bdd26b119417df20902bdd.zip | |
Support for asynchronous HTTP response processing (#19)
This change introduces WriteResponseAsync which can be used to move potentially slow request handler code (like upstream lookups) off the I/O service thread to ensure we are always able to serve as many HTTP requests as possible. The current implementation defaults to 16 async worker threads and there is currently no back-pressure.
- Added RequestStats - Metrics for network requests. Aggregates tracking of duration, payload sizes into a single class for ease of use
- Added some metrics on upstream communication
Co-authored-by: Per Larsson <[email protected]>
Diffstat (limited to 'zenserver/testing')
| -rw-r--r-- | zenserver/testing/httptest.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/zenserver/testing/httptest.cpp b/zenserver/testing/httptest.cpp index 01866a63b..924546762 100644 --- a/zenserver/testing/httptest.cpp +++ b/zenserver/testing/httptest.cpp @@ -4,9 +4,12 @@ #include <zencore/compactbinarybuilder.h> #include <zencore/compactbinarypackage.h> +#include <zencore/timer.h> namespace zen { +using namespace fmt::literals; + HttpTestingService::HttpTestingService() { m_Router.RegisterRoute( @@ -15,6 +18,44 @@ HttpTestingService::HttpTestingService() HttpVerb::kGet); m_Router.RegisterRoute( + "hello_slow", + [this](HttpRouterRequest& Req) { + Req.ServerRequest().WriteResponseAsync([this](HttpServerRequest& Request) { + Stopwatch Timer; + Sleep(1000); + Request.WriteResponse(HttpResponseCode::OK, + HttpContentType::kText, + "hello, took me {}"_format(NiceTimeSpanMs(Timer.GetElapsedTimeMs()))); + }); + }, + HttpVerb::kGet); + + m_Router.RegisterRoute( + "hello_veryslow", + [this](HttpRouterRequest& Req) { + Req.ServerRequest().WriteResponseAsync([this](HttpServerRequest& Request) { + Stopwatch Timer; + Sleep(60000); + Request.WriteResponse(HttpResponseCode::OK, + HttpContentType::kText, + "hello, took me {}"_format(NiceTimeSpanMs(Timer.GetElapsedTimeMs()))); + }); + }, + HttpVerb::kGet); + + m_Router.RegisterRoute( + "hello_throw", + [this](HttpRouterRequest& Req) { + Req.ServerRequest().WriteResponseAsync([this](HttpServerRequest&) { throw std::runtime_error("intentional error"); }); + }, + HttpVerb::kGet); + + m_Router.RegisterRoute( + "hello_noresponse", + [this](HttpRouterRequest& Req) { Req.ServerRequest().WriteResponseAsync([this](HttpServerRequest&) {}); }, + HttpVerb::kGet); + + m_Router.RegisterRoute( "metrics", [this](HttpRouterRequest& Req) { metrics::OperationTiming::Scope _(m_TimingStats); |