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

Avoid opening sessions for static resources #5491

Closed
markhobson opened this issue May 30, 2024 · 4 comments
Closed

Avoid opening sessions for static resources #5491

markhobson opened this issue May 30, 2024 · 4 comments

Comments

@markhobson
Copy link

Every HTTP request currently results in an HTTP session being opened via SessionInterface.open_session. When a server-side session implementation is used, like Flask-Session, this results in storage writes to update the expiry date. Requests for static resources are also subject to this, which can degrade performance.

It would be useful to easily opt static resources out of sessions. Currently this is only possible with a custom session interface, for example:

class StaticRequestFilteringSessionInterface(SessionInterface):
    def __init__(self, app):
        self._delegate = app.session_interface
        self._exclude_path_prefix = app.static_url_path + "/"

    def open_session(self, app, request):
        if request.path.startswith(self._exclude_path_prefix):
            return self.make_null_session(app)

        return self._delegate.open_session(app, request)

    def save_session(self, app, session, response):
        return self._delegate.save_session(app, session, response)

Configured with:

from flask_session import Session

...
Session(app)
app.session_interface = StaticRequestFilteringSessionInterface(app)
@davidism
Copy link
Member

davidism commented May 30, 2024

In production, if performance is an issue, you want to serve your static files directly through your HTTP server, not through Flask. Then this doesn't apply anyway. In other cases, you may want to serve static files with other conditions applied, at which point it's not clear sessions should always be excluded. I don't think it's worth adding the complexity of implementation and explanation to Flask itself, especially when it's already possible to write a custom session (a completely supported and intended public API) to do whatever you want for your case.

@davidism davidism closed this as not planned Won't fix, can't repro, duplicate, stale May 30, 2024
@markhobson
Copy link
Author

Thanks for the swift reply. I appreciate it's not core functionality, perhaps something that Flask-Session would consider instead.

@markhobson
Copy link
Author

Raised pallets-eco/flask-session#254.

@davidism
Copy link
Member

I don't think it makes sense there either, for the same reasons.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants