mirror of
https://github.com/open-webui/open-webui
synced 2025-06-26 18:26:48 +00:00
refac: async redis
This commit is contained in:
parent
8da1ce358f
commit
db3c26ab7a
@ -510,6 +510,7 @@ async def lifespan(app: FastAPI):
|
|||||||
redis_sentinels=get_sentinels_from_env(
|
redis_sentinels=get_sentinels_from_env(
|
||||||
REDIS_SENTINEL_HOSTS, REDIS_SENTINEL_PORT
|
REDIS_SENTINEL_HOSTS, REDIS_SENTINEL_PORT
|
||||||
),
|
),
|
||||||
|
async_mode=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
if isinstance(app.state.redis, Redis):
|
if isinstance(app.state.redis, Redis):
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
import socketio
|
import socketio
|
||||||
import redis
|
|
||||||
from redis import asyncio as aioredis
|
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
@ -20,26 +18,45 @@ def parse_redis_service_url(redis_url):
|
|||||||
|
|
||||||
|
|
||||||
def get_redis_connection(
|
def get_redis_connection(
|
||||||
redis_url, redis_sentinels, decode_responses=True
|
redis_url, redis_sentinels, async_mode=False, decode_responses=True
|
||||||
) -> Optional[redis.Redis]:
|
):
|
||||||
if redis_sentinels:
|
if async_mode:
|
||||||
redis_config = parse_redis_service_url(redis_url)
|
import redis.asyncio as redis
|
||||||
sentinel = redis.sentinel.Sentinel(
|
|
||||||
redis_sentinels,
|
|
||||||
port=redis_config["port"],
|
|
||||||
db=redis_config["db"],
|
|
||||||
username=redis_config["username"],
|
|
||||||
password=redis_config["password"],
|
|
||||||
decode_responses=decode_responses,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Get a master connection from Sentinel
|
# If using sentinel in async mode
|
||||||
return sentinel.master_for(redis_config["service"])
|
if redis_sentinels:
|
||||||
elif redis_url:
|
redis_config = parse_redis_service_url(redis_url)
|
||||||
# Standard Redis connection
|
sentinel = redis.sentinel.Sentinel(
|
||||||
return redis.Redis.from_url(redis_url, decode_responses=decode_responses)
|
redis_sentinels,
|
||||||
|
port=redis_config["port"],
|
||||||
|
db=redis_config["db"],
|
||||||
|
username=redis_config["username"],
|
||||||
|
password=redis_config["password"],
|
||||||
|
decode_responses=decode_responses,
|
||||||
|
)
|
||||||
|
return sentinel.master_for(redis_config["service"])
|
||||||
|
elif redis_url:
|
||||||
|
return redis.from_url(redis_url, decode_responses=decode_responses)
|
||||||
|
else:
|
||||||
|
return None
|
||||||
else:
|
else:
|
||||||
return None
|
import redis
|
||||||
|
|
||||||
|
if redis_sentinels:
|
||||||
|
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"],
|
||||||
|
decode_responses=decode_responses,
|
||||||
|
)
|
||||||
|
return sentinel.master_for(redis_config["service"])
|
||||||
|
elif redis_url:
|
||||||
|
return redis.Redis.from_url(redis_url, decode_responses=decode_responses)
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_sentinels_from_env(sentinel_hosts_env, sentinel_port_env):
|
def get_sentinels_from_env(sentinel_hosts_env, sentinel_port_env):
|
||||||
|
Loading…
Reference in New Issue
Block a user