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

Release DB sessions before forwarding requests #14

Merged
merged 1 commit into from
Mar 14, 2025
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
6 changes: 3 additions & 3 deletions apigateway/services.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Module defining API Gateway services. """
"""Module defining API Gateway services."""

import hashlib
import json
Expand Down Expand Up @@ -99,7 +99,7 @@ def init_app(self, app: Flask):
app (Flask): The Flask app to initialize the AuthService with.
"""
super().init_app(app)
bearer_cls = create_bearer_token_validator(app.db.session, OAuth2Token)
bearer_cls = create_bearer_token_validator(extensions.db.session, OAuth2Token)
self.require_oauth.register_token_validator(bearer_cls())
self._register_hooks(app)

Expand Down Expand Up @@ -1161,7 +1161,7 @@ def init_app(self, app: Flask):
Security.init_app(
self,
app,
datastore=SQLAlchemyUserDatastore(app.db, User, Role),
datastore=SQLAlchemyUserDatastore(extensions.db, User, Role),
anonymous_user=AnonymousUser,
)

Expand Down
6 changes: 6 additions & 0 deletions apigateway/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from flask.views import View
from flask_login import current_user

from apigateway import extensions
from apigateway.email_templates import (
AccountRegistrationAttemptEmail,
EmailTemplate,
Expand Down Expand Up @@ -203,6 +204,11 @@ def dispatch_request(self, **kwargs) -> Tuple[bytes, int]:
Returns:
Tuple[bytes, int]: A tuple containing the content of the response and the status code.
"""

# flask_principal holds an active DB session.
# Release the session early to avoid blocking during long running requests to external endpoints.
extensions.db.session.remove()

return self._proxy_request()

def _proxy_request(self) -> Tuple[bytes, int]:
Expand Down