add async with all functions
This commit is contained in:
@@ -1,22 +1,40 @@
|
||||
from collections.abc import Generator
|
||||
from threading import BoundedSemaphore
|
||||
from collections.abc import AsyncIterator
|
||||
|
||||
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)
|
||||
class RequestCapacity:
|
||||
def __init__(self, max_in_flight: int) -> None:
|
||||
self._max_in_flight = max_in_flight
|
||||
self._in_flight = 0
|
||||
|
||||
@property
|
||||
def in_flight(self) -> int:
|
||||
return self._in_flight
|
||||
|
||||
def try_acquire(self) -> bool:
|
||||
if self._in_flight >= self._max_in_flight:
|
||||
return False
|
||||
self._in_flight += 1
|
||||
return True
|
||||
|
||||
def release(self) -> None:
|
||||
self._in_flight -= 1
|
||||
|
||||
|
||||
def acquire_request_slot(request: Request) -> Generator[None, None, None]:
|
||||
semaphore: BoundedSemaphore = request.app.state.request_capacity
|
||||
def create_request_capacity(max_in_flight: int) -> RequestCapacity:
|
||||
return RequestCapacity(max_in_flight)
|
||||
|
||||
if not semaphore.acquire(blocking=False):
|
||||
|
||||
async def acquire_request_slot(request: Request) -> AsyncIterator[None]:
|
||||
capacity: RequestCapacity = request.app.state.request_capacity
|
||||
|
||||
if not capacity.try_acquire():
|
||||
raise rate_limited()
|
||||
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
semaphore.release()
|
||||
capacity.release()
|
||||
|
||||
Reference in New Issue
Block a user