forked from pallets-eco/flask-session
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from MartinUrban/redis-sentinel
Add support for Redis Sentinel
- Loading branch information
Showing
11 changed files
with
286 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,34 +6,9 @@ jobs: | |
strategy: | ||
matrix: | ||
python-version: [3.8, 3.9, 3.10, 3.11, 3.12] | ||
services: | ||
mongodb: | ||
image: mongo | ||
ports: | ||
- 27017:27017 | ||
dynamodb: | ||
image: amazon/dynamodb-local | ||
ports: | ||
- 8000:8000 | ||
|
||
postgresql: | ||
image: postgres:latest | ||
ports: | ||
- 5433:5432 | ||
env: | ||
POSTGRES_PASSWORD: pwd | ||
POSTGRES_USER: root | ||
POSTGRES_DB: dummy | ||
# Set health checks to wait until postgres has started | ||
options: >- | ||
--health-cmd pg_isready | ||
--health-interval 10s | ||
--health-timeout 5s | ||
--health-retries 5 | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: supercharge/[email protected] | ||
- uses: niden/actions-memcached@v7 | ||
- uses: hoverkraft-tech/[email protected] | ||
- name: Install testing requirements | ||
run: pip3 install -r requirements/dev.txt | ||
- name: Run tests | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from .redis import RedisSession, RedisSessionInterface # noqa: F401 | ||
from .redis_sentinel import RedisSentinelSession, RedisSentinelSessionInterface |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from typing import Optional | ||
|
||
from flask import Flask | ||
from redis import Sentinel | ||
|
||
from .redis import RedisSessionInterface | ||
from ..base import ServerSideSession | ||
from ..defaults import Defaults | ||
|
||
|
||
class RedisSentinelSession(ServerSideSession): | ||
pass | ||
|
||
|
||
class RedisSentinelSessionInterface(RedisSessionInterface): | ||
"""Uses the Redis key-value store deployed in a high availability mode as a session storage. (`redis-py` required) | ||
:param client: A ``redis.Sentinel`` instance. | ||
:param master: The name of the master node. | ||
:param key_prefix: A prefix that is added to all storage keys. | ||
:param use_signer: Whether to sign the session id cookie or not. | ||
:param permanent: Whether to use permanent session or not. | ||
:param sid_length: The length of the generated session id in bytes. | ||
:param serialization_format: The serialization format to use for the session data. | ||
""" | ||
|
||
session_class = RedisSentinelSession | ||
ttl = True | ||
|
||
def __init__( | ||
self, | ||
app: Flask, | ||
client: Optional[Sentinel] = Defaults.SESSION_REDIS_SENTINEL, | ||
master: str = Defaults.SESSION_REDIS_SENTINEL_MASTER_SET, | ||
key_prefix: str = Defaults.SESSION_KEY_PREFIX, | ||
use_signer: bool = Defaults.SESSION_USE_SIGNER, | ||
permanent: bool = Defaults.SESSION_PERMANENT, | ||
sid_length: int = Defaults.SESSION_ID_LENGTH, | ||
serialization_format: str = Defaults.SESSION_SERIALIZATION_FORMAT, | ||
): | ||
if client is None or not isinstance(client, Sentinel): | ||
raise TypeError("No valid Sentinel instance provided.") | ||
self.sentinel = client | ||
self.master = master | ||
super().__init__( | ||
app, self.client, key_prefix, use_signer, permanent, sid_length, serialization_format | ||
) | ||
self._client = None | ||
|
||
@property | ||
def client(self): | ||
return self.sentinel.master_for(self.master) | ||
|
||
@client.setter | ||
def client(self, value): | ||
# the _client is only needed and the setter only needed for the inheritance to work | ||
self._client = value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters