From 77a19753697ffe60f15dafe724d13cc91ba3d710 Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Wed, 27 Mar 2019 22:51:33 +0100 Subject: [PATCH] Use redis connection pool --- openslides/utils/redis.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/openslides/utils/redis.py b/openslides/utils/redis.py index 4c385d4eb..bfe48e09a 100644 --- a/openslides/utils/redis.py +++ b/openslides/utils/redis.py @@ -1,5 +1,7 @@ +import asyncio from typing import Any +from channels_redis.core import ConnectionPool from django.conf import settings @@ -13,6 +15,10 @@ else: use_redis = bool(redis_address) +pool = ConnectionPool({"address": redis_address}) +semaphore = asyncio.Semaphore(100) + + class RedisConnectionContextManager: """ Async context manager for connections @@ -20,19 +26,18 @@ class RedisConnectionContextManager: # TODO: contextlib.asynccontextmanager can be used in python 3.7 - def __init__(self, redis_address: str) -> None: - self.redis_address = redis_address - async def __aenter__(self) -> "aioredis.RedisConnection": - self.conn = await aioredis.create_redis(self.redis_address) + await semaphore.acquire() + self.conn = await pool.pop() return self.conn async def __aexit__(self, exc_type: Any, exc: Any, tb: Any) -> None: - self.conn.close() + pool.push(self.conn) + semaphore.release() def get_connection() -> RedisConnectionContextManager: """ Returns contextmanager for a redis connection. """ - return RedisConnectionContextManager(redis_address) + return RedisConnectionContextManager()