From ac70ce0975049adbc78ae2b94c27aeaa22b494ff Mon Sep 17 00:00:00 2001 From: Daniil Okhlopkov <5613295+ohld@users.noreply.github.com> Date: Tue, 30 Sep 2025 19:33:13 +0300 Subject: [PATCH] Improve Redis connection handling --- src/config.py | 1 + src/redis.py | 10 ++++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/config.py b/src/config.py index 6e38627b..12d7f100 100644 --- a/src/config.py +++ b/src/config.py @@ -13,6 +13,7 @@ class Config(BaseSettings): DATABASE_POOL_PRE_PING: bool = True REDIS_URL: RedisDsn + REDIS_HEALTH_CHECK_INTERVAL: int = 30 SITE_DOMAIN: str = "myapp.com" diff --git a/src/redis.py b/src/redis.py index c7458760..7d8fe205 100644 --- a/src/redis.py +++ b/src/redis.py @@ -10,6 +10,8 @@ pool = aioredis.ConnectionPool.from_url( str(settings.REDIS_URL), max_connections=settings.REDIS_MAX_CONNECTIONS, + health_check_interval=settings.REDIS_HEALTH_CHECK_INTERVAL, + socket_keepalive=True, decode_responses=True, ) redis_client = aioredis.Redis(connection_pool=pool) @@ -64,10 +66,10 @@ async def add_memes_to_queue_by_key( key: str, memes: list[dict], expire: int = 3600 ) -> None: jsoned_memes = [orjson.dumps(meme) for meme in memes] - p = await redis_client.pipeline(transaction=True) - await p.sadd(key, *jsoned_memes) - await p.expire(key, expire) - await p.execute(raise_on_error=True) + async with redis_client.pipeline(transaction=True) as pipe: + await pipe.sadd(key, *jsoned_memes) + await pipe.expire(key, expire) + await pipe.execute(raise_on_error=True) def get_user_info_key(user_id: int) -> str: