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, 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 – Maximum delay before failing requests.

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

  • async_wrapper – 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', buffer_ms=50, use_file_lock=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 – 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 – 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, 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 – Maximum delay before failing requests.

  • raise_when_fail – Whether to raise an exception when a request fails.

  • retry_until_max_delay – Retry until the maximum delay is reached.

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