mirror of
https://github.com/open-webui/open-webui.git
synced 2026-05-05 10:28:06 -05:00
refac
This commit is contained in:
@@ -40,7 +40,7 @@ class SentinelRedisProxy:
|
||||
if not callable(orig_attr):
|
||||
return orig_attr
|
||||
|
||||
FACTORY_METHODS = {"pipeline", "pubsub", "monitor", "client", "transaction"}
|
||||
FACTORY_METHODS = {'pipeline', 'pubsub', 'monitor', 'client', 'transaction'}
|
||||
if item in FACTORY_METHODS:
|
||||
return orig_attr
|
||||
|
||||
@@ -61,7 +61,7 @@ class SentinelRedisProxy:
|
||||
) as e:
|
||||
if i < REDIS_SENTINEL_MAX_RETRY_COUNT - 1:
|
||||
log.debug(
|
||||
"Redis sentinel fail-over (%s). Retry %s/%s",
|
||||
'Redis sentinel fail-over (%s). Retry %s/%s',
|
||||
type(e).__name__,
|
||||
i + 1,
|
||||
REDIS_SENTINEL_MAX_RETRY_COUNT,
|
||||
@@ -70,7 +70,7 @@ class SentinelRedisProxy:
|
||||
time.sleep(REDIS_RECONNECT_DELAY / 1000)
|
||||
continue
|
||||
log.error(
|
||||
"Redis operation failed after %s retries: %s",
|
||||
'Redis operation failed after %s retries: %s',
|
||||
REDIS_SENTINEL_MAX_RETRY_COUNT,
|
||||
e,
|
||||
)
|
||||
@@ -94,7 +94,7 @@ class SentinelRedisProxy:
|
||||
) as e:
|
||||
if i < REDIS_SENTINEL_MAX_RETRY_COUNT - 1:
|
||||
log.debug(
|
||||
"Redis sentinel fail-over (%s). Retry %s/%s",
|
||||
'Redis sentinel fail-over (%s). Retry %s/%s',
|
||||
type(e).__name__,
|
||||
i + 1,
|
||||
REDIS_SENTINEL_MAX_RETRY_COUNT,
|
||||
@@ -103,7 +103,7 @@ class SentinelRedisProxy:
|
||||
await asyncio.sleep(REDIS_RECONNECT_DELAY / 1000)
|
||||
continue
|
||||
log.error(
|
||||
"Redis operation failed after %s retries: %s",
|
||||
'Redis operation failed after %s retries: %s',
|
||||
REDIS_SENTINEL_MAX_RETRY_COUNT,
|
||||
e,
|
||||
)
|
||||
@@ -124,7 +124,7 @@ class SentinelRedisProxy:
|
||||
) as e:
|
||||
if i < REDIS_SENTINEL_MAX_RETRY_COUNT - 1:
|
||||
log.debug(
|
||||
"Redis sentinel fail-over (%s). Retry %s/%s",
|
||||
'Redis sentinel fail-over (%s). Retry %s/%s',
|
||||
type(e).__name__,
|
||||
i + 1,
|
||||
REDIS_SENTINEL_MAX_RETRY_COUNT,
|
||||
@@ -133,7 +133,7 @@ class SentinelRedisProxy:
|
||||
time.sleep(REDIS_RECONNECT_DELAY / 1000)
|
||||
continue
|
||||
log.error(
|
||||
"Redis operation failed after %s retries: %s",
|
||||
'Redis operation failed after %s retries: %s',
|
||||
REDIS_SENTINEL_MAX_RETRY_COUNT,
|
||||
e,
|
||||
)
|
||||
@@ -144,15 +144,15 @@ class SentinelRedisProxy:
|
||||
|
||||
def parse_redis_service_url(redis_url):
|
||||
parsed_url = urlparse(redis_url)
|
||||
if parsed_url.scheme != "redis" and parsed_url.scheme != "rediss":
|
||||
if parsed_url.scheme != 'redis' and parsed_url.scheme != 'rediss':
|
||||
raise ValueError("Invalid Redis URL scheme. Must be 'redis' or 'rediss'.")
|
||||
|
||||
return {
|
||||
"username": parsed_url.username or None,
|
||||
"password": parsed_url.password or None,
|
||||
"service": parsed_url.hostname or "mymaster",
|
||||
"port": parsed_url.port or 6379,
|
||||
"db": int(parsed_url.path.lstrip("/") or 0),
|
||||
'username': parsed_url.username or None,
|
||||
'password': parsed_url.password or None,
|
||||
'service': parsed_url.hostname or 'mymaster',
|
||||
'port': parsed_url.port or 6379,
|
||||
'db': int(parsed_url.path.lstrip('/') or 0),
|
||||
}
|
||||
|
||||
|
||||
@@ -160,14 +160,12 @@ def get_redis_client(async_mode=False):
|
||||
try:
|
||||
return get_redis_connection(
|
||||
redis_url=REDIS_URL,
|
||||
redis_sentinels=get_sentinels_from_env(
|
||||
REDIS_SENTINEL_HOSTS, REDIS_SENTINEL_PORT
|
||||
),
|
||||
redis_sentinels=get_sentinels_from_env(REDIS_SENTINEL_HOSTS, REDIS_SENTINEL_PORT),
|
||||
redis_cluster=REDIS_CLUSTER,
|
||||
async_mode=async_mode,
|
||||
)
|
||||
except Exception as e:
|
||||
log.debug(f"Failed to get Redis client: {e}")
|
||||
log.debug(f'Failed to get Redis client: {e}')
|
||||
return None
|
||||
|
||||
|
||||
@@ -178,7 +176,6 @@ def get_redis_connection(
|
||||
async_mode=False,
|
||||
decode_responses=True,
|
||||
):
|
||||
|
||||
cache_key = (
|
||||
redis_url,
|
||||
tuple(redis_sentinels) if redis_sentinels else (),
|
||||
@@ -199,24 +196,22 @@ def get_redis_connection(
|
||||
redis_config = parse_redis_service_url(redis_url)
|
||||
sentinel = redis.sentinel.Sentinel(
|
||||
redis_sentinels,
|
||||
port=redis_config["port"],
|
||||
db=redis_config["db"],
|
||||
username=redis_config["username"],
|
||||
password=redis_config["password"],
|
||||
port=redis_config['port'],
|
||||
db=redis_config['db'],
|
||||
username=redis_config['username'],
|
||||
password=redis_config['password'],
|
||||
decode_responses=decode_responses,
|
||||
socket_connect_timeout=REDIS_SOCKET_CONNECT_TIMEOUT,
|
||||
)
|
||||
connection = SentinelRedisProxy(
|
||||
sentinel,
|
||||
redis_config["service"],
|
||||
redis_config['service'],
|
||||
async_mode=async_mode,
|
||||
)
|
||||
elif redis_cluster:
|
||||
if not redis_url:
|
||||
raise ValueError("Redis URL must be provided for cluster mode.")
|
||||
return redis.cluster.RedisCluster.from_url(
|
||||
redis_url, decode_responses=decode_responses
|
||||
)
|
||||
raise ValueError('Redis URL must be provided for cluster mode.')
|
||||
return redis.cluster.RedisCluster.from_url(redis_url, decode_responses=decode_responses)
|
||||
elif redis_url:
|
||||
connection = redis.from_url(redis_url, decode_responses=decode_responses)
|
||||
else:
|
||||
@@ -226,28 +221,24 @@ def get_redis_connection(
|
||||
redis_config = parse_redis_service_url(redis_url)
|
||||
sentinel = redis.sentinel.Sentinel(
|
||||
redis_sentinels,
|
||||
port=redis_config["port"],
|
||||
db=redis_config["db"],
|
||||
username=redis_config["username"],
|
||||
password=redis_config["password"],
|
||||
port=redis_config['port'],
|
||||
db=redis_config['db'],
|
||||
username=redis_config['username'],
|
||||
password=redis_config['password'],
|
||||
decode_responses=decode_responses,
|
||||
socket_connect_timeout=REDIS_SOCKET_CONNECT_TIMEOUT,
|
||||
)
|
||||
connection = SentinelRedisProxy(
|
||||
sentinel,
|
||||
redis_config["service"],
|
||||
redis_config['service'],
|
||||
async_mode=async_mode,
|
||||
)
|
||||
elif redis_cluster:
|
||||
if not redis_url:
|
||||
raise ValueError("Redis URL must be provided for cluster mode.")
|
||||
return redis.cluster.RedisCluster.from_url(
|
||||
redis_url, decode_responses=decode_responses
|
||||
)
|
||||
raise ValueError('Redis URL must be provided for cluster mode.')
|
||||
return redis.cluster.RedisCluster.from_url(redis_url, decode_responses=decode_responses)
|
||||
elif redis_url:
|
||||
connection = redis.Redis.from_url(
|
||||
redis_url, decode_responses=decode_responses
|
||||
)
|
||||
connection = redis.Redis.from_url(redis_url, decode_responses=decode_responses)
|
||||
|
||||
_CONNECTION_CACHE[cache_key] = connection
|
||||
return connection
|
||||
@@ -255,7 +246,7 @@ def get_redis_connection(
|
||||
|
||||
def get_sentinels_from_env(sentinel_hosts_env, sentinel_port_env):
|
||||
if sentinel_hosts_env:
|
||||
sentinel_hosts = sentinel_hosts_env.split(",")
|
||||
sentinel_hosts = sentinel_hosts_env.split(',')
|
||||
sentinel_port = int(sentinel_port_env)
|
||||
return [(host, sentinel_port) for host in sentinel_hosts]
|
||||
return []
|
||||
@@ -263,12 +254,10 @@ def get_sentinels_from_env(sentinel_hosts_env, sentinel_port_env):
|
||||
|
||||
def get_sentinel_url_from_env(redis_url, sentinel_hosts_env, sentinel_port_env):
|
||||
redis_config = parse_redis_service_url(redis_url)
|
||||
username = redis_config["username"] or ""
|
||||
password = redis_config["password"] or ""
|
||||
auth_part = ""
|
||||
username = redis_config['username'] or ''
|
||||
password = redis_config['password'] or ''
|
||||
auth_part = ''
|
||||
if username or password:
|
||||
auth_part = f"{username}:{password}@"
|
||||
hosts_part = ",".join(
|
||||
f"{host}:{sentinel_port_env}" for host in sentinel_hosts_env.split(",")
|
||||
)
|
||||
return f"redis+sentinel://{auth_part}{hosts_part}/{redis_config['db']}/{redis_config['service']}"
|
||||
auth_part = f'{username}:{password}@'
|
||||
hosts_part = ','.join(f'{host}:{sentinel_port_env}' for host in sentinel_hosts_env.split(','))
|
||||
return f'redis+sentinel://{auth_part}{hosts_part}/{redis_config["db"]}/{redis_config["service"]}'
|
||||
|
||||
Reference in New Issue
Block a user