Skip to main content

API Throttler

Build Release PyPi

A Python toolkit to enforce API rate limit on the backend. The toolkit enable the service backend to limit the number of API calls in a specified period, e.g., 15 API calls per 900 seconds. There are four throttler classes in the toolkit:

  • FixedWindowThrottler
  • SlidingWindowThrottler
  • FixedWindowThrottlerRedis
  • SlidingWindowThrottlerRedis

The first two throttler classes use local storage to save throttler data (e.g., API calls that have been served and their timestamps), while the last two throttler classes use a Redis server to store throttler information. The difference between the fixed window and sliding window throttlers is the fixed window throttler uses the timestamp when the first feasible request is served as the starting timestamp to determine the number of allowed API calls in the following period, while the sliding window throttler uses the current timestamp minus the specified period as the starting timestamp to calculate the number of allowed API calls. The advantage of fixed window throttler is its simplicity, but there could be many API calls allowed if they are at the end of last period and the beginning of the current period. On the other hand, the sliding window throttler could resolve this issue, but it takes more memory.

Usage

To use this API throttler toolkit, first install it using pip:

pip install api-throttler

Then, import the package in your python script and use appropriate throttler classes:

import time

from api_throttler import Throttler, FixedWindowThrottler, SlidingWindowThrottler


# Limit 3 calls per 10 seconds
fixed_window_throttler = FixedWindowThrottler(calls=3, period=10)
sliding_window_throttler = SlidingWindowThrottler(calls=3, period=10)


def call_api(throttler: Throttler, key: str = 'some_string_key'):
    if not throttler.is_throttled(key):
        print('API call is NOT throttled')
    else:
        print('API call is throttled')


print('Using fixed window API throttler')
for i in range(20):
    print(f'This is the {i}-th second')
    # Call API in the following i-th seconds
    if i in {0, 8, 9, 10, 11, 12}:
        call_api(fixed_window_throttler)
    time.sleep(1)

print('-'*40)

print('Using sliding window API throttler')
for i in range(20):
    print(f'This is the {i}-th second')
    if i in {0, 8, 9, 10, 11, 12}:
        call_api(sliding_window_throttler)
    time.sleep(1)

In the above example, the data of the throttler is saved in local memory. If you would like to save it in a redis server, you can use the FixedWindowThrottlerRedis and SlidingWindowThrottlerRedis classes. The following scripts shows how to use the two classes using fake redis:

import time

import fakeredis
from api_throttler import Throttler, FixedWindowThrottlerRedis, SlidingWindowThrottlerRedis

cache = fakeredis.FakeStrictRedis()

# Limit 3 calls per 10 seconds
fixed_window_throttler = FixedWindowThrottlerRedis(calls=3, period=10, cache=cache)
sliding_window_throttler = SlidingWindowThrottlerRedis(calls=3, period=10, cache=cache)


def call_api(throttler: Throttler, key: str = 'some_string_key'):
    if not throttler.is_throttled(key):
        print('API call is NOT throttled')
    else:
        print('API call is throttled')


print('Using fixed window API throttler')
for i in range(20):
    print(f'This is the {i}-th second')
    # Call API in the following i-th seconds
    if i in {0, 8, 9, 10, 11, 12}:
        call_api(fixed_window_throttler)
    time.sleep(1)

print('-'*40)

print('Using sliding window API throttler')
for i in range(20):
    print(f'This is the {i}-th second')
    if i in {0, 8, 9, 10, 11, 12}:
        call_api(sliding_window_throttler)
    time.sleep(1)

If you would like to try a real example using API served by a Flask app and a redis server, please try to run the app.py using Docker by typing the following command in your terminal:

docker-compose up --build

Release files for api-throttler 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for api-throttler 0.3.1
File Size Uploaded
api_throttler-0.3.1.tar.gz 5.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for api-throttler 0.3.1
File Interpreter ABI Platform
api_throttler-0.3.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 12.0 kB

Release files / api_throttler-0.3.1.tar.gz

Download URL api_throttler-0.3.1.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
814e88e72dad61fea7d3f9c3bb5837a4efa3d841c73c061ab849f9cbeb4df64c
BLAKE2b-256 checksum
How to use checksums
8cda95546edc514fdc30aef209a0f3f3ca55fc1ec9b92f32f0c158f8aca6d508
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.9.1

Release files / api_throttler-0.3.1-py2.py3-none-any.whl

Download URL api_throttler-0.3.1-py2.py3-none-any.whl
Size 6.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
acfcd7ce018ecb2a1df15db319248abde0a0a75aed3b3d53d11171699da7d461
BLAKE2b-256 checksum
How to use checksums
b19c3da2c395c6f7abad2cd6b016ff66a70fe011684844cf2d49a499bde2e436
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.9.1

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 release files

0.2.2

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page