From d0b13cf38814db23842e50200991f43d00bbf688 Mon Sep 17 00:00:00 2001 From: Jan Kessler Date: Thu, 27 Mar 2025 08:51:55 +0100 Subject: [PATCH] prefix sentinel envs with redis_ --- backend/open_webui/config.py | 8 ++++---- backend/open_webui/env.py | 4 ++-- backend/open_webui/main.py | 6 +++--- backend/open_webui/socket/main.py | 10 +++++----- backend/open_webui/socket/utils.py | 8 ++++---- backend/open_webui/utils/redis.py | 12 ++++++------ 6 files changed, 24 insertions(+), 24 deletions(-) diff --git a/backend/open_webui/config.py b/backend/open_webui/config.py index 900645c83..3827f4183 100644 --- a/backend/open_webui/config.py +++ b/backend/open_webui/config.py @@ -19,8 +19,8 @@ from open_webui.env import ( DATABASE_URL, ENV, REDIS_URL, - SENTINEL_PORT, - SENTINEL_HOSTS, + REDIS_SENTINEL_HOSTS, + REDIS_SENTINEL_PORT, FRONTEND_BUILD_DIR, OFFLINE_MODE, OPEN_WEBUI_DIR, @@ -254,11 +254,11 @@ class AppConfig: _state: dict[str, PersistentConfig] _redis: Optional[redis.Redis] = None - def __init__(self, redis_url: Optional[str] = None, sentinels: Optional[list] = []): + def __init__(self, redis_url: Optional[str] = None, redis_sentinels: Optional[list] = []): super().__setattr__("_state", {}) if redis_url: super().__setattr__( - "_redis", get_redis_connection(redis_url, sentinels, decode_responses=True) + "_redis", get_redis_connection(redis_url, redis_sentinels, decode_responses=True) ) def __setattr__(self, key, value): diff --git a/backend/open_webui/env.py b/backend/open_webui/env.py index 1f7f58445..0c1ee61a0 100644 --- a/backend/open_webui/env.py +++ b/backend/open_webui/env.py @@ -323,8 +323,8 @@ ENABLE_REALTIME_CHAT_SAVE = ( #################################### REDIS_URL = os.environ.get("REDIS_URL", "") -SENTINEL_HOSTS = os.environ.get("SENTINEL_HOSTS", "") -SENTINEL_PORT = os.environ.get("SENTINEL_PORT", "26379") +REDIS_SENTINEL_HOSTS = os.environ.get("REDIS_SENTINEL_HOSTS", "") +REDIS_SENTINEL_PORT = os.environ.get("REDIS_SENTINEL_PORT", "26379") #################################### # WEBUI_AUTH (Required for security) diff --git a/backend/open_webui/main.py b/backend/open_webui/main.py index eeac2b3c7..d2c435857 100644 --- a/backend/open_webui/main.py +++ b/backend/open_webui/main.py @@ -315,8 +315,8 @@ from open_webui.env import ( AUDIT_LOG_LEVEL, CHANGELOG, REDIS_URL, - SENTINEL_HOSTS, - SENTINEL_PORT, + REDIS_SENTINEL_HOSTS, + REDIS_SENTINEL_PORT, GLOBAL_LOG_LEVEL, MAX_BODY_LOG_SIZE, SAFE_MODE, @@ -426,7 +426,7 @@ app = FastAPI( oauth_manager = OAuthManager(app) -app.state.config = AppConfig(redis_url=REDIS_URL, sentinels=get_sentinels_from_env(SENTINEL_HOSTS, SENTINEL_PORT)) +app.state.config = AppConfig(redis_url=REDIS_URL, redis_sentinels=get_sentinels_from_env(REDIS_SENTINEL_HOSTS, REDIS_SENTINEL_PORT)) app.state.WEBUI_NAME = WEBUI_NAME app.state.LICENSE_METADATA = None diff --git a/backend/open_webui/socket/main.py b/backend/open_webui/socket/main.py index b681d7e3b..0d391c73d 100644 --- a/backend/open_webui/socket/main.py +++ b/backend/open_webui/socket/main.py @@ -64,16 +64,16 @@ TIMEOUT_DURATION = 3 if WEBSOCKET_MANAGER == "redis": log.debug("Using Redis to manage websockets.") - sentinels=get_sentinels_from_env(WEBSOCKET_SENTINEL_HOSTS, WEBSOCKET_SENTINEL_PORT) - SESSION_POOL = RedisDict("open-webui:session_pool", redis_url=WEBSOCKET_REDIS_URL, sentinels=sentinels) - USER_POOL = RedisDict("open-webui:user_pool", redis_url=WEBSOCKET_REDIS_URL, sentinels=sentinels) - USAGE_POOL = RedisDict("open-webui:usage_pool", redis_url=WEBSOCKET_REDIS_URL, sentinels=sentinels) + redis_sentinels=get_sentinels_from_env(WEBSOCKET_SENTINEL_HOSTS, WEBSOCKET_SENTINEL_PORT) + SESSION_POOL = RedisDict("open-webui:session_pool", redis_url=WEBSOCKET_REDIS_URL, redis_sentinels=redis_sentinels) + USER_POOL = RedisDict("open-webui:user_pool", redis_url=WEBSOCKET_REDIS_URL, redis_sentinels=redis_sentinels) + USAGE_POOL = RedisDict("open-webui:usage_pool", redis_url=WEBSOCKET_REDIS_URL, redis_sentinels=redis_sentinels) clean_up_lock = RedisLock( redis_url=WEBSOCKET_REDIS_URL, lock_name="usage_cleanup_lock", timeout_secs=WEBSOCKET_REDIS_LOCK_TIMEOUT, - sentinels=sentinels, + redis_sentinels=redis_sentinels, ) aquire_func = clean_up_lock.aquire_lock renew_func = clean_up_lock.renew_lock diff --git a/backend/open_webui/socket/utils.py b/backend/open_webui/socket/utils.py index 00aad57e2..a63815c02 100644 --- a/backend/open_webui/socket/utils.py +++ b/backend/open_webui/socket/utils.py @@ -3,12 +3,12 @@ import uuid from open_webui.utils.redis import get_redis_connection class RedisLock: - def __init__(self, redis_url, lock_name, timeout_secs, sentinels=[]): + def __init__(self, redis_url, lock_name, timeout_secs, redis_sentinels=[]): self.lock_name = lock_name self.lock_id = str(uuid.uuid4()) self.timeout_secs = timeout_secs self.lock_obtained = False - self.redis = get_redis_connection(redis_url, sentinels, decode_responses=True) + self.redis = get_redis_connection(redis_url, redis_sentinels, decode_responses=True) def aquire_lock(self): # nx=True will only set this key if it _hasn't_ already been set @@ -30,9 +30,9 @@ class RedisLock: class RedisDict: - def __init__(self, name, redis_url, sentinels=[]): + def __init__(self, name, redis_url, redis_sentinels=[]): self.name = name - self.redis = get_redis_connection(redis_url, sentinels, decode_responses=True) + self.redis = get_redis_connection(redis_url, redis_sentinels, decode_responses=True) def __setitem__(self, key, value): serialized_value = json.dumps(value) diff --git a/backend/open_webui/utils/redis.py b/backend/open_webui/utils/redis.py index ff39f96ac..3ad1f78be 100644 --- a/backend/open_webui/utils/redis.py +++ b/backend/open_webui/utils/redis.py @@ -16,11 +16,11 @@ def parse_redis_sentinel_url(redis_url): "db": int(parsed_url.path.lstrip("/") or 0), } -def get_redis_connection(redis_url, sentinels, decode_responses=True): - if sentinels: +def get_redis_connection(redis_url, redis_sentinels, decode_responses=True): + if redis_sentinels: redis_config = parse_redis_sentinel_url(redis_url) sentinel = redis.sentinel.Sentinel( - sentinels, + redis_sentinels, port=redis_config['port'], db=redis_config['db'], username=redis_config['username'], @@ -34,9 +34,9 @@ def get_redis_connection(redis_url, sentinels, decode_responses=True): # Standard Redis connection return redis.Redis.from_url(redis_url, decode_responses=decode_responses) -def get_sentinels_from_env(SENTINEL_HOSTS, SENTINEL_PORT): - sentinel_hosts=SENTINEL_HOSTS.split(',') - sentinel_port=int(SENTINEL_PORT) +def get_sentinels_from_env(sentinel_hosts_env, sentinel_port_env): + sentinel_hosts=sentinel_hosts_env.split(',') + sentinel_port=int(sentinel_port_env) return [(host, sentinel_port) for host in sentinel_hosts] class AsyncRedisSentinelManager(socketio.AsyncRedisManager):