2019-03-27 22:51:33 +01:00
|
|
|
import asyncio
|
2018-11-01 17:30:18 +01:00
|
|
|
from typing import Any
|
|
|
|
|
2019-03-27 22:51:33 +01:00
|
|
|
from channels_redis.core import ConnectionPool
|
2018-11-01 17:30:18 +01:00
|
|
|
from django.conf import settings
|
|
|
|
|
2019-08-29 14:25:02 +02:00
|
|
|
from . import logging
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2018-11-01 17:30:18 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
import aioredis
|
|
|
|
except ImportError:
|
|
|
|
use_redis = False
|
|
|
|
else:
|
|
|
|
# set use_redis to true, if there is a value for REDIS_ADDRESS in the settings
|
2019-01-06 16:22:33 +01:00
|
|
|
redis_address = getattr(settings, "REDIS_ADDRESS", "")
|
2018-11-01 17:30:18 +01:00
|
|
|
use_redis = bool(redis_address)
|
2019-08-29 14:25:02 +02:00
|
|
|
if use_redis:
|
|
|
|
logger.info(f"Redis address {redis_address}")
|
2018-11-01 17:30:18 +01:00
|
|
|
|
2019-03-27 22:51:33 +01:00
|
|
|
pool = ConnectionPool({"address": redis_address})
|
2019-09-10 15:00:55 +02:00
|
|
|
counter = 0
|
2019-03-27 22:51:33 +01:00
|
|
|
|
|
|
|
|
2018-11-01 17:30:18 +01:00
|
|
|
class RedisConnectionContextManager:
|
|
|
|
"""
|
|
|
|
Async context manager for connections
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2018-11-01 17:30:18 +01:00
|
|
|
# TODO: contextlib.asynccontextmanager can be used in python 3.7
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
async def __aenter__(self) -> "aioredis.RedisConnection":
|
2019-09-10 15:00:55 +02:00
|
|
|
global counter
|
|
|
|
while counter > 100:
|
|
|
|
await asyncio.sleep(0.1)
|
|
|
|
counter += 1
|
|
|
|
|
2019-03-27 22:51:33 +01:00
|
|
|
self.conn = await pool.pop()
|
2018-11-01 17:30:18 +01:00
|
|
|
return self.conn
|
|
|
|
|
|
|
|
async def __aexit__(self, exc_type: Any, exc: Any, tb: Any) -> None:
|
2019-08-29 14:25:02 +02:00
|
|
|
if exc:
|
|
|
|
logger.warn(f"Redis Exception: {exc}. Do not reuse connection...")
|
|
|
|
pool.conn_error(self.conn)
|
|
|
|
else:
|
|
|
|
pool.push(self.conn)
|
|
|
|
self.conn = None
|
|
|
|
|
2019-09-10 15:00:55 +02:00
|
|
|
global counter
|
|
|
|
counter -= 1
|
2018-11-01 17:30:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_connection() -> RedisConnectionContextManager:
|
|
|
|
"""
|
|
|
|
Returns contextmanager for a redis connection.
|
|
|
|
"""
|
2019-03-27 22:51:33 +01:00
|
|
|
return RedisConnectionContextManager()
|