You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
4.2 KiB
108 lines
4.2 KiB
5 years ago
|
import asyncio
|
||
|
import pickle
|
||
|
|
||
2 years ago
|
try: # pragma: no cover
|
||
|
from redis import asyncio as aioredis
|
||
|
from redis.exceptions import RedisError
|
||
|
except ImportError: # pragma: no cover
|
||
|
try:
|
||
|
import aioredis
|
||
|
from aioredis.exceptions import RedisError
|
||
|
except ImportError:
|
||
|
aioredis = None
|
||
|
RedisError = None
|
||
5 years ago
|
|
||
9 months ago
|
from .async_pubsub_manager import AsyncPubSubManager
|
||
5 years ago
|
|
||
|
|
||
|
class AsyncRedisManager(AsyncPubSubManager): # pragma: no cover
|
||
|
"""Redis based client manager for asyncio servers.
|
||
|
|
||
|
This class implements a Redis backend for event sharing across multiple
|
||
3 years ago
|
processes.
|
||
5 years ago
|
|
||
3 years ago
|
To use a Redis backend, initialize the :class:`AsyncServer` instance as
|
||
5 years ago
|
follows::
|
||
|
|
||
3 years ago
|
url = 'redis://hostname:port/0'
|
||
|
server = socketio.AsyncServer(
|
||
|
client_manager=socketio.AsyncRedisManager(url))
|
||
5 years ago
|
|
||
|
:param url: The connection URL for the Redis server. For a default Redis
|
||
|
store running on the same host, use ``redis://``. To use an
|
||
|
SSL connection, use ``rediss://``.
|
||
|
:param channel: The channel name on which the server sends and receives
|
||
|
notifications. Must be the same in all the servers.
|
||
4 years ago
|
:param write_only: If set to ``True``, only initialize to emit events. The
|
||
5 years ago
|
default of ``False`` initializes the class for emitting
|
||
|
and receiving.
|
||
3 years ago
|
:param redis_options: additional keyword arguments to be passed to
|
||
|
``aioredis.from_url()``.
|
||
5 years ago
|
"""
|
||
|
name = 'aioredis'
|
||
|
|
||
|
def __init__(self, url='redis://localhost:6379/0', channel='socketio',
|
||
3 years ago
|
write_only=False, logger=None, redis_options=None):
|
||
5 years ago
|
if aioredis is None:
|
||
|
raise RuntimeError('Redis package is not installed '
|
||
2 years ago
|
'(Run "pip install redis" in your virtualenv).')
|
||
3 years ago
|
if not hasattr(aioredis.Redis, 'from_url'):
|
||
|
raise RuntimeError('Version 2 of aioredis package is required.')
|
||
|
self.redis_url = url
|
||
|
self.redis_options = redis_options or {}
|
||
|
self._redis_connect()
|
||
5 years ago
|
super().__init__(channel=channel, write_only=write_only, logger=logger)
|
||
|
|
||
3 years ago
|
def _redis_connect(self):
|
||
|
self.redis = aioredis.Redis.from_url(self.redis_url,
|
||
|
**self.redis_options)
|
||
|
self.pubsub = self.redis.pubsub(ignore_subscribe_messages=True)
|
||
|
|
||
5 years ago
|
async def _publish(self, data):
|
||
|
retry = True
|
||
|
while True:
|
||
|
try:
|
||
3 years ago
|
if not retry:
|
||
|
self._redis_connect()
|
||
|
return await self.redis.publish(
|
||
|
self.channel, pickle.dumps(data))
|
||
2 years ago
|
except RedisError:
|
||
5 years ago
|
if retry:
|
||
|
self._get_logger().error('Cannot publish to redis... '
|
||
|
'retrying')
|
||
|
retry = False
|
||
|
else:
|
||
|
self._get_logger().error('Cannot publish to redis... '
|
||
|
'giving up')
|
||
|
break
|
||
|
|
||
3 years ago
|
async def _redis_listen_with_retries(self):
|
||
5 years ago
|
retry_sleep = 1
|
||
3 years ago
|
connect = False
|
||
5 years ago
|
while True:
|
||
|
try:
|
||
3 years ago
|
if connect:
|
||
|
self._redis_connect()
|
||
|
await self.pubsub.subscribe(self.channel)
|
||
|
retry_sleep = 1
|
||
|
async for message in self.pubsub.listen():
|
||
|
yield message
|
||
2 years ago
|
except RedisError:
|
||
5 years ago
|
self._get_logger().error('Cannot receive from redis... '
|
||
|
'retrying in '
|
||
|
'{} secs'.format(retry_sleep))
|
||
3 years ago
|
connect = True
|
||
5 years ago
|
await asyncio.sleep(retry_sleep)
|
||
|
retry_sleep *= 2
|
||
|
if retry_sleep > 60:
|
||
|
retry_sleep = 60
|
||
3 years ago
|
|
||
|
async def _listen(self):
|
||
|
channel = self.channel.encode('utf-8')
|
||
|
await self.pubsub.subscribe(self.channel)
|
||
|
async for message in self._redis_listen_with_retries():
|
||
|
if message['channel'] == channel and \
|
||
|
message['type'] == 'message' and 'data' in message:
|
||
|
yield message['data']
|
||
|
await self.pubsub.unsubscribe(self.channel)
|