JayBeams  0.1
Another project to have fun coding.
Public Member Functions | Private Member Functions | Private Attributes | List of all members
jb::ehs::request_dispatcher Class Reference

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_handlerhandlers_
 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_
 

Detailed Description

Holds a collection of HTTP request handlers and forwards requests.

Definition at line 28 of file request_dispatcher.hpp.

Constructor & Destructor Documentation

◆ request_dispatcher()

jb::ehs::request_dispatcher::request_dispatcher ( std::string const &  server_name)
explicit

Create a new empty dispatcher.

Parameters
server_namethe content of the 'server' HTTP response header.

Definition at line 9 of file request_dispatcher.cpp.

Member Function Documentation

◆ add_handler()

void jb::ehs::request_dispatcher::add_handler ( std::string const &  path,
request_handler &&  handler 
)

Add a new handler.

Parameters
paththe path that this handler is responsible for.
handlerthe handler called when a request hits a path.
Exceptions
std::runtime_errorif the path already exists

Definition at line 35 of file request_dispatcher.cpp.

References handlers_, and mu_.

Referenced by BOOST_AUTO_TEST_CASE().

◆ append_metrics()

void jb::ehs::request_dispatcher::append_metrics ( response_type res) const

Append the internal metrics to the body of res.

Parameters
resa 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().

◆ count_accept_closed()

void jb::ehs::request_dispatcher::count_accept_closed ( )
inline

Count accept requests on closed acceptors (rare)

Definition at line 163 of file request_dispatcher.hpp.

References accept_closed_.

◆ count_accept_error()

void jb::ehs::request_dispatcher::count_accept_error ( )
inline

Count accept errors.

Definition at line 155 of file request_dispatcher.hpp.

References accept_error_.

Referenced by BOOST_AUTO_TEST_CASE().

◆ count_accept_ok()

void jb::ehs::request_dispatcher::count_accept_ok ( )
inline

Count accept successes.

Definition at line 148 of file request_dispatcher.hpp.

References accept_ok_.

Referenced by BOOST_AUTO_TEST_CASE().

◆ count_close_connection()

void jb::ehs::request_dispatcher::count_close_connection ( )
inline

Count a new connection closed.

Definition at line 80 of file request_dispatcher.hpp.

References close_connection_.

Referenced by BOOST_AUTO_TEST_CASE().

◆ count_open_connection()

void jb::ehs::request_dispatcher::count_open_connection ( )
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().

◆ count_read_error()

void jb::ehs::request_dispatcher::count_read_error ( )
inline

Count a write errors.

Definition at line 97 of file request_dispatcher.hpp.

References read_error_.

Referenced by BOOST_AUTO_TEST_CASE().

◆ count_read_ok()

void jb::ehs::request_dispatcher::count_read_ok ( )
inline

Count a successful read.

Definition at line 89 of file request_dispatcher.hpp.

References read_ok_.

Referenced by BOOST_AUTO_TEST_CASE().

◆ count_write_100()

void jb::ehs::request_dispatcher::count_write_100 ( )
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().

◆ count_write_200()

void jb::ehs::request_dispatcher::count_write_200 ( )
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().

◆ count_write_300()

void jb::ehs::request_dispatcher::count_write_300 ( )
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().

◆ count_write_400()

void jb::ehs::request_dispatcher::count_write_400 ( )
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().

◆ count_write_500()

void jb::ehs::request_dispatcher::count_write_500 ( )
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().

◆ count_write_error()

void jb::ehs::request_dispatcher::count_write_error ( )
inline

Count a write errors.

Definition at line 139 of file request_dispatcher.hpp.

References write_error_.

Referenced by BOOST_AUTO_TEST_CASE().

◆ count_write_invalid()

void jb::ehs::request_dispatcher::count_write_invalid ( )
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().

◆ count_write_ok()

void jb::ehs::request_dispatcher::count_write_ok ( )
inline

Count a write successes.

Definition at line 131 of file request_dispatcher.hpp.

References write_ok_.

Referenced by BOOST_AUTO_TEST_CASE().

◆ find_handler()

std::pair< request_handler, bool > jb::ehs::request_dispatcher::find_handler ( beast::string_view  path) const
private

Find the request handler for the given path.

Parameters
paththe request path
Returns
a pair with the request handler (if any) and a boolean. The boolean is set to false if the path was not found.

Definition at line 137 of file request_dispatcher.cpp.

References handlers_, and mu_.

Referenced by get_accept_closed(), and process().

◆ get_accept_closed()

long jb::ehs::request_dispatcher::get_accept_closed ( ) const
inline

◆ get_accept_error()

long jb::ehs::request_dispatcher::get_accept_error ( ) const
inline

Definition at line 158 of file request_dispatcher.hpp.

References accept_error_.

Referenced by append_metrics(), and BOOST_AUTO_TEST_CASE().

◆ get_accept_ok()

long jb::ehs::request_dispatcher::get_accept_ok ( ) const
inline

Definition at line 151 of file request_dispatcher.hpp.

References accept_ok_.

Referenced by append_metrics(), and BOOST_AUTO_TEST_CASE().

◆ get_close_connection()

long jb::ehs::request_dispatcher::get_close_connection ( ) const
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().

◆ get_open_connection()

long jb::ehs::request_dispatcher::get_open_connection ( ) const
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().

◆ get_read_error()

long jb::ehs::request_dispatcher::get_read_error ( ) const
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().

◆ get_read_ok()

long jb::ehs::request_dispatcher::get_read_ok ( ) const
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().

◆ get_write_100()

