Skip to content

base

Rate Limit Protocols for Synchronous and Asynchronous Context Managers

AsyncRateLimit

Bases: Protocol

Asynchronous Rate Limit Protocol

Parameters:

Name Type Description Default
bucket_config BucketConfig

Configuration for the rate limit

required
max_concurrent int | None

Maximum number of concurrent requests allowed to acquire capacity

None
Source code in limitor/base.py
class AsyncRateLimit(Protocol):
    """Asynchronous Rate Limit Protocol

    Args:
        bucket_config: Configuration for the rate limit
        max_concurrent: Maximum number of concurrent requests allowed to acquire capacity
    """

    def __init__(self, bucket_config: BucketConfig, max_concurrent: int | None = None) -> None: ...

    async def acquire(self, amount: float = 1, timeout: float | None = None) -> None:
        """Acquire an item from the rate limit. This method should block until a token is available

        Args:
            amount: The amount of capacity to acquire, defaults to 1
            timeout: Optional timeout in seconds for the acquire operation
        """

    async def __aenter__(self) -> AsyncRateLimit:
        """Enter the context manager, acquiring resources if necessary

        This method should return an instance of AsyncRateLimit

        Returns:
            An instance of the rate limit context manager
        """
        ...  # pylint: disable=unnecessary-ellipsis

    async def __aexit__(self, exc_type: type[BaseException], exc_val: BaseException, exc_tb: TracebackType) -> None:
        """Exit the context manager, releasing any resources if necessary

        Args:
            exc_type: The type of the exception raised, if any
            exc_val: The value of the exception raised, if any
            exc_tb: The traceback object, if any
        """

__aenter__() async

Enter the context manager, acquiring resources if necessary

This method should return an instance of AsyncRateLimit

Returns:

Type Description
AsyncRateLimit

An instance of the rate limit context manager

Source code in limitor/base.py
async def __aenter__(self) -> AsyncRateLimit:
    """Enter the context manager, acquiring resources if necessary

    This method should return an instance of AsyncRateLimit

    Returns:
        An instance of the rate limit context manager
    """
    ...  # pylint: disable=unnecessary-ellipsis

__aexit__(exc_type, exc_val, exc_tb) async

Exit the context manager, releasing any resources if necessary

Parameters:

Name Type Description Default
exc_type type[BaseException]

The type of the exception raised, if any

required
exc_val BaseException

The value of the exception raised, if any

required
exc_tb TracebackType

The traceback object, if any

required
Source code in limitor/base.py
async def __aexit__(self, exc_type: type[BaseException], exc_val: BaseException, exc_tb: TracebackType) -> None:
    """Exit the context manager, releasing any resources if necessary

    Args:
        exc_type: The type of the exception raised, if any
        exc_val: The value of the exception raised, if any
        exc_tb: The traceback object, if any
    """

acquire(amount=1, timeout=None) async

Acquire an item from the rate limit. This method should block until a token is available

Parameters:

Name Type Description Default
amount float

The amount of capacity to acquire, defaults to 1

1
timeout float | None

Optional timeout in seconds for the acquire operation

None
Source code in limitor/base.py
async def acquire(self, amount: float = 1, timeout: float | None = None) -> None:
    """Acquire an item from the rate limit. This method should block until a token is available

    Args:
        amount: The amount of capacity to acquire, defaults to 1
        timeout: Optional timeout in seconds for the acquire operation
    """

HasCapacity

Bases: Protocol

Protocol for objects that have a capacity attribute

Source code in limitor/base.py
class HasCapacity(Protocol):  # pylint: disable=too-few-public-methods
    """Protocol for objects that have a capacity attribute"""

    capacity: float
    """Maximum number of items the bucket can hold i.e. number of requests that can be processed at once"""

capacity instance-attribute

Maximum number of items the bucket can hold i.e. number of requests that can be processed at once

SyncRateLimit

Bases: Protocol

Synchronous Rate Limit Protocol

Parameters:

Name Type Description Default
bucket_config BucketConfig

Configuration for the rate limit

required
Source code in limitor/base.py
class SyncRateLimit(Protocol):
    """Synchronous Rate Limit Protocol

    Args:
        bucket_config: Configuration for the rate limit
    """

    def __init__(self, bucket_config: BucketConfig) -> None: ...

    def acquire(self, amount: float = 1) -> None:
        """Acquire an item from the rate limit. This method should block until a token is available

        Args:
            amount: The amount of capacity to acquire, defaults to 1
        """

    def __enter__(self) -> SyncRateLimit:
        """Enter the context manager, acquiring resources if necessary

        This method should return an instance of SyncRateLimit

        Returns:
            An instance of the rate limit context manager
        """
        ...  # pylint: disable=unnecessary-ellipsis

    def __exit__(self, exc_type: type[BaseException], exc_val: BaseException, exc_tb: TracebackType) -> None:
        """Exit the context manager, releasing any resources if necessary

        Args:
            exc_type: The type of the exception raised, if any
            exc_val: The value of the exception raised, if any
            exc_tb: The traceback object, if any
        """

__enter__()

Enter the context manager, acquiring resources if necessary

This method should return an instance of SyncRateLimit

Returns:

Type Description
SyncRateLimit

An instance of the rate limit context manager

Source code in limitor/base.py
def __enter__(self) -> SyncRateLimit:
    """Enter the context manager, acquiring resources if necessary

    This method should return an instance of SyncRateLimit

    Returns:
        An instance of the rate limit context manager
    """
    ...  # pylint: disable=unnecessary-ellipsis

__exit__(exc_type, exc_val, exc_tb)

Exit the context manager, releasing any resources if necessary

Parameters:

Name Type Description Default
exc_type type[BaseException]

The type of the exception raised, if any

required
exc_val BaseException

The value of the exception raised, if any

required
exc_tb TracebackType

The traceback object, if any

required
Source code in limitor/base.py
def __exit__(self, exc_type: type[BaseException], exc_val: BaseException, exc_tb: TracebackType) -> None:
    """Exit the context manager, releasing any resources if necessary

    Args:
        exc_type: The type of the exception raised, if any
        exc_val: The value of the exception raised, if any
        exc_tb: The traceback object, if any
    """

acquire(amount=1)

Acquire an item from the rate limit. This method should block until a token is available

Parameters:

Name Type Description Default
amount float

The amount of capacity to acquire, defaults to 1

1
Source code in limitor/base.py
def acquire(self, amount: float = 1) -> None:
    """Acquire an item from the rate limit. This method should block until a token is available

    Args:
        amount: The amount of capacity to acquire, defaults to 1
    """