forked from mtusi2026/mtusi2026lab2
init commit: create skeleton from lab1
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
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()
|
||||
Reference in New Issue
Block a user