Skip to main content

Rate limiting library for Python with sync/async support

Project description

Flowguard

Flowguard is a rate limiting library for Python. It provides both synchronous (RateLimiter) and asynchronous (AsyncRateLimiter) classes to manage request rates with flexible time windows (seconds, minutes, hours, days) and optional burst limits.

Features

  • Synchronous and Asynchronous Support: Use RateLimiter for blocking operations or AsyncRateLimiter for async/await workflows.
  • Customizable Time Windows: Set rate limits per second, minute, hour, or day, with configurable window durations.
  • Burst Limiting: Optional maximum burst capacity to control maximum concurrent requests.
  • Thread-Safe and Interruptible: Built with atomic operations and mutexes for safe concurrent use.
  • Context Manager Support: For automatic resource management.

Installation

Flowguard is available as a Python package. Install it using pip:

pip install flowguard

Ensure you have a compatible Python version (3.8 or higher) installed.

Parameters

  • sec, min, hour, day: Rate limits for respective time units.
  • sec_window, min_window, hour_window, day_window: Custom window durations (in seconds) for respective units. Default is 1.
  • blocking: Default True (For using as a context manager). if False, returns immediately if no permit is available.
  • max_burst: Optional maximum number of concurrent permits allowed.

Usage

Synchronous Rate Limiter Example ::

import logging
import threading
from time import sleep
from flowguard import RateLimiter

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s.%(msecs)03d %(levelname)s: %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S'
)

logger = logging.getLogger(__name__)

def non_blocking_example(i, limiter):
    permit = limiter.acquire()
    if permit:
        logger.info(f"Permit acquired for {i}.")
        # Simulate work ::
        sleep(1)
        limiter.release()
    else:
        logger.info("No permit is available.")
        # Here we can do other task when no immediate permit is available

def context_manager_example(i, limiter):
    with limiter:
        logger.info(f"Permit acquired for {i}.")
        # Simulate work ::
        sleep(1)

def explicit_example(i, limiter):
    # This call will block until it gets a permit
    permit = limiter.acquire()
    if permit:
        logger.info(f"Permit acquired for {i}")
        # Simulate work
        sleep(1)
        limiter.release()

def main():
    # This will create a ratelimiter instance with 3 req/2 sec and 15 req/min with a max allowed concurrent req = 2
    blocking_limiter = RateLimiter(sec= 3, min= 15, sec_window=2, max_burst= 2)
    non_blocking_limiter = RateLimiter(sec= 3, min= 15, sec_window=2, max_burst= 2, blocking=False)
    
    print("Blocking Limiter".center(60, "="), "\n")    
    print("Using Context Manager".center(60, "="), "\n")

    threads = []

    for i in range(30):
        t = threading.Thread(target=context_manager_example, args=(i, blocking_limiter), daemon= True)
        threads.append(t)
        t.start()
    
    # Waiting for the work to finish oktherwise both function output will be mixed
    for t in threads:
        t.join()

    blocking_limiter = RateLimiter(sec= 3, min= 15, sec_window=2, max_burst= 2)
    
    print("\n", "Explicit acquire/ release".center(60, "="), "\n")

    threads = []

    for i in range(30):
        t = threading.Thread(target=explicit_example, args=(i, blocking_limiter), daemon= True)
        threads.append(t)
        t.start()

    for t in threads:
        t.join()

    print("\n", "Non-Blocking Mode".center(60, "="), "\n")

    threads = []

    for i in range(30):
        t = threading.Thread(target=non_blocking_example, args=(i, non_blocking_limiter), daemon= True)
        threads.append(t)
        t.start()
        # Without the sleep it will give permit = max_burst count and all other threads will failed to acquire any permit
        sleep(.1)
    
    for t in threads:
        t.join()

if __name__ == "__main__":
    main()

Asynchronous Rate Limiter Example ::

import asyncio
import logging
from flowguard import AsyncRateLimiter

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s.%(msecs)03d %(levelname)s: %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S'
)

logger = logging.getLogger(__name__)


async def explicit_example(i, limiter):
    await limiter.acquire()
    logger.info(f"Permit acquired for {i}")
    # Simulate work 
    await asyncio.sleep(1)
    await limiter.release()

async def context_manager_example(i, limiter):
    async with limiter:
        logger.info(f"Permit acquired for {i}")

async def main():
    limiter = AsyncRateLimiter(sec=10)

    print("\n", "Explicit Example".center(60, "="), "\n")
    explicit_tasks = [
        explicit_example(i, limiter) 
        for i in range(15)  
    ]

    await asyncio.gather(*explicit_tasks)

    print("\n", "Context Manager Example".center(60, "="), "\n")
    burst_limiter = AsyncRateLimiter(sec=5, max_burst=3, sec_window= 2)
    
    burst_tasks = [
        context_manager_example(f"burst-{i}", burst_limiter)
        for i in range(15)  
    ]
    
    await asyncio.gather(*burst_tasks)
    

