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, buffer_ms=50)

  Create an in-memory rate limiter with configurable rate and clock-drift buffer.

  Parameters:

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

    - duration (`int` | `Duration`) – Time window for the rate limit.

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

  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 (`str` | `None`) – Path to the SQLite database file. If None, a temporary on-disk SQLite database is created.

    - 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', buffer_ms=50, use_file_lock=False)

  Create a SQLite-backed rate limiter with configurable rate, persistence, and file locking.

  Parameters:

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

    - duration (`int` | `Duration`) – Time window for the rate limit.

    - db_path (`str` | `None`) – Path to the SQLite database file. If None, a temporary on-disk SQLite database is created.

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

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

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

  Returns:

    Configured SQLite-backed limiter instance.

  Return type:

    Limiter

pyrate_limiter.limiter_factory.init_global_limiter(bucket, 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.

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

  Return type:

    `None`