Skip to content

Commit

Permalink
SQL cleanup thread or per requests (#211)
Browse files Browse the repository at this point in the history
Also use retry decorator to storage exceptions
  • Loading branch information
Lxstr authored Feb 15, 2024
2 parents 95da35b + 9af1b58 commit 0467483
Show file tree
Hide file tree
Showing 15 changed files with 246 additions and 48 deletions.
13 changes: 8 additions & 5 deletions .github/workflows/ruff.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
name: Ruff
on: [push, pull_request]
jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: chartboost/ruff-action@v1
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: chartboost/ruff-action@v1
with:
version: 0.1.11
args: --select B
31 changes: 18 additions & 13 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
name: Run unittests
on: [push, pull_request]
jobs:
unittests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: supercharge/[email protected]
- uses: niden/actions-memcached@v7
- uses: actions/setup-python@v4
with:
python-version: 'pypy3.9'
- name: Install testing requirements
run: pip3 install -r requirements/pytest.txt
- name: Run tests
run: pytest tests
unittests:
runs-on: ubuntu-latest
services:
mongodb:
image: mongo
ports:
- 27017:27017
steps:
- uses: actions/checkout@v3
- uses: supercharge/[email protected]
- uses: niden/actions-memcached@v7
- uses: actions/setup-python@v4
with:
python-version: "pypy3.9"
- name: Install testing requirements
run: pip3 install -r requirements/pytest.txt
- name: Run tests
run: pytest tests
1 change: 0 additions & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ API
Session id, internally we use :func:`secrets.token_urlsafe` to generate one
session id. You can access it with ``session.sid``.

.. autoclass:: NullSessionInterface
.. autoclass:: RedisSessionInterface
.. autoclass:: MemcachedSessionInterface
.. autoclass:: FileSystemSessionInterface
Expand Down
15 changes: 14 additions & 1 deletion docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,21 @@ Flask-Session Configuration Values

Default: ``32``

.. py:data:: SESSION_CLEANUP_N_REQUESTS
Only for non Time-to-live backends, such as SQL.

The average number of requests after which Flask-Session will perform session cleanup.

For best performance, it is recommended to instead use the app command ``flask session_cleanup`` with a cron task or database scheduler to perform session cleanup.

Default: ``None``

.. versionadded:: 0.6
``SESSION_ID_LENGTH``
``SESSION_ID_LENGTH``

.. versionadded:: 0.7
``SESSION_CLEANUP_N_REQUESTS``

Backend-specific Configuration Values
---------------------------------------
Expand Down
7 changes: 4 additions & 3 deletions examples/hello.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from flask import Flask, session

from flask_session import Session

app = Flask(__name__)
app.config.from_object(__name__)
app.config.update(
{
"SESSION_TYPE": "sqlalchemy",
"SQLALCHEMY_DATABASE_URI": "sqlite:////tmp/test.db",
"SQLALCHEMY_USE_SIGNER": True,
"SESSION_TYPE": "mongodb",
# "SQLALCHEMY_DATABASE_URI": "sqlite:////tmp/test.db",
# "SQLALCHEMY_USE_SIGNER": True,
}
)
Session(app)
Expand Down
4 changes: 4 additions & 0 deletions src/flask_session/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ def _get_interface(self, app):
SESSION_SQLALCHEMY_BIND_KEY = config.get(
"SESSION_SQLALCHEMY_BIND_KEY", Defaults.SESSION_SQLALCHEMY_BIND_KEY
)
SESSION_CLEANUP_N_REQUESTS = config.get(
"SESSION_CLEANUP_N_REQUESTS", Defaults.SESSION_CLEANUP_N_REQUESTS
)

common_params = {
"app": app,
Expand Down Expand Up @@ -147,6 +150,7 @@ def _get_interface(self, app):
sequence=SESSION_SQLALCHEMY_SEQUENCE,
schema=SESSION_SQLALCHEMY_SCHEMA,
bind_key=SESSION_SQLALCHEMY_BIND_KEY,
cleanup_n_requests=SESSION_CLEANUP_N_REQUESTS,
)
else:
raise RuntimeError(f"Unrecognized value for SESSION_TYPE: {SESSION_TYPE}")
Expand Down
62 changes: 62 additions & 0 deletions src/flask_session/_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""
MIT License
Copyright (c) 2023 giuppep
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import time
from functools import wraps
from typing import Any, Callable

from flask import current_app


def retry_query(
*, max_attempts: int = 3, delay: float = 0.3, backoff: int = 2
) -> Callable[..., Any]:
"""Decorator to retry a query when an OperationalError is raised.
Args:
max_attempts: Maximum number of attempts. Defaults to 3.
delay: Delay between attempts in seconds. Defaults to 0.3.
backoff: Backoff factor. Defaults to 2.
"""

def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
@wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any:
for attempt in range(max_attempts):
try:
return func(*args, **kwargs)
# TODO: use proper exception type
except Exception as e:
if attempt == max_attempts - 1:
raise e

sleep_time = delay * backoff**attempt
current_app.logger.exception(
f"Exception when querying database ({e})."
f"Retrying ({attempt + 1}/{max_attempts}) in {sleep_time:.2f}s."
)
time.sleep(sleep_time)

return wrapper

return decorator
3 changes: 3 additions & 0 deletions src/flask_session/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class Defaults:
SESSION_PERMANENT = True
SESSION_SID_LENGTH = 3

# Clean up settings for non TTL backends (SQL, PostgreSQL, etc.)
SESSION_CLEANUP_N_REQUESTS = None

# Redis settings
SESSION_REDIS = None

Expand Down
Loading

0 comments on commit 0467483

Please sign in to comment.