if __name__ == "__main__":
    asyncio.run(main())

License

Flowguard is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! Feel free to submit pull requests, report issues, or suggest improvements.

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

flowguard-0.1.2.tar.gz (17.8 kB view details)

Uploaded Source

Built Distributions

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

flowguard-0.1.2-cp38-abi3-win_amd64.whl (335.9 kB view details)

Uploaded CPython 3.8+Windows x86-64

flowguard-0.1.2-cp38-abi3-win32.whl (312.5 kB view details)

Uploaded CPython 3.8+Windows x86

flowguard-0.1.2-cp38-abi3-musllinux_1_2_x86_64.whl (586.2 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ x86-64

flowguard-0.1.2-cp38-abi3-musllinux_1_2_i686.whl (621.2 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ i686

flowguard-0.1.2-cp38-abi3-musllinux_1_2_armv7l.whl (680.9 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARMv7l

flowguard-0.1.2-cp38-abi3-musllinux_1_2_aarch64.whl (574.2 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

flowguard-0.1.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (414.8 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ x86-64

flowguard-0.1.2-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (482.3 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ s390x

flowguard-0.1.2-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (453.1 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ppc64le

flowguard-0.1.2-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (416.2 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARMv7l

flowguard-0.1.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (399.6 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

flowguard-0.1.2-cp38-abi3-manylinux_2_12_i686.manylinux2010_i686.whl (442.9 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.12+ i686

flowguard-0.1.2-cp38-abi3-macosx_11_0_arm64.whl (371.1 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

flowguard-0.1.2-cp38-abi3-macosx_10_12_x86_64.whl (390.6 kB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

Details for the file flowguard-0.1.2.tar.gz.

File metadata

  • Download URL: flowguard-0.1.2.tar.gz
  • Upload date:
  • Size: 17.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.8.7

File hashes

Hashes for flowguard-0.1.2.tar.gz
Algorithm Hash digest
SHA256 1370d5734004757b30eb39e339e98d2ff3cf6e6593f0d39c35154dad70eb760b
MD5 13df3552c70b48cf1738f6e8198c1f58
BLAKE2b-256 7196ba0678aa284314903c7be2ee6b631ec6a894212f942ee0ea69fc65c16dcb

See more details on using hashes here.

File details

Details for the file flowguard-0.1.2-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: flowguard-0.1.2-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 335.9 kB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.8.7

File hashes

Hashes for flowguard-0.1.2-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 9e8e1847514e3317ee9cad264d9042dda058a5e20c84c12c1824ea5228e32d5d
MD5 967453603e7778a60340063a7bb11df2
BLAKE2b-256 6b72beac5ee156662ad16455b114ba3f4d9a19fb8bbd743d7aecc84c8b6e3997

See more details on using hashes here.

File details

Details for the file flowguard-0.1.2-cp38-abi3-win32.whl.

File metadata

  • Download URL: flowguard-0.1.2-cp38-abi3-win32.whl
  • Upload date:
  • Size: 312.5 kB
  • Tags: CPython 3.8+, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.8.7

File hashes

Hashes for flowguard-0.1.2-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 cc23a50b740410ca2042d6fd2fd12e0dfb1c459fbf073e443357544a83dd6516
MD5 05ff13074dcaec9c4cf0070b6b715305
BLAKE2b-256 d3d550d02f157dd1aaef3f7a9059179190a1d6418fcfa92fd7370cef37d87a8b

See more details on using hashes here.

File details

Details for the file flowguard-0.1.2-cp38-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for flowguard-0.1.2-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 7c91d570a152bcae2d808d518317a783be0f99ae01ad39a179fdb05d6916c4f7
MD5 054b0e84337b1f3d73be44b598a3ac30
BLAKE2b-256 fc4b12dc2fa88498af6eaa33be190da44df6cf94e491ebee6c773fbf4c147e23

See more details on using hashes here.

File details

Details for the file flowguard-0.1.2-cp38-abi3-musllinux_1_2_i686.whl.

File metadata

File hashes

Hashes for flowguard-0.1.2-cp38-abi3-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 459ee1f7bb6fbc2272e18a62bc8817a7e943735149a9c78d97672d46f97f1788
MD5 080e53364178f6a9d58b972b0b504c6a
BLAKE2b-256 8a36186b923b627ae8a55456df6b48835ec309f1c063c4f6b57c089c6c64c8e0

See more details on using hashes here.

File details

Details for the file flowguard-0.1.2-cp38-abi3-musllinux_1_2_armv7l.whl.

File metadata

File hashes

Hashes for flowguard-0.1.2-cp38-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 5157b808574090417de3f8c92f7f99f33e9cd37e133d5c4fa63385e032eddf58
MD5 71c4e512b1d3bc27599a86bd1a29d82e
BLAKE2b-256 05657dc47e551e33fa232784621f26e4d8ecc13e6de1be2c4e2b8adfab63f105

See more details on using hashes here.

File details

Details for the file flowguard-0.1.2-cp38-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for flowguard-0.1.2-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 795a29058a4800ff565c107d01e0be53ab07d120f4b955bf829bcf27041fd0a9
MD5 4e6daade13a7a4a1f8fc1d456e43033a
BLAKE2b-256 8f61a0f9a6f09226f0e1322be0b73bc7ee7821aeaf7d01aec580d1824c785ea4

See more details on using hashes here.

File details

Details for the file flowguard-0.1.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for flowguard-0.1.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3215fc6f80ed6f65026faf07121fec3ec4d619efbf77e772da9c12d887d078bd
MD5 ee5145cc1385b36bed5dd99af4d75c7e
BLAKE2b-256 c4a65b1849704b6fb4aaf7a5116ed59679849483f8521a192e5d9d17ba8943c8

See more details on using hashes here.

File details

Details for the file flowguard-0.1.2-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl.

File metadata

File hashes

Hashes for flowguard-0.1.2-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 632f3b00a6215b58eea8fd1262ed0628f8a566bb6a2c602b95f56dc04d0867b2
MD5 1bf590296168f68518d66098650f658d
BLAKE2b-256 58435ccecfd55a908774aca2143ed9252951e2f032fe8720a44d8b1f9a22239d

See more details on using hashes here.

File details

Details for the file flowguard-0.1.2-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl.

File metadata

File hashes

Hashes for flowguard-0.1.2-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 f5eaa1b3ad23271e4dfb1036e03ac1af94ec8651f9ac248141a2b1b71e01c6a1
MD5 3a6b47f800e0a7ac6d552d2329a1ab33
BLAKE2b-256 dae59f928b7fa1c01d4e0a10256d1cbe6b79856c1e6a069b598b69f65041e64b

See more details on using hashes here.

File details

Details for the file flowguard-0.1.2-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for flowguard-0.1.2-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 ef3d0ae09edf96c444b37c62b1ad66850de06ebaf478e5e0ef3fe39e0b1e9c5f
MD5 0167bd79bc5e2048146a41d36cff216c
BLAKE2b-256 43b87e2077b096aa1cb08ac72f27e8da730d29ede9285074ac8df22433d2c5e0

See more details on using hashes here.

File details

Details for the file flowguard-0.1.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for flowguard-0.1.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 ba11e6555979aff0bc5cb8a44669e75160dad905e97fa04066cd84cfbae81e7d
MD5 c4ca6ade4c0a10d536782be6abf96fc9
BLAKE2b-256 5dd496ff02da880bca362e06e53e4dc5a2fcd2a176ee15ae4617a746f05f0f1d

See more details on using hashes here.

File details

Details for the file flowguard-0.1.2-cp38-abi3-manylinux_2_12_i686.manylinux2010_i686.whl.

File metadata

File hashes

Hashes for flowguard-0.1.2-cp38-abi3-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm Hash digest
SHA256 3810c4479530d303edbc2d2c4082b71dccb1c91e9bdde7044d7c37e541e68648
MD5 92deb8f29423ee34d6863fbbde54718e
BLAKE2b-256 260ca3bc60fa92c89553d8009a6edde9cbc14bc3cb59dbc417c82a837ba636f1

See more details on using hashes here.

File details

Details for the file flowguard-0.1.2-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for flowguard-0.1.2-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 930f59fe7a7aa4511bd751eb1c8f52ebe634abbb47f5d840a1495828afa99636
MD5 06b6f437c1d60cacfa04c6d67b55f213
BLAKE2b-256 da7fe8e06e1f56f8166b85a7dc0298e0c7cf370247ddc6a63dbfededf5694fb2

See more details on using hashes here.

File details

Details for the file flowguard-0.1.2-cp38-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for flowguard-0.1.2-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 18aa061902a3cf4ec590c99fdf29c332ce0d455214686b17c412df6485e17cf2
MD5 3bd05aadbaa8e8a8a8c4e5dd7c8289ee
BLAKE2b-256 b52bbacd40eb7ecb2d0c2c7d96a80739ece9ebe4bb73d3cc86cfd4bae5f4f6c4

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