Skip to content

Instantly share code, notes, and snippets.

@pcustic
Created February 15, 2024 12:04
Show Gist options
  • Save pcustic/60c0e38ff5c47f1943f22c6119ec127c to your computer and use it in GitHub Desktop.
Save pcustic/60c0e38ff5c47f1943f22c6119ec127c to your computer and use it in GitHub Desktop.
PyrateLimiter example with Redis
from pyrate_limiter import RedisBucket, Limiter, Duration, Rate, BucketFullException
from redis import Redis
max_requests = 5
time_period = Duration.SECOND * 2
rate = Rate(max_requests, time_period)
redis_connection = Redis(host="localhost")
redis_bucket = RedisBucket.init([rate], redis_connection, "my-bucket-name")
limiter = Limiter(redis_bucket)
for request in range(max_requests + 1):
try:
limiter.try_acquire(request)
except BucketFullException as err:
print(err)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment