-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add slowapi * fix startup * adjust settings * add rate limits to tx routes * elastic
- Loading branch information
Showing
9 changed files
with
370 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from fastapi import status | ||
from fastapi.responses import JSONResponse | ||
from loguru import logger | ||
from slowapi import Limiter | ||
from slowapi.util import get_remote_address | ||
from starlette.requests import Request | ||
|
||
from ..core.settings import settings | ||
|
||
|
||
def _rate_limit_exceeded_handler(request: Request, exc: Exception) -> JSONResponse: | ||
remote_address = get_remote_address(request) | ||
logger.warning( | ||
f"Rate limit {settings.mint_global_rate_limit_per_minute}/minute exceeded: {remote_address}" | ||
) | ||
return JSONResponse( | ||
status_code=status.HTTP_429_TOO_MANY_REQUESTS, | ||
content={"detail": "Rate limit exceeded."}, | ||
) | ||
|
||
|
||
def get_remote_address_excluding_local(request: Request) -> str: | ||
remote_address = get_remote_address(request) | ||
if remote_address == "127.0.0.1": | ||
return "" | ||
return remote_address | ||
|
||
|
||
limiter_global = Limiter( | ||
key_func=get_remote_address_excluding_local, | ||
strategy="fixed-window-elastic-expiry", | ||
default_limits=[f"{settings.mint_global_rate_limit_per_minute}/minute"], | ||
enabled=settings.mint_rate_limit, | ||
) | ||
|
||
limiter = Limiter( | ||
key_func=get_remote_address_excluding_local, | ||
strategy="fixed-window-elastic-expiry", | ||
default_limits=[f"{settings.mint_transaction_rate_limit_per_minute}/minute"], | ||
enabled=settings.mint_rate_limit, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from fastapi import FastAPI | ||
from fastapi.exception_handlers import ( | ||
request_validation_exception_handler as _request_validation_exception_handler, | ||
) | ||
from fastapi.exceptions import RequestValidationError | ||
from fastapi.responses import JSONResponse | ||
from loguru import logger | ||
from starlette.middleware.cors import CORSMiddleware | ||
from starlette.requests import Request | ||
|
||
from ..core.settings import settings | ||
from .limit import _rate_limit_exceeded_handler, limiter_global | ||
|
||
if settings.debug_profiling: | ||
from fastapi_profiler import PyInstrumentProfilerMiddleware | ||
|
||
from slowapi.errors import RateLimitExceeded | ||
from slowapi.middleware import SlowAPIMiddleware | ||
|
||
|
||
def add_middlewares(app: FastAPI): | ||
app.add_middleware( | ||
CORSMiddleware, | ||
allow_origins=["*"], | ||
allow_methods=["*"], | ||
allow_headers=["*"], | ||
expose_headers=["*"], | ||
) | ||
|
||
if settings.debug_profiling: | ||
assert PyInstrumentProfilerMiddleware is not None | ||
app.add_middleware(PyInstrumentProfilerMiddleware) | ||
|
||
if settings.mint_rate_limit: | ||
app.state.limiter = limiter_global | ||
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler) | ||
app.add_middleware(SlowAPIMiddleware) | ||
|
||
|
||
async def request_validation_exception_handler( | ||
request: Request, exc: RequestValidationError | ||
) -> JSONResponse: | ||
""" | ||
This is a wrapper to the default RequestValidationException handler of FastAPI. | ||
This function will be called when client input is not valid. | ||
""" | ||
query_params = request.query_params._dict | ||
detail = { | ||
"errors": exc.errors(), | ||
"query_params": query_params, | ||
} | ||
# log the error | ||
logger.error(detail) | ||
# pass on | ||
return await _request_validation_exception_handler(request, exc) |
Oops, something went wrong.