chore: format

This commit is contained in:
Timothy Jaeryang Baek
2025-03-28 11:47:14 -07:00
parent b8c1bb0ec5
commit 1ac87c55ff
6 changed files with 432 additions and 244 deletions

View File

@@ -3,6 +3,7 @@ import redis
from redis import asyncio as aioredis
from urllib.parse import urlparse
def parse_redis_sentinel_url(redis_url):
parsed_url = urlparse(redis_url)
if parsed_url.scheme != "redis":
@@ -11,39 +12,54 @@ def parse_redis_sentinel_url(redis_url):
return {
"username": parsed_url.username or None,
"password": parsed_url.password or None,
"service": parsed_url.hostname or 'mymaster',
"service": parsed_url.hostname or "mymaster",
"port": parsed_url.port or 6379,
"db": int(parsed_url.path.lstrip("/") or 0),
}
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(
redis_sentinels,
port=redis_config['port'],
db=redis_config['db'],
username=redis_config['username'],
password=redis_config['password'],
decode_responses=decode_responses
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
return sentinel.master_for(redis_config['service'])
return sentinel.master_for(redis_config["service"])
else:
# Standard Redis connection
return redis.Redis.from_url(redis_url, decode_responses=decode_responses)
def get_sentinels_from_env(sentinel_hosts_env, sentinel_port_env):
if sentinel_hosts_env:
sentinel_hosts=sentinel_hosts_env.split(',')
sentinel_port=int(sentinel_port_env)
sentinel_hosts = sentinel_hosts_env.split(",")
sentinel_port = int(sentinel_port_env)
return [(host, sentinel_port) for host in sentinel_hosts]
return []
class AsyncRedisSentinelManager(socketio.AsyncRedisManager):
def __init__(self, sentinel_hosts, sentinel_port=26379, redis_port=6379, service="mymaster", db=0,
username=None, password=None, channel='socketio', write_only=False, logger=None, redis_options=None):
def __init__(
self,
sentinel_hosts,
sentinel_port=26379,
redis_port=6379,
service="mymaster",
db=0,
username=None,
password=None,
channel="socketio",
write_only=False,
logger=None,
redis_options=None,
):
"""
Initialize the Redis Sentinel Manager.
This implementation mostly replicates the __init__ of AsyncRedisManager and
@@ -65,7 +81,7 @@ class AsyncRedisSentinelManager(socketio.AsyncRedisManager):
``aioredis.from_url()``.
"""
self._sentinels = [(host, sentinel_port) for host in sentinel_hosts]
self._redis_port=redis_port
self._redis_port = redis_port
self._service = service
self._db = db
self._username = username
@@ -75,7 +91,9 @@ class AsyncRedisSentinelManager(socketio.AsyncRedisManager):
# connect and call grandparent constructor
self._redis_connect()
super(socketio.AsyncRedisManager, self).__init__(channel=channel, write_only=write_only, logger=logger)
super(socketio.AsyncRedisManager, self).__init__(
channel=channel, write_only=write_only, logger=logger
)
def _redis_connect(self):
"""Establish connections to Redis through Sentinel."""
@@ -84,7 +102,7 @@ class AsyncRedisSentinelManager(socketio.AsyncRedisManager):
port=self._redis_port,
db=self._db,
password=self._password,
**self.redis_options
**self.redis_options,
)
self.redis = sentinel.master_for(self._service)