Merge pull request #4543 from ostcar/redis_connection_pool

Use redis connection pool
This commit is contained in:
Emanuel Schütze 2019-03-29 08:40:28 +01:00 committed by GitHub
commit 13364c7475
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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()