[docs]classRateController(ABC):"""Abstract base class for rate controllers."""
[docs]asyncdef__aenter__(self)->Self:"""Enter the controller's context."""returnself
[docs]asyncdef__aexit__(self,*_:Any)->Optional[bool]:"""Exit the controller's context."""returnFalse
[docs]@abstractmethoddefcan_acquire(self,tokens:float=1)->bool:""" Args: tokens: The amount of tokens to acquire for the request. Defaults to `1`. Returns: Whether a request for the given amount of tokens can be processed instantly. """
[docs]@asynccontextmanager@abstractmethodasyncdefrequest(self,tokens:float=1,**kwargs:Any)->AsyncIterator[None]:"""Asynchronous context manager that requests the given amount of tokens before execution. Args: tokens: The number of tokens required for the request. Defaults to `1`. """yield# pragma: no cover