Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/soulomoon/python-throttle
Super naive python redis limiter
https://github.com/soulomoon/python-throttle
limiter python redis throttle
Last synced: 24 days ago
JSON representation
Super naive python redis limiter
- Host: GitHub
- URL: https://github.com/soulomoon/python-throttle
- Owner: soulomoon
- License: mit
- Created: 2018-03-10T15:53:07.000Z (over 6 years ago)
- Default Branch: develop
- Last Pushed: 2021-11-20T08:23:36.000Z (almost 3 years ago)
- Last Synced: 2024-10-04T03:19:56.825Z (about 1 month ago)
- Topics: limiter, python, redis, throttle
- Language: Python
- Homepage: https://github.com/soulomoon/python-throttle
- Size: 53.7 KB
- Stars: 8
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![python version](https://img.shields.io/pypi/pyversions/python-throttle.svg)](https://pypi.python.org/pypi/python-throttle)
![Build Status](https://travis-ci.org/soulomoon/python-throttle.svg?branch=develop)
[![codecov](https://codecov.io/gh/soulomoon/python-throttle/branch/develop/graph/badge.svg)](https://codecov.io/gh/soulomoon/python-throttle)
[![PyPI version](https://badge.fury.io/py/python-throttle.svg)](https://badge.fury.io/py/python-throttle)# python redis backed limiter
## sliding log or fixed window limiter
This module mainly offer two limiter
* FixedWindowLimiter
simply using redis incr, which about 10 times the speed of the sliding version but the limit is not smooth, may overflow a threshold size near the gap between two intervals
* SlidingWindowLimiter
using redis ordered set, slow but offers more smooth limit and more extendability## installation
`pip install python-throttle`## dummy example usage:
reminder: use name_space to avoid possible conflict on the same key
```python
import time
from limiter import FixedWindowLimiter
TEST_REDIS_CONFIG = {'host': 'localhost','port': 6379,'db': 10}
ip = "who are you?"
throttle = FixedWindowLimiter(threshold=2, interval=3, redis_config=TEST_REDIS_CONFIG, name_space="default")
print("first time, blocked?: {}".format(throttle.exceeded(ip)))
print("second time, blocked?: {}".format(throttle.exceeded(ip)))
print("now I block you, blocked?: {}".format(throttle.exceeded(ip)))
time.sleep(3)
print("refill energy, blocked?: {}".format(throttle.exceeded(ip)))
```
ouput:
```
first time blocked?: False
second time blocked?: False
now I block you blocked?: True
refill energy blocked?: False
```Reset the limiter:
```
throttle.reset()
```## dummy benchmark
It is from my unittest and not accurate,
```
rate_counter_pressure_test: SlidingRedisCounter
rate_counter_pressure_test: SlidingRedisCounter time count: 1.0075314449495636
rate_counter_pressure_test: FixedWindowRedisCounter
rate_counter_pressure_test: FixedWindowRedisCounter time count: 0.13711917598266155
```