From 44493aad2a86c42b7eec127c8e047d0863f82437 Mon Sep 17 00:00:00 2001 From: GabrielBarberini Date: Thu, 13 Jun 2024 20:06:42 -0300 Subject: [PATCH 1/3] implements gzpi middleware for payloads above 1kb --- lib/api.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/api.py b/lib/api.py index e91e166..b6af499 100644 --- a/lib/api.py +++ b/lib/api.py @@ -5,6 +5,7 @@ from fastapi import FastAPI, Request, status from fastapi.exceptions import RequestValidationError from fastapi.middleware.cors import CORSMiddleware +from fastapi.middleware.gzip import GZipMiddleware from fastapi.openapi.utils import get_openapi from fastapi.responses import RedirectResponse, JSONResponse @@ -29,6 +30,9 @@ app.include_router(motor.router) app.include_router(rocket.router) +# Compress responses above 1KB +app.add_middleware(GZipMiddleware, minimum_size=1000) + def custom_openapi(): if app.openapi_schema: From 9cf049e83a46c50744dacd8c8c50322688761865 Mon Sep 17 00:00:00 2001 From: GabrielBarberini Date: Thu, 13 Jun 2024 20:34:23 -0300 Subject: [PATCH 2/3] increases db timeout and allow context reentry --- lib/repositories/repo.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/repositories/repo.py b/lib/repositories/repo.py index 12b63bb..38fa949 100644 --- a/lib/repositories/repo.py +++ b/lib/repositories/repo.py @@ -20,6 +20,7 @@ def __new__(cls, *args, **kwargs): return cls._instances[cls] def __init__(self, collection_name: str): + self._entered = False if not getattr(self, '_initialized', False): self._collection_name = collection_name self._initialized_event = asyncio.Event() @@ -45,7 +46,8 @@ async def _async_init(self): self._initialized_event.set() async def __aenter__(self): - await self._initialized_event.wait() + if not self._entered: + await self._initialized_event.wait() return self async def __aexit__(self, exc_type, exc_value, traceback): @@ -63,7 +65,7 @@ def _initialize_connection(self): server_api=ServerApi("1"), maxIdleTimeMS=5000, connectTimeoutMS=5000, - serverSelectionTimeoutMS=15000, + serverSelectionTimeoutMS=30000, ) self._collection = self._client.rocketpy[self._collection_name] logger.info("MongoDB client initialized for %s", self.__class__) From 8151a2d138d15f00e1f4bf11bca3c749c1f09a61 Mon Sep 17 00:00:00 2001 From: GabrielBarberini Date: Thu, 13 Jun 2024 21:16:23 -0300 Subject: [PATCH 3/3] fix cyclic import warning --- Dockerfile | 2 +- lib/__init__.py | 3 --- lib/__main__.py | 2 +- lib/settings/gunicorn.py | 2 +- 4 files changed, 3 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index 282f994..573f83f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,4 +16,4 @@ RUN apt-get update && \ COPY ./lib /app/lib -CMD ["gunicorn", "-c", "lib/settings/gunicorn.py", "-w", "1", "--threads=2", "-k", "uvicorn.workers.UvicornWorker", "lib:app", "--log-level", "Debug", "-b", "0.0.0.0:3000", "--timeout", "30"] +CMD ["gunicorn", "-c", "lib/settings/gunicorn.py", "-w", "1", "--threads=2", "-k", "uvicorn.workers.UvicornWorker", "lib.api:app", "--log-level", "Debug", "-b", "0.0.0.0:3000", "--timeout", "30"] diff --git a/lib/__init__.py b/lib/__init__.py index db869db..2f27218 100644 --- a/lib/__init__.py +++ b/lib/__init__.py @@ -23,6 +23,3 @@ def parse_error(error): exc_type = type(error).__name__ exc_obj = f"{error}".replace("\n", " ").replace(" ", " ") return f"{exc_type} exception: {exc_obj}" - - -from .api import app # noqa diff --git a/lib/__main__.py b/lib/__main__.py index 311e8bf..9be9014 100644 --- a/lib/__main__.py +++ b/lib/__main__.py @@ -1,5 +1,5 @@ # __main__.py -from lib import app +from lib.api import app if __name__ == '__main__': app.run() diff --git a/lib/settings/gunicorn.py b/lib/settings/gunicorn.py index 7190073..b85352d 100644 --- a/lib/settings/gunicorn.py +++ b/lib/settings/gunicorn.py @@ -10,7 +10,7 @@ def post_fork(server, worker): # pylint: disable=unused-argument service_version="1.2.0", deployment_environment="production", ) - from lib import ( # pylint: disable=import-outside-toplevel + from lib.api import ( # pylint: disable=import-outside-toplevel app as fastapi_server, )