Multi-rate throttle for Django Rest Framework
Project description
MULTIRATE THROTTLING
This module provides a multirate parent throttle for Django Rest Framework (DRF) with optional support for Constance.
The multirate settings prevents from adding several throttle classes for each rate when the scope could be the same.
The use of Constance allows the dynamic change of the throttling rates on the fly.
It supports the use of a white list of IP addresses that are not subject to the throttle.
Dependencies
- Django
- Django Rest Framework
- Constance (Optionally)
Installation
pip install multirate-throttling
Usage
The list of rates for a scope can we defined as a comma-separated list of rates.
A single rate can be defined as a "number/time_unit" where 'number' if the maximum number of requests in the specified 'unit time': 5/day
The unit time can be one of the following: sec, min, hour, day, week. Actually, only the first letter of the unit matters, so you can also use: s, m, h, d, w
Example of a valid list of rates for a scope: "15/s, 20/day, 50/we"
NOTES:
- Should a time unit be repeated, only the last occurrence would be applied.
- Should a rate in the list be wrong, that rate will be ignored and its value logged.
Settings
You can store each scope's settings in two places:
- As a standard DRF throttle, adding the scope and rate list to the 'DEFAULT_THROTTLE_RATES' key of 'REST_FRAMEWORK' in Django settings
- In Constance, prepending 'THROTTLE_RATE_' to the scope's name in UPPER CASE and adding it to the Constance config as a key with the rate list as its value. Later, you will be able to change the rate list on the fly at any time.
As the usage of Constance is optional, you must add the following variable to Django Settings to enable it:
MULTIRATE_THROTTLING_USE_CONSTANCE = True
Examples
- As a DRF standard throttle setting:
REST_FRAMEWORK = {
'THROTTLE_RATE_WHITE_LIST': ['127.0.0.1', ],
'DEFAULT_THROTTLE_RATES': {
'some_scope': '5/sec, 50/min, 100/hour, 500/day, 100/week',
}
}
- As a Constance config:
MULTIRATE_THROTTLING_USE_CONSTANCE = True
CONSTANCE_CONFIG = {
'THROTTLE_RATE_WHITE_LIST': '127.0.0.1, 127.0.0.2',
'THROTTLE_RATE_SOME_SCOPE': '5/sec, 50/min, 100/hour, 500/day, 100/week',
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file multirate_throttling-0.1.3.tar.gz
.
File metadata
- Download URL: multirate_throttling-0.1.3.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1df077f849084fdc2f2a4040223706fea68c84a637940c93a15aad375e36fa13 |
|
MD5 | 6a2ec64e899e610f7ba5fef911a92cf3 |
|
BLAKE2b-256 | f58808de9c24c04042c8955b872a962c672455f14bc38a10befd919de163b265 |
File details
Details for the file multirate_throttling-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: multirate_throttling-0.1.3-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3aa369b68585d346f2fa96a846b599fc9246e04800025a7dcc6a879ee392af27 |
|
MD5 | 24a4bb4c59b866942030e3ef842bc57d |
|
BLAKE2b-256 | fd0b99542290717af595af3da11ccd4e4e480dd17e667c5186751406588d79ca |