Skip to main content

A simple Python Ratelimiter

Project description

Rate limiter

Copyright (c) 2024 Tuyen Phan

A simple python implementation of a general purpose rate limiter.

Example:

from rate_limit import RateLimiter, Bucket, Period as Per

# Initialize.
rate_of = Bucket.builder()
limiter = RateLimiter(per_second=rate_of(3, Per.SECOND),
                      per_minute=rate_of(10, Per.MINUTE))

# Use limiter.
if limiter.reduce():
  print "There is still quota left, so we can send your request."
else:
  print "Quota exhausted for", list(limiter.exhausted())

# Display status.
print limiter.status()

# Get remaining tokens for bucket.
if limiter.get("per_minute") < 3:
  print "You are awfully close to exhausting your minute limits."

There is also a cleaner way to utilize the rate limiter, by simply wrapping the implementation:

class Client(object):
  def __init__(self, limiter):
    self.send = limiter.wrap(self._send_impl)

  def _send_impl(self, req):
    print "Sending request (%s)" % json.dumps(req)


# This is now a rate-limited call which throws when exceeding the limit.
client.send("foo")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simple-ratelimiter-1.0.1.tar.gz (2.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simple_ratelimiter-1.0.1-py3-none-any.whl (2.6 kB view details)

Uploaded Python 3

File details

Details for the file simple-ratelimiter-1.0.1.tar.gz.

File metadata

  • Download URL: simple-ratelimiter-1.0.1.tar.gz
  • Upload date:
  • Size: 2.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.7

File hashes

Hashes for simple-ratelimiter-1.0.1.tar.gz
Algorithm Hash digest
SHA256 0bee6e37e287b4a052135028ab52478d8e491aa252b6fdb1a768372a0a419640
MD5 d466c41e785c7af9969acecf55ac4e94
BLAKE2b-256 df06d624622668e954c063c5468b612580ffe63ffefea813142ecdcf8d8e90c3

See more details on using hashes here.

File details

Details for the file simple_ratelimiter-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_ratelimiter-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 75832a5c518e62c6804e7fa795a407ff9aa131b832cec11764f080abb52d60b6
MD5 25c10958e2ef4bf31857b16c6d2d9506
BLAKE2b-256 50bcead436f82c59fb4bac7f2e1463ef5bb51a1f0c26ff76dcf6152e5bb804ad

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page