Skip to content

configs

Configuration for Rate Limiter implementations

BucketConfig dataclass

Configuration for any Rate Limiter

Source code in limitor/configs.py
@dataclass
class BucketConfig:
    """Configuration for any Rate Limiter"""

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

    seconds: float = 1
    """Up to `capacity` acquisitions are allowed within this time period in a burst"""

    def __post_init__(self) -> None:
        """Validate the configuration parameters"""
        if self.seconds <= 0:
            raise ValueError("seconds must be positive and non-zero")

        if self.capacity < 1:
            raise ValueError("capacity must be at least 1")

capacity = 10 class-attribute instance-attribute

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

seconds = 1 class-attribute instance-attribute

Up to capacity acquisitions are allowed within this time period in a burst

__post_init__()

Validate the configuration parameters

Source code in limitor/configs.py
def __post_init__(self) -> None:
    """Validate the configuration parameters"""
    if self.seconds <= 0:
        raise ValueError("seconds must be positive and non-zero")

    if self.capacity < 1:
        raise ValueError("capacity must be at least 1")

Capacity

Bases: NamedTuple

Information about the current capacity of the bucket

Source code in limitor/configs.py
class Capacity(NamedTuple):
    """Information about the current capacity of the bucket"""

    has_capacity: bool
    """Indicates if the bucket has enough capacity to accommodate the requested amount"""

    needed_capacity: float
    """Amount of capacity needed to accommodate the request, if any"""

has_capacity instance-attribute

Indicates if the bucket has enough capacity to accommodate the requested amount

needed_capacity instance-attribute

Amount of capacity needed to accommodate the request, if any