Skip to content
Back to top
View this page

# pyrate_limiter.limiter_factory module

A collection of common use cases and patterns for pyrate_limiter

pyrate_limiter.limiter_factory.create_inmemory_limiter(rate_per_duration=3, duration=Duration.SECOND, max_delay=Duration.DAY, buffer_ms=50, async_wrapper=False)

  Create an in-memory rate limiter with configurable rate, duration, delay, and optional async support.

  Parameters:

    - rate_per_duration (`int`) – Number of allowed requests per duration.

    - duration (`Union`[`int`, `Duration`]) – Time window for the rate limit.

    - max_delay (`Union`[`int`, `Duration`]) – Maximum delay before failing requests.

    - buffer_ms (`int`) – Extra wait time in milliseconds to account for clock drift.

    - async_wrapper (`bool`) – Whether to wrap the bucket for async usage.

  Returns:

    Configured in-memory limiter instance.

  Return type:

    Limiter

pyrate_limiter.limiter_factory.create_sqlite_bucket(rates, db_path, table_name='pyrate_limiter', use_file_lock=False)

  Create and initialize a SQLite bucket for rate limiting.

  Parameters:

    - rates (`List`[`Rate`]) – List of rate limit configurations.

    - db_path (`Optional`[`str`]) – Path to the SQLite database file (or in-memory if None).

    - table_name (`str`) – Name of the table to store rate bucket data.

    - use_file_lock (`bool`) – Enable file locking for multi-process synchronization.

  Returns:

    Initialized SQLite-backed bucket.

  Return type:

    SQLiteBucket

pyrate_limiter.limiter_factory.create_sqlite_limiter(rate_per_duration=3, duration=Duration.SECOND, db_path=None, table_name='rate_bucket', max_delay=Duration.DAY, buffer_ms=50, use_file_lock=False, async_wrapper=False)

  Create a SQLite-backed rate limiter with configurable rate, persistence, and optional async support.

  Parameters:

    - rate_per_duration (`int`) – Number of allowed requests per duration.

    - duration (`Union`[`int`, `Duration`]) – Time window for the rate limit.

    - db_path (`Optional`[`str`]) – Path to the SQLite database file (or in-memory if None).

    - table_name (`str`) – Name of the table used for rate buckets.

    - max_delay (`Union`[`int`, `Duration`]) – Maximum delay before failing requests.

    - buffer_ms (`int`) – Extra wait time in milliseconds to account for clock drift.

    - use_file_lock (`bool`) – Enable file locking for multi-process synchronization.

    - async_wrapper (`bool`) – Whether to wrap the bucket for async usage.

  Returns:

    Configured SQLite-backed limiter instance.

  Return type:

    Limiter

pyrate_limiter.limiter_factory.init_global_limiter(bucket, max_delay=Duration.HOUR, raise_when_fail=False, retry_until_max_delay=True, buffer_ms=50)

  Initialize a global Limiter instance using the provided bucket.

  Intended for use as an initializer for ProcessPoolExecutor.

  Parameters:

    - bucket (`AbstractBucket`) – The rate-limiting bucket to be used.

    - max_delay (`Union`[`int`, `Duration`]) – Maximum delay before failing requests.

    - raise_when_fail (`bool`) – Whether to raise an exception when a request fails.

    - retry_until_max_delay (`bool`) – Retry until the maximum delay is reached.

    - buffer_ms (`int`) – Additional buffer time in milliseconds for retries.