add basic rate limiting
This commit is contained in:
@ -13,8 +13,14 @@ from .constructors import (
|
||||
)
|
||||
from utils import radio_util
|
||||
from typing import Optional
|
||||
from fastapi import FastAPI, BackgroundTasks, Request, Response, HTTPException
|
||||
from starlette.concurrency import run_in_threadpool
|
||||
from fastapi import (
|
||||
FastAPI,
|
||||
BackgroundTasks,
|
||||
Request,
|
||||
Response,
|
||||
HTTPException,
|
||||
Depends)
|
||||
from fastapi_throttle import RateLimiter
|
||||
from fastapi.responses import RedirectResponse, JSONResponse
|
||||
|
||||
|
||||
@ -43,7 +49,8 @@ class Radio(FastAPI):
|
||||
|
||||
for endpoint, handler in self.endpoints.items():
|
||||
app.add_api_route(
|
||||
f"/{endpoint}", handler, methods=["POST"], include_in_schema=True
|
||||
f"/{endpoint}", handler, methods=["POST"], include_in_schema=True,
|
||||
dependencies=[Depends(RateLimiter(times=10, seconds=5))],
|
||||
)
|
||||
|
||||
# NOTE: Not in loop because method is GET for this endpoint
|
||||
@ -58,7 +65,7 @@ class Radio(FastAPI):
|
||||
|
||||
async def on_start(self) -> None:
|
||||
logging.info("radio: Initializing")
|
||||
await run_in_threadpool(self.radio_util.load_playlist)
|
||||
self.loop.run_in_executor(None, self.radio_util.load_playlist)
|
||||
|
||||
async def radio_skip(
|
||||
self, data: ValidRadioNextRequest, request: Request
|
||||
@ -317,7 +324,7 @@ class Radio(FastAPI):
|
||||
if len(self.radio_util.active_playlist) > 1:
|
||||
self.radio_util.active_playlist.append(next) # Push to end of playlist
|
||||
else:
|
||||
await run_in_threadpool(self.radio_util.load_playlist)
|
||||
self.loop.run_in_executor(None, self.radio_util.load_playlist)
|
||||
|
||||
self.radio_util.now_playing = next
|
||||
next["start"] = time_started
|
||||
|
Reference in New Issue
Block a user