long jb::ehs::request_dispatcher::get_write_100 ( ) const
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().

◆ get_write_200()

long jb::ehs::request_dispatcher::get_write_200 ( ) const
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().

◆ get_write_300()

long jb::ehs::request_dispatcher::get_write_300 ( ) const
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().

◆ get_write_400()

long jb::ehs::request_dispatcher::get_write_400 ( ) const
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().

◆ get_write_500()

long jb::ehs::request_dispatcher::get_write_500 ( ) const
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().

◆ get_write_error()

long jb::ehs::request_dispatcher::get_write_error ( ) const
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().

◆ get_write_invalid()

long jb::ehs::request_dispatcher::get_write_invalid ( ) const
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().

◆ get_write_ok()

long jb::ehs::request_dispatcher::get_write_ok ( ) const
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().

◆ internal_error()

response_type jb::ehs::request_dispatcher::internal_error ( request_type const &  request)
private

Create a 500 response.

Parameters
requestthe request that triggered this response
Returns
a 500 HTTP response formatted to match the version of the request.

Definition at line 113 of file request_dispatcher.cpp.

References server_name_, and update_response_counter().

Referenced by get_accept_closed(), and process().

◆ not_found()

response_type jb::ehs::request_dispatcher::not_found ( request_type const &  request)
private

Create a 404 response.

Parameters
requestthe request that triggered this response
Returns
a 404 HTTP response formatted to match the version of the request.

Definition at line 124 of file request_dispatcher.cpp.

References server_name_, and update_response_counter().

Referenced by get_accept_closed(), and process().

◆ 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.

Parameters
requestthe HTTP request to process
Returns
a HTTP response to send back to the client.

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().

◆ update_response_counter()

void jb::ehs::request_dispatcher::update_response_counter ( response_type const &  res)
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().

Member Data Documentation

◆ accept_closed_

std::atomic<long> jb::ehs::request_dispatcher::accept_closed_
private

Definition at line 269 of file request_dispatcher.hpp.

Referenced by count_accept_closed(), and get_accept_closed().

◆ accept_error_

std::atomic<long> jb::ehs::request_dispatcher::accept_error_
private

Definition at line 268 of file request_dispatcher.hpp.

Referenced by count_accept_error(), and get_accept_error().

◆ accept_ok_

std::atomic<long> jb::ehs::request_dispatcher::accept_ok_
private

Definition at line 267 of file request_dispatcher.hpp.

Referenced by count_accept_ok(), and get_accept_ok().

◆ close_connection_

std::atomic<long> jb::ehs::request_dispatcher::close_connection_
private

Definition at line 256 of file request_dispatcher.hpp.

Referenced by count_close_connection(), and get_close_connection().

◆ handlers_

std::map<std::string, request_handler> jb::ehs::request_dispatcher::handlers_
private

The collection of handlers.

Definition at line 249 of file request_dispatcher.hpp.

Referenced by add_handler(), and find_handler().

◆ mu_

std::mutex jb::ehs::request_dispatcher::mu_
mutableprivate

Protect the critical sections.

Definition at line 246 of file request_dispatcher.hpp.

Referenced by add_handler(), and find_handler().

◆ open_connection_

std::atomic<long> jb::ehs::request_dispatcher::open_connection_
private

Multiple counters.

Definition at line 255 of file request_dispatcher.hpp.

Referenced by count_open_connection(), and get_open_connection().

◆ read_error_

std::atomic<long> jb::ehs::request_dispatcher::read_error_
private

Definition at line 258 of file request_dispatcher.hpp.

Referenced by count_read_error(), and get_read_error().

◆ read_ok_

std::atomic<long> jb::ehs::request_dispatcher::read_ok_
private

Definition at line 257 of file request_dispatcher.hpp.

Referenced by count_read_ok(), and get_read_ok().

◆ server_name_

std::string jb::ehs::request_dispatcher::server_name_
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().

◆ write_100_

std::atomic<long> jb::ehs::request_dispatcher::write_100_
private

Definition at line 260 of file request_dispatcher.hpp.

Referenced by count_write_100(), and get_write_100().

◆ write_200_

std::atomic<long> jb::ehs::request_dispatcher::write_200_
private

Definition at line 261 of file request_dispatcher.hpp.

Referenced by count_write_200(), and get_write_200().

◆ write_300_

std::atomic<long> jb::ehs::request_dispatcher::write_300_
private

Definition at line 262 of file request_dispatcher.hpp.

Referenced by count_write_300(), and get_write_300().

◆ write_400_

std::atomic<long> jb::ehs::request_dispatcher::write_400_
private

Definition at line 263 of file request_dispatcher.hpp.

Referenced by count_write_400(), and get_write_400().

◆ write_500_

std::atomic<long> jb::ehs::request_dispatcher::write_500_
private

Definition at line 264 of file request_dispatcher.hpp.

Referenced by count_write_500(), and get_write_500().

◆ write_error_

std::atomic<long> jb::ehs::request_dispatcher::write_error_
private

Definition at line 266 of file request_dispatcher.hpp.

Referenced by count_write_error(), and get_write_error().

◆ write_invalid_

std::atomic<long> jb::ehs::request_dispatcher::write_invalid_
private

Definition at line 259 of file request_dispatcher.hpp.

Referenced by count_write_invalid(), and get_write_invalid().

◆ write_ok_

std::atomic<long> jb::ehs::request_dispatcher::write_ok_
private

Definition at line 265 of file request_dispatcher.hpp.

Referenced by count_write_ok(), and get_write_ok().


The documentation for this class was generated from the following files: