43 lines
1.1 KiB
Python
43 lines
1.1 KiB
Python
from collections.abc import AsyncIterator
|
|
|
|
from fastapi import Request
|
|
|
|
from app.errors.exceptions import rate_limited, unavailable
|
|
|
|
|
|
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 create_request_capacity(max_in_flight: int) -> RequestCapacity:
|
|
return RequestCapacity(max_in_flight)
|
|
|
|
|
|
async def acquire_request_slot(request: Request) -> AsyncIterator[None]:
|
|
if request.app.state.lifecycle.draining:
|
|
raise unavailable("Реплика завершает работу")
|
|
capacity: RequestCapacity = request.app.state.request_capacity
|
|
|
|
if not capacity.try_acquire():
|
|
raise rate_limited()
|
|
|
|
try:
|
|
yield
|
|
finally:
|
|
capacity.release()
|