Use redis connection pool

This commit is contained in:
Oskar Hahn 2019-03-27 22:51:33 +01:00
parent 5d51c798fe
commit 77a1975369

View File

@ -1,5 +1,7 @@
import asyncio
from typing import Any from typing import Any
from channels_redis.core import ConnectionPool
from django.conf import settings from django.conf import settings
@ -13,6 +15,10 @@ else:
use_redis = bool(redis_address) use_redis = bool(redis_address)
pool = ConnectionPool({"address": redis_address})
semaphore = asyncio.Semaphore(100)
class RedisConnectionContextManager: class RedisConnectionContextManager:
""" """
Async context manager for connections Async context manager for connections
@ -20,19 +26,18 @@ class RedisConnectionContextManager:
# TODO: contextlib.asynccontextmanager can be used in python 3.7 # 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": 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 return self.conn
async def __aexit__(self, exc_type: Any, exc: Any, tb: Any) -> None: 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: def get_connection() -> RedisConnectionContextManager:
""" """
Returns contextmanager for a redis connection. Returns contextmanager for a redis connection.
""" """
return RedisConnectionContextManager(redis_address) return RedisConnectionContextManager()