drogon
C++14/17-based HTTP application framework
Loading...
Searching...
No Matches
drogon::plugin::Hodor Class Reference

The Hodor plugin implements a global rate limiter that limits the number of requests in a particular time unit. The json configuration is as follows: More...

#include <drogon/plugins/Hodor.h>

Inheritance diagram for drogon::plugin::Hodor:
Collaboration diagram for drogon::plugin::Hodor:

Public Member Functions

void initAndStart (const Json::Value &config) override
void shutdown () override
void setUserIdGetter (std::function< std::optional< std::string >(const HttpRequestPtr &)> func)
 the method is used to set a function to get the user id from the request. users should call this method after calling the app().run() method. etc. use the beginning advice of AOP.
void setRejectResponseFactory (std::function< HttpResponsePtr(const HttpRequestPtr &)> func)
 the method is used to set a function to create the response when the rate limit is exceeded. users should call this method after calling the app().run() method. etc. use the beginning advice of AOP.
Public Member Functions inherited from drogon::PluginBase
void initialize ()
 This method must be called by drogon.
Public Member Functions inherited from drogon::DrObject< Hodor >
const std::string & className () const override
 Get the class name.
bool isClass (const std::string &class_name) const override
 Return true if the class name is 'class_name'.

Additional Inherited Members

Static Public Member Functions inherited from drogon::DrObject< Hodor >
static const std::string & classTypeName ()

Detailed Description

The Hodor plugin implements a global rate limiter that limits the number of requests in a particular time unit. The json configuration is as follows:

{
"name": "drogon::plugin::Hodor",
"dependencies": [],
"config": {
// The algorithm used to limit the number of requests.
// The default value is "token_bucket". other values are "fixed_window"
or "sliding_window".
"algorithm": "token_bucket",
// a regular expression (for matching the path of a request) list for
URLs that have to be limited. if the list is empty, all URLs are limited.
"urls": ["^/api/.*", ...],
// The time unit in seconds. the default value is 60.
"time_unit": 60,
// The maximum number of requests in a time unit. the default value 0
means no limit.
"capacity": 0,
// The maximum number of requests in a time unit for a single IP. the
default value 0 means no limit.
"ip_capacity": 0,
// The maximum number of requests in a time unit for a single user.
a function must be provided to the plugin to get the user id from the request.
the default value 0 means no limit.
"user_capacity": 0,
// Use the RealIpResolver plugin to get the real IP address of the
request. if this option is true, the RealIpResolver plugin should be added to
the dependencies list. the default value is false.
"use_real_ip_resolver": false,
// Multiple threads mode: the default value is true. if this option is
true, some mutexes are used for thread-safe.
"multi_threads": true,
// The message body of the response when the request is rejected.
"rejection_message": "Too many requests",
// In seconds, the minimum expiration time of the limiters for different
IPs or users. the default value is 600.
"limiter_expire_time": 600,
"sub_limits": [
{
"urls": ["^/api/1/.*", ...],
"capacity": 0,
"ip_capacity": 0,
"user_capacity": 0
},...
],
// Trusted proxy ip or cidr
"trust_ips": ["127.0.0.1", "172.16.0.0/12"],
}
}
This plugin is used to resolve client real ip from HTTP request.
Definition RealIpResolver.h:45

Enable the plugin by adding the configuration to the list of plugins in the configuration file.

Member Function Documentation

◆ initAndStart()

void drogon::plugin::Hodor::initAndStart ( const Json::Value & config)
overridevirtual

This method must be called by drogon to initialize and start the plugin. It must be implemented by the user.

Implements drogon::PluginBase.

◆ shutdown()

void drogon::plugin::Hodor::shutdown ( )
overridevirtual

This method must be called by drogon to shutdown the plugin. It must be implemented by the user.

Implements drogon::PluginBase.


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