Skip to content
Back to top
View this page

# pyrate_limiter.limiter module

Limiter class implementation

class pyrate_limiter.limiter.Limiter(argument, buffer_ms=50)

  Bases: `object`

  This class responsibility is to sum up all underlying logic and make working with async/sync functions easily

  __init__(argument, buffer_ms=50)

    Init Limiter using either a single bucket / multiple-bucket factory / single rate / rate list.

    Parameters:

      argument (Union[BucketFactory, AbstractBucket, Rate, List[Rate]]) – The bucket or rate configuration.

  as_decorator(*, name='ratelimiter', weight=1)
  bucket_factory
  buckets()

    Get list of active buckets

    Return type:

      `List`[`AbstractBucket`]

  buffer_ms
  close()
    Return type:

      `None`

  dispose(bucket)

    Dispose/Remove a specific bucket, using bucket-id or bucket object as param

    Return type:

      `bool`

  handle_bucket_put(bucket, item, blocking, _force_async=False, deadline=None)

    Putting item into bucket

    Return type:

      `bool` | `Awaitable`[`bool`]

  lock
  try_acquire(name='pyrate', weight=1, blocking=True, timeout=-1)

    Attempt to acquire a permit from the limiter.

    Parameters:

      - name (str, default "pyrate") – The bucket key to acquire from.

      - weight (int, default 1) – Number of permits to consume.

      - timeout (int | float, default -1) – Maximum time (in seconds) to wait; -1 means wait indefinitely.

      - blocking (bool, default True) – If True, block until a permit is available (subject to timeout); if False, return immediately.

    Returns:

      True if the permit was acquired, False otherwise. Async limiters return an awaitable resolving to the same.

    Return type:

      bool or Awaitable[bool]

  async try_acquire_async(name='pyrate', weight=1, blocking=True, timeout=-1)

    Attempt to asynchronously acquire a permit from the limiter.

    Parameters:

      - name (str, default "pyrate") – The bucket key to acquire from.

      - weight (int, default 1) – Number of permits to consume.

      - blocking (bool, default True) – If True, wait until a permit is available (subject to timeout); if False, return immediately.

      - timeout (int | float, default -1) – Maximum time (in seconds) to wait; -1 means wait indefinitely.

    Returns:

      True if the permit was acquired, False otherwise.

    Return type:

      bool

    Notes

    This is the async variant of `try_acquire`. A top-level, thread-local async lock is used to prevent blocking the event loop.

class pyrate_limiter.limiter.LockLike(*args, **kwargs)

  Bases: `Protocol`

  acquire(blocking=Ellipsis, timeout=Ellipsis)
    Return type:

      `bool`

  release()
    Return type:

      `None`

class pyrate_limiter.limiter.SingleBucketFactory(bucket, schedule_leak=True)

  Bases: `BucketFactory`

  Single-bucket factory for quick use with Limiter

  __init__(bucket, schedule_leak=True)

    Initialize the SingleBucketFactory with a bucket and an optional leak scheduling flag.

    schedule_leak (bool): If True, the factory will schedule periodic leaks for the bucket. Default is True. Disable only if you plan to handle leaking manually.

  bucket
  get(_)

    Get the corresponding bucket to this item

    Return type:

      `AbstractBucket`

  wrap_item(name, weight=1)

    Add the current timestamp to the receiving item using any clock backend - Turn it into a RateItem - Can return either a coroutine or a RateItem instance

pyrate_limiter.limiter.combined_lock(locks, blocking, timeout=-1)