Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dexter2206/choke
Choke - Python package implementing Redis-driven throttling
https://github.com/dexter2206/choke
Last synced: 23 days ago
JSON representation
Choke - Python package implementing Redis-driven throttling
- Host: GitHub
- URL: https://github.com/dexter2206/choke
- Owner: dexter2206
- License: mit
- Created: 2019-03-07T12:55:41.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-05-28T19:01:21.000Z (over 5 years ago)
- Last Synced: 2024-10-25T23:57:58.086Z (2 months ago)
- Language: Python
- Size: 17.6 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
README
choke: simple implementation of throttling mechanism
====================================================|License: MIT| |Build Status|
**choke** is a package implementing a trivial to use general purpose throttling mechanism. The basic workflow with **choke** is as follows:
1. Create manager - an object responsible for keeping track of timestamps when some events (think: calls to your functions) occur.
2. Instruct manager to "choke" some callables, i.e. define maximum number of calls that can occur per given time window.
3. Use your callables as usual, keeping in mind that when the above defined limit is exceeded, the choked callable will raise `CallLimitExceededError`.Here is an example containing everything you need to use **choke**:
.. code:: python
from time import sleep
from redis import StrictRedis
from choke import RedisChokeManager, CallLimitExceededErrorREDIS = StrictRedis() # Tweak this to reflect your setup
CHOKE_MANAGER = RedisChokeManager(redis=REDIS)# Example configuration: enforce limit of no more than 10 calls in two seconds window
@CHOKE_MANAGER.choke(limit=10, window_length=2)
def foo(x, y):
"""Just print something to show that foo was called."""
print(f'foo called with ({x}, {y})')if __name__ == '__main__':
# We expect pattern of 10 successes followed by 10 failures followed again by 10 successes
# Some deviations from this pattern may obviously occur as calling foo takes nonzero time
for i in range(30):
try:
foo(i, y=i ** 2)
except CallLimitExceededError:
print('Foo not called. Limit exceeded!')
sleep(0.1).. |License: MIT| image:: https://img.shields.io/badge/License-MIT-yellow.svg
:target: https://opensource.org/licenses/MIT
.. |Build Status| image:: https://travis-ci.org/dexter2206/choke.svg?branch=master
:target: https://travis-ci.org/dexter2206/choke