In cowboy, is there a way to do something before cowboy_router decides to dispatch the request to the appropriate request handler? The scenario I'm thinking of is that the node is overloaded with requests -- so I would rather the node return a 503 quickly than risk going down entirely.
So far, I'm doing it as the first processing step in the http request handler.
For instance, in the example on the user reference, there is no hook available before routing. So a 503 response has to be returned in each of the handlers. I think it might be cleaner (and faster) if there is a way to attach a pre-dispatch hook that would check if the node is overloaded and return a quick response.
Dispatch = cowboy_router:compile([
%% {HostMatch, list({PathMatch, Handler, InitialState})}
{'_', [
{"/one", my_handler1, #{}},
{"/two", my_handler2, #{}},
{"/three", my_handler3, #{}},
{"/four", my_handler4, #{}},
]}
]),
%% Name, TransOpts, ProtoOpts
cowboy:start_clear(my_http_listener,
[{port, 8080}],
#{env => #{dispatch => Dispatch}}
).