Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements gzpi middleware for payloads above 1kb #24

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
3 changes: 0 additions & 3 deletions lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# __main__.py
from lib import app
from lib.api import app

if __name__ == '__main__':
app.run()
4 changes: 4 additions & 0 deletions lib/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions lib/repositories/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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):
Expand All @@ -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__)
Expand Down
2 changes: 1 addition & 1 deletion lib/settings/gunicorn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down
Loading