2022-10-27 10:07:02 +02:00
|
|
|
import flask
|
|
|
|
import limits.storage
|
|
|
|
|
|
|
|
from app.parallel_limiter import set_redis_concurrent_lock
|
2024-01-30 18:29:59 +01:00
|
|
|
from app.rate_limiter import set_redis_concurrent_lock as rate_limit_set_redis
|
2022-10-27 10:07:02 +02:00
|
|
|
from app.session import RedisSessionStore
|
|
|
|
|
|
|
|
|
|
|
|
def initialize_redis_services(app: flask.Flask, redis_url: str):
|
2023-01-11 16:25:35 +01:00
|
|
|
if redis_url.startswith("redis://") or redis_url.startswith("rediss://"):
|
2022-10-27 10:07:02 +02:00
|
|
|
storage = limits.storage.RedisStorage(redis_url)
|
|
|
|
app.session_interface = RedisSessionStore(storage.storage, storage.storage, app)
|
|
|
|
set_redis_concurrent_lock(storage)
|
2024-01-30 18:29:59 +01:00
|
|
|
rate_limit_set_redis(storage)
|
2022-10-27 10:07:02 +02:00
|
|
|
elif redis_url.startswith("redis+sentinel://"):
|
|
|
|
storage = limits.storage.RedisSentinelStorage(redis_url)
|
|
|
|
app.session_interface = RedisSessionStore(
|
|
|
|
storage.storage, storage.storage_slave, app
|
|
|
|
)
|
|
|
|
set_redis_concurrent_lock(storage)
|
2024-01-30 18:29:59 +01:00
|
|
|
rate_limit_set_redis(storage)
|
2022-10-27 10:07:02 +02:00
|
|
|
else:
|
|
|
|
raise RuntimeError(
|
|
|
|
f"Tried to set_redis_session with an invalid redis url: ${redis_url}"
|
|
|
|
)
|