mirror of
				https://github.com/open-webui/open-webui
				synced 2025-06-26 18:26:48 +00:00 
			
		
		
		
	prefix sentinel envs with redis_
This commit is contained in:
		
							parent
							
								
									e68cd9b671
								
							
						
					
					
						commit
						d0b13cf388
					
				@ -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):
 | 
			
		||||
 | 
			
		||||
@ -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)
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
 | 
			
		||||
@ -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)
 | 
			
		||||
 | 
			
		||||
@ -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):
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user