Configuration#
Using Flask Config#
The following Flask Configuration values are honored by
Limiter
. If the corresponding configuration value is also present
as an argument to the Limiter
constructor, the constructor argument will
take priority.
Constructor argument: |
Overall kill switch for rate limits. Defaults to |
Constructor argument: |
A callable that returns the domain to rate limit (e.g. username, ip address etc) |
Constructor argument: |
Prefix that is prepended to each stored rate limit key and app context
global name. This can be useful when using a shared storage for multiple
applications or rate limit domains. For multi-instance use cases, explicitly
pass |
Constructor argument: |
A comma (or some other delimiter) separated string that will be used to apply limits to the application as a whole (i.e. shared by all routes). |
Constructor argument: |
The cost of a hit to the application wide shared limit as an integer or a function that takes no parameters and returns the cost as an integer (Default: 1) |
Constructor argument: |
A comma (or some other delimiter) separated string that will be used to
apply a default limit on all routes. If not provided, the default limits can be
passed to the |
Constructor argument: |
Whether default limits are applied per method, per route or as a combination of all method per route. |
Constructor argument: |
The cost of a hit to the default limits as an integer or a function that takes no parameters and returns the cost as an integer (Default: 1) |
Constructor argument: |
A function that should return a truthy value if the default rate limit(s)
should be skipped for the current request. This callback is called from the
flask request context |
Constructor argument: |
A function that should return a truthy value if a deduction should be made
from the default rate limit(s) for the current request. This callback is called
from the flask request context |
Constructor argument: |
A storage location conforming to the scheme in storage-scheme.
A basic in-memory storage can be used by specifying
Other supported backends include:
For specific examples and requirements of supported backends please refer to storage-scheme and the limits library. |
Constructor argument: |
A dictionary to set extra options to be passed to the storage implementation upon initialization. |
Constructor argument: |
The rate limiting strategy to use. Rate limiting strategies for details. |
Constructor argument: |
Enables returning Rate-limiting Headers. Defaults to |
Constructor argument: |
Header for the current rate limit. Defaults to |
Constructor argument: |
Header for the reset time of the current rate limit. Defaults to |
Constructor argument: |
Header for the number of requests remaining in the current rate limit. Defaults to |
Constructor argument: |
Header for when the client should retry the request. Defaults to |
Constructor argument: |
Allows configuration of how the value of the |
Constructor argument: |
Whether to allow failures while attempting to perform a rate limit
such as errors with downstream storage. Setting this value to |
Constructor argument: |
|
Constructor argument: |
A comma (or some other delimiter) separated string that will be used when the configured storage is down. |
Constructor argument: |
Whether to stop processing remaining limits after the first breach.
Default to |
Constructor argument: |
A function that will be called when any limit in this extension is breached. |
Rate limit string notation#
Rate limits are specified as strings following the format:
[count] [per|/] [n (optional)] [second|minute|hour|day|month|year][s]
You can combine multiple rate limits by separating them with a delimiter of your choice.
Examples#
10 per hour
10 per 2 hours
10/hour
5/2 seconds;10/hour;100/day;2000 per year
100/day, 500/7 days
Warning
If rate limit strings that are provided to the limit()
decorator are malformed and can’t be parsed the decorated route will fall back
to the default rate limit(s) and an ERROR
log message will be emitted. Refer
to Logging for more details on capturing this information. Malformed
default rate limit strings will however raise an exception as they are evaluated
early enough to not cause disruption to a running application.
Rate-limiting Headers#
If the configuration is enabled, information about the rate limit with respect to the route being requested will be added to the response headers. Since multiple rate limits can be active for a given route - the rate limit with the lowest time granularity will be used in the scenario when the request does not breach any rate limits.
|
The total number of requests allowed for the active window |
|
The number of requests remaining in the active window. |
|
UTC seconds since epoch when the window will be reset. |
|
Seconds to retry after or the http date when the
Rate Limit will be reset. The way the value is presented
depends on the configuration value set in |
The header names can be customised if required by either using the flask configuration (
RATELIMIT_HEADER_LIMIT
,
RATELIMIT_HEADER_RESET
,
RATELIMIT_HEADER_RETRY_AFTER
,
RATELIMIT_HEADER_REMAINING
)
values or by providing the header_name_mapping
argument
to the extension constructor as follows:
from flask_limiter import Limiter, HEADERS
limiter = Limiter(header_name_mapping={
HEADERS.LIMIT : "X-My-Limit",
HEADERS.RESET : "X-My-Reset",
HEADERS.REMAINING: "X-My-Remaining"
}
)