prefix sentinel envs with redis_

This commit is contained in:
Jan Kessler 2025-03-27 08:51:55 +01:00
parent e68cd9b671
commit d0b13cf388
No known key found for this signature in database
GPG Key ID: FCF0DCB4ADFC53E7
6 changed files with 24 additions and 24 deletions

View File

@ -19,8 +19,8 @@ from open_webui.env import (
DATABASE_URL, DATABASE_URL,
ENV, ENV,
REDIS_URL, REDIS_URL,
SENTINEL_PORT, REDIS_SENTINEL_HOSTS,
SENTINEL_HOSTS, REDIS_SENTINEL_PORT,
FRONTEND_BUILD_DIR, FRONTEND_BUILD_DIR,
OFFLINE_MODE, OFFLINE_MODE,
OPEN_WEBUI_DIR, OPEN_WEBUI_DIR,
@ -254,11 +254,11 @@ class AppConfig:
_state: dict[str, PersistentConfig] _state: dict[str, PersistentConfig]
_redis: Optional[redis.Redis] = None _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", {}) super().__setattr__("_state", {})
if redis_url: if redis_url:
super().__setattr__( 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): def __setattr__(self, key, value):

View File

@ -323,8 +323,8 @@ ENABLE_REALTIME_CHAT_SAVE = (
#################################### ####################################
REDIS_URL = os.environ.get("REDIS_URL", "") REDIS_URL = os.environ.get("REDIS_URL", "")
SENTINEL_HOSTS = os.environ.get("SENTINEL_HOSTS", "") REDIS_SENTINEL_HOSTS = os.environ.get("REDIS_SENTINEL_HOSTS", "")
SENTINEL_PORT = os.environ.get("SENTINEL_PORT", "26379") REDIS_SENTINEL_PORT = os.environ.get("REDIS_SENTINEL_PORT", "26379")
#################################### ####################################
# WEBUI_AUTH (Required for security) # WEBUI_AUTH (Required for security)

View File

@ -315,8 +315,8 @@ from open_webui.env import (
AUDIT_LOG_LEVEL, AUDIT_LOG_LEVEL,
CHANGELOG, CHANGELOG,
REDIS_URL, REDIS_URL,
SENTINEL_HOSTS, REDIS_SENTINEL_HOSTS,
SENTINEL_PORT, REDIS_SENTINEL_PORT,
GLOBAL_LOG_LEVEL, GLOBAL_LOG_LEVEL,
MAX_BODY_LOG_SIZE, MAX_BODY_LOG_SIZE,
SAFE_MODE, SAFE_MODE,
@ -426,7 +426,7 @@ app = FastAPI(
oauth_manager = OAuthManager(app) 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.WEBUI_NAME = WEBUI_NAME
app.state.LICENSE_METADATA = None app.state.LICENSE_METADATA = None

View File

@ -64,16 +64,16 @@ TIMEOUT_DURATION = 3
if WEBSOCKET_MANAGER == "redis": if WEBSOCKET_MANAGER == "redis":
log.debug("Using Redis to manage websockets.") log.debug("Using Redis to manage websockets.")
sentinels=get_sentinels_from_env(WEBSOCKET_SENTINEL_HOSTS, WEBSOCKET_SENTINEL_PORT) redis_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) 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, sentinels=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, sentinels=sentinels) USAGE_POOL = RedisDict("open-webui:usage_pool", redis_url=WEBSOCKET_REDIS_URL, redis_sentinels=redis_sentinels)
clean_up_lock = RedisLock( clean_up_lock = RedisLock(
redis_url=WEBSOCKET_REDIS_URL, redis_url=WEBSOCKET_REDIS_URL,
lock_name="usage_cleanup_lock", lock_name="usage_cleanup_lock",
timeout_secs=WEBSOCKET_REDIS_LOCK_TIMEOUT, timeout_secs=WEBSOCKET_REDIS_LOCK_TIMEOUT,
sentinels=sentinels, redis_sentinels=redis_sentinels,
) )
aquire_func = clean_up_lock.aquire_lock aquire_func = clean_up_lock.aquire_lock
renew_func = clean_up_lock.renew_lock renew_func = clean_up_lock.renew_lock

View File

@ -3,12 +3,12 @@ import uuid
from open_webui.utils.redis import get_redis_connection from open_webui.utils.redis import get_redis_connection
class RedisLock: 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_name = lock_name
self.lock_id = str(uuid.uuid4()) self.lock_id = str(uuid.uuid4())
self.timeout_secs = timeout_secs self.timeout_secs = timeout_secs
self.lock_obtained = False 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): def aquire_lock(self):
# nx=True will only set this key if it _hasn't_ already been set # nx=True will only set this key if it _hasn't_ already been set
@ -30,9 +30,9 @@ class RedisLock:
class RedisDict: class RedisDict:
def __init__(self, name, redis_url, sentinels=[]): def __init__(self, name, redis_url, redis_sentinels=[]):
self.name = name 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): def __setitem__(self, key, value):
serialized_value = json.dumps(value) serialized_value = json.dumps(value)

View File

@ -16,11 +16,11 @@ def parse_redis_sentinel_url(redis_url):
"db": int(parsed_url.path.lstrip("/") or 0), "db": int(parsed_url.path.lstrip("/") or 0),
} }
def get_redis_connection(redis_url, sentinels, decode_responses=True): def get_redis_connection(redis_url, redis_sentinels, decode_responses=True):
if sentinels: if redis_sentinels:
redis_config = parse_redis_sentinel_url(redis_url) redis_config = parse_redis_sentinel_url(redis_url)
sentinel = redis.sentinel.Sentinel( sentinel = redis.sentinel.Sentinel(
sentinels, redis_sentinels,
port=redis_config['port'], port=redis_config['port'],
db=redis_config['db'], db=redis_config['db'],
username=redis_config['username'], username=redis_config['username'],
@ -34,9 +34,9 @@ def get_redis_connection(redis_url, sentinels, decode_responses=True):
# Standard Redis connection # Standard Redis connection
return redis.Redis.from_url(redis_url, decode_responses=decode_responses) return redis.Redis.from_url(redis_url, decode_responses=decode_responses)
def get_sentinels_from_env(SENTINEL_HOSTS, SENTINEL_PORT): def get_sentinels_from_env(sentinel_hosts_env, sentinel_port_env):
sentinel_hosts=SENTINEL_HOSTS.split(',') sentinel_hosts=sentinel_hosts_env.split(',')
sentinel_port=int(SENTINEL_PORT) sentinel_port=int(sentinel_port_env)
return [(host, sentinel_port) for host in sentinel_hosts] return [(host, sentinel_port) for host in sentinel_hosts]
class AsyncRedisSentinelManager(socketio.AsyncRedisManager): class AsyncRedisSentinelManager(socketio.AsyncRedisManager):