from collections.abc import Generator from threading import BoundedSemaphore from fastapi import Request from app.errors.exceptions import rate_limited def create_request_capacity(max_in_flight: int) -> BoundedSemaphore: return BoundedSemaphore(max_in_flight) def acquire_request_slot(request: Request) -> Generator[None, None, None]: semaphore: BoundedSemaphore = request.app.state.request_capacity if not semaphore.acquire(blocking=False): raise rate_limited() try: yield finally: semaphore.release()