Merge pull request #4543 from ostcar/redis_connection_pool
Use redis connection pool
This commit is contained in:
commit
13364c7475
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user