JayBeams
0.1
Another project to have fun coding.
|
Holds a collection of HTTP request handlers and forwards requests. More...
#include <request_dispatcher.hpp>
Public Member Functions | |
request_dispatcher (std::string const &server_name) | |
Create a new empty dispatcher. More... | |
void | add_handler (std::string const &path, request_handler &&handler) |
Add a new handler. More... | |
response_type | process (request_type const &request) |
Process a new request using the right handler. More... | |
void | append_metrics (response_type &res) const |
Append the internal metrics to the body of res. More... | |
void | count_open_connection () |
Event counters. More... | |
long | get_open_connection () const |
Returns the count of open connections. More... | |
void | count_close_connection () |
Count a new connection closed. More... | |
long | get_close_connection () const |
Get the count of close connections. More... | |
void | count_read_ok () |
Count a successful read. More... | |
long | get_read_ok () const |
Get the count of successful reads. More... | |
void | count_read_error () |
Count a write errors. More... | |
long | get_read_error () const |
Get the count write errors. More... | |
long | get_write_invalid () const |
Get count of responses with invalid codes (outside the [100,600) range). More... | |
long | get_write_100 () const |
Get the count write in the 100 range. More... | |
long | get_write_200 () const |
Get the count write in the 200 range. More... | |
long | get_write_300 () const |
Get the count write in the 300 range. More... | |
long | get_write_400 () const |
Get the count write in the 400 range. More... | |
long | get_write_500 () const |
Get the count write in the 500 range. More... | |
void | count_write_ok () |
Count a write successes. More... | |
long | get_write_ok () const |
Get the count write successes. More... | |
void | count_write_error () |
Count a write errors. More... | |
long | get_write_error () const |
Get the count write errors. More... | |
void | count_accept_ok () |
Count accept successes. More... | |
long | get_accept_ok () const |
void | count_accept_error () |
Count accept errors. More... | |
long | get_accept_error () const |
void | count_accept_closed () |
Count accept requests on closed acceptors (rare) More... | |
long | get_accept_closed () const |
Private Member Functions | |
response_type | internal_error (request_type const &request) |
Create a 500 response. More... | |
response_type | not_found (request_type const &request) |
Create a 404 response. More... | |
std::pair< request_handler, bool > | find_handler (beast::string_view path) const |
Find the request handler for the given path. More... | |
void | update_response_counter (response_type const &res) |
Update the response code counters based on res. More... | |
void | count_write_invalid () |
Internally updated event counters. More... | |
void | count_write_100 () |
Count a write in the 100 range. More... | |
void | count_write_200 () |
Count a write in the 200 range. More... | |
void | count_write_300 () |
Count a write in the 300 range. More... | |
void | count_write_400 () |
Count a write in the 400 range. More... | |
void | count_write_500 () |
Count a write in the 500 range. More... | |
Private Attributes | |
std::mutex | mu_ |
Protect the critical sections. More... | |
std::map< std::string, request_handler > | handlers_ |
The collection of handlers. More... | |
std::string | server_name_ |
The name of the server returned in all HTTP responses. More... | |
std::atomic< long > | open_connection_ |
Multiple counters. More... | |
std::atomic< long > | close_connection_ |
std::atomic< long > | read_ok_ |
std::atomic< long > | read_error_ |
std::atomic< long > | write_invalid_ |
std::atomic< long > | write_100_ |
std::atomic< long > | write_200_ |
std::atomic< long > | write_300_ |
std::atomic< long > | write_400_ |
std::atomic< long > | write_500_ |
std::atomic< long > | write_ok_ |
std::atomic< long > | write_error_ |
std::atomic< long > | accept_ok_ |
std::atomic< long > | accept_error_ |
std::atomic< long > | accept_closed_ |
Holds a collection of HTTP request handlers and forwards requests.
Definition at line 28 of file request_dispatcher.hpp.
|
explicit |
Create a new empty dispatcher.
server_name | the content of the 'server' HTTP response header. |
Definition at line 9 of file request_dispatcher.cpp.
void jb::ehs::request_dispatcher::add_handler | ( | std::string const & | path, |
request_handler && | handler | ||
) |
Add a new handler.
path | the path that this handler is responsible for. |
handler | the handler called when a request hits a path. |
std::runtime_error | if the path already exists |
Definition at line 35 of file request_dispatcher.cpp.
References handlers_, and mu_.
Referenced by BOOST_AUTO_TEST_CASE().
void jb::ehs::request_dispatcher::append_metrics | ( | response_type & | res | ) | const |
Append the internal metrics to the body of res.
res | a http response where we will append the metrics. |
Definition at line 63 of file request_dispatcher.cpp.
References get_accept_closed(), get_accept_error(), get_accept_ok(), get_close_connection(), get_open_connection(), get_read_error(), get_read_ok(), get_write_100(), get_write_200(), get_write_300(), get_write_400(), get_write_500(), get_write_error(), get_write_invalid(), and get_write_ok().
Referenced by BOOST_AUTO_TEST_CASE(), and get_accept_closed().
|
inline |
Count accept requests on closed acceptors (rare)
Definition at line 163 of file request_dispatcher.hpp.
References accept_closed_.
|
inline |
Count accept errors.
Definition at line 155 of file request_dispatcher.hpp.
References accept_error_.
Referenced by BOOST_AUTO_TEST_CASE().
|
inline |
Count accept successes.
Definition at line 148 of file request_dispatcher.hpp.
References accept_ok_.
Referenced by BOOST_AUTO_TEST_CASE().
|
inline |
Count a new connection closed.
Definition at line 80 of file request_dispatcher.hpp.
References close_connection_.
Referenced by BOOST_AUTO_TEST_CASE().
|
inline |
Event counters.
A series of functions to count interesting events. TODO(coryan) this should really be a separate class that can count any number of metrics, including maps, histograms, rates, etc. Look at prometheus.io for inspiration.Count a new connection opened
Definition at line 72 of file request_dispatcher.hpp.
References open_connection_.
Referenced by BOOST_AUTO_TEST_CASE().
|
inline |
Count a write errors.
Definition at line 97 of file request_dispatcher.hpp.
References read_error_.
Referenced by BOOST_AUTO_TEST_CASE().
|
inline |
Count a successful read.
Definition at line 89 of file request_dispatcher.hpp.
References read_ok_.
Referenced by BOOST_AUTO_TEST_CASE().
|
inlineprivate |
Count a write in the 100 range.
Definition at line 223 of file request_dispatcher.hpp.
References write_100_.
Referenced by update_response_counter().
|
inlineprivate |
Count a write in the 200 range.
Definition at line 227 of file request_dispatcher.hpp.
References write_200_.
Referenced by update_response_counter().
|
inlineprivate |
Count a write in the 300 range.
Definition at line 231 of file request_dispatcher.hpp.
References write_300_.
Referenced by update_response_counter().
|
inlineprivate |
Count a write in the 400 range.
Definition at line 235 of file request_dispatcher.hpp.
References write_400_.
Referenced by update_response_counter().
|
inlineprivate |
Count a write in the 500 range.
Definition at line 239 of file request_dispatcher.hpp.
References write_500_.
Referenced by update_response_counter().
|
inline |
Count a write errors.
Definition at line 139 of file request_dispatcher.hpp.
References write_error_.
Referenced by BOOST_AUTO_TEST_CASE().
|
inlineprivate |
Internally updated event counters.
These event counters are only updated internally, the clients of the class can query the counters, but not update them.Count responses with invalid codes (outside the [100,600) range).
Definition at line 219 of file request_dispatcher.hpp.
References write_invalid_.
Referenced by update_response_counter().
|
inline |
Count a write successes.
Definition at line 131 of file request_dispatcher.hpp.
References write_ok_.
Referenced by BOOST_AUTO_TEST_CASE().
|
private |
Find the request handler for the given path.
path | the request path |
Definition at line 137 of file request_dispatcher.cpp.
References handlers_, and mu_.
Referenced by get_accept_closed(), and process().
|
inline |
Definition at line 166 of file request_dispatcher.hpp.
References accept_closed_, append_metrics(), find_handler(), internal_error(), not_found(), and update_response_counter().
Referenced by append_metrics().
|
inline |
Definition at line 158 of file request_dispatcher.hpp.
References accept_error_.
Referenced by append_metrics(), and BOOST_AUTO_TEST_CASE().
|
inline |
Definition at line 151 of file request_dispatcher.hpp.
References accept_ok_.
Referenced by append_metrics(), and BOOST_AUTO_TEST_CASE().
|
inline |
Get the count of close connections.
Definition at line 84 of file request_dispatcher.hpp.
References close_connection_.
Referenced by append_metrics(), and BOOST_AUTO_TEST_CASE().
|
inline |
Returns the count of open connections.
Definition at line 76 of file request_dispatcher.hpp.
References open_connection_.
Referenced by append_metrics(), and BOOST_AUTO_TEST_CASE().
|
inline |
Get the count write errors.
Definition at line 101 of file request_dispatcher.hpp.
References read_error_.
Referenced by append_metrics(), and BOOST_AUTO_TEST_CASE().
|
inline |
Get the count of successful reads.
Definition at line 93 of file request_dispatcher.hpp.
References read_ok_.
Referenced by append_metrics(), and BOOST_AUTO_TEST_CASE().
|
inline |
Get the count write in the 100 range.
Definition at line 111 of file request_dispatcher.hpp.
References write_100_.
Referenced by append_metrics(), and BOOST_AUTO_TEST_CASE().
|
inline |
Get the count write in the 200 range.
Definition at line 115 of file request_dispatcher.hpp.
References write_200_.
Referenced by append_metrics(), and BOOST_AUTO_TEST_CASE().
|
inline |
Get the count write in the 300 range.
Definition at line 119 of file request_dispatcher.hpp.
References write_300_.
Referenced by append_metrics(), and BOOST_AUTO_TEST_CASE().
|
inline |
Get the count write in the 400 range.
Definition at line 123 of file request_dispatcher.hpp.
References write_400_.
Referenced by append_metrics(), and BOOST_AUTO_TEST_CASE().
|
inline |
Get the count write in the 500 range.
Definition at line 127 of file request_dispatcher.hpp.
References write_500_.
Referenced by append_metrics(), and BOOST_AUTO_TEST_CASE().
|
inline |
Get the count write errors.
Definition at line 143 of file request_dispatcher.hpp.
References write_error_.
Referenced by append_metrics(), and BOOST_AUTO_TEST_CASE().
|
inline |
Get count of responses with invalid codes (outside the [100,600) range).
Definition at line 106 of file request_dispatcher.hpp.
References write_invalid_.
Referenced by append_metrics(), and BOOST_AUTO_TEST_CASE().
|
inline |
Get the count write successes.
Definition at line 135 of file request_dispatcher.hpp.
References write_ok_.
Referenced by append_metrics(), and BOOST_AUTO_TEST_CASE().
|
private |
Create a 500 response.
request | the request that triggered this response |
Definition at line 113 of file request_dispatcher.cpp.
References server_name_, and update_response_counter().
Referenced by get_accept_closed(), and process().
|
private |
Create a 404 response.
request | the request that triggered this response |
Definition at line 124 of file request_dispatcher.cpp.
References server_name_, and update_response_counter().
Referenced by get_accept_closed(), and process().
response_type jb::ehs::request_dispatcher::process | ( | request_type const & | request | ) |
Process a new request using the right handler.
Returns the response to send back to the client. The handler typically creates a normal 200 response, but other responses can be created by the client. The dispatcher automatically creates 404 responses if the request path does not have a handler registered, and 500 responses if there is an exception handling the request.
request | the HTTP request to process |
Definition at line 44 of file request_dispatcher.cpp.
References find_handler(), jb::info, internal_error(), JB_LOG, not_found(), server_name_, and update_response_counter().
Referenced by BOOST_AUTO_TEST_CASE().
|
private |
Update the response code counters based on res.
Definition at line 146 of file request_dispatcher.cpp.
References count_write_100(), count_write_200(), count_write_300(), count_write_400(), count_write_500(), and count_write_invalid().
Referenced by get_accept_closed(), internal_error(), not_found(), and process().
|
private |
Definition at line 269 of file request_dispatcher.hpp.
Referenced by count_accept_closed(), and get_accept_closed().
|
private |
Definition at line 268 of file request_dispatcher.hpp.
Referenced by count_accept_error(), and get_accept_error().
|
private |
Definition at line 267 of file request_dispatcher.hpp.
Referenced by count_accept_ok(), and get_accept_ok().
|
private |
Definition at line 256 of file request_dispatcher.hpp.
Referenced by count_close_connection(), and get_close_connection().
|
private |
The collection of handlers.
Definition at line 249 of file request_dispatcher.hpp.
Referenced by add_handler(), and find_handler().
|
mutableprivate |
Protect the critical sections.
Definition at line 246 of file request_dispatcher.hpp.
Referenced by add_handler(), and find_handler().
|
private |
Multiple counters.
Definition at line 255 of file request_dispatcher.hpp.
Referenced by count_open_connection(), and get_open_connection().
|
private |
Definition at line 258 of file request_dispatcher.hpp.
Referenced by count_read_error(), and get_read_error().
|
private |
Definition at line 257 of file request_dispatcher.hpp.
Referenced by count_read_ok(), and get_read_ok().
|
private |
The name of the server returned in all HTTP responses.
Definition at line 252 of file request_dispatcher.hpp.
Referenced by internal_error(), not_found(), and process().
|
private |
Definition at line 260 of file request_dispatcher.hpp.
Referenced by count_write_100(), and get_write_100().
|
private |
Definition at line 261 of file request_dispatcher.hpp.
Referenced by count_write_200(), and get_write_200().
|
private |
Definition at line 262 of file request_dispatcher.hpp.
Referenced by count_write_300(), and get_write_300().
|
private |
Definition at line 263 of file request_dispatcher.hpp.
Referenced by count_write_400(), and get_write_400().
|
private |
Definition at line 264 of file request_dispatcher.hpp.
Referenced by count_write_500(), and get_write_500().
|
private |
Definition at line 266 of file request_dispatcher.hpp.
Referenced by count_write_error(), and get_write_error().
|
private |
Definition at line 259 of file request_dispatcher.hpp.
Referenced by count_write_invalid(), and get_write_invalid().
|
private |
Definition at line 265 of file request_dispatcher.hpp.
Referenced by count_write_ok(), and get_write_ok().