Skip to content

Commit dad0b13

Browse files
authored
Avoid redis 5.x deprecation warning when closing connection (#376)
1 parent 6c98134 commit dad0b13

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

channels_redis/core.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@
1515
from channels.exceptions import ChannelFull
1616
from channels.layers import BaseChannelLayer
1717

18-
from .utils import _consistent_hash, _wrap_close, create_pool, decode_hosts
18+
from .utils import (
19+
_close_redis,
20+
_consistent_hash,
21+
_wrap_close,
22+
create_pool,
23+
decode_hosts,
24+
)
1925

2026
logger = logging.getLogger(__name__)
2127

@@ -86,7 +92,7 @@ async def flush(self):
8692
async with self._lock:
8793
for index in list(self._connections):
8894
connection = self._connections.pop(index)
89-
await connection.close(close_connection_pool=True)
95+
await _close_redis(connection)
9096

9197

9298
class RedisChannelLayer(BaseChannelLayer):

channels_redis/pubsub.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
import msgpack
77
from redis import asyncio as aioredis
88

9-
from .utils import _consistent_hash, _wrap_close, create_pool, decode_hosts
9+
from .utils import (
10+
_close_redis,
11+
_consistent_hash,
12+
_wrap_close,
13+
create_pool,
14+
decode_hosts,
15+
)
1016

1117
logger = logging.getLogger(__name__)
1218

@@ -285,7 +291,7 @@ async def flush(self):
285291
# The pool was created just for this client, so make sure it is closed,
286292
# otherwise it will schedule the connection to be closed inside the
287293
# __del__ method, which doesn't have a loop running anymore.
288-
await self._redis.close(close_connection_pool=True)
294+
await _close_redis(self._redis)
289295
self._redis = None
290296
self._pubsub = None
291297
self._subscribed_to = set()

channels_redis/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ def _wrapper(self, *args, **kwargs):
3535
loop.close = types.MethodType(_wrapper, loop)
3636

3737

38+
async def _close_redis(connection):
39+
"""
40+
Handle compatibility with redis-py 4.x and 5.x close methods
41+
"""
42+
try:
43+
await connection.aclose(close_connection_pool=True)
44+
except AttributeError:
45+
await connection.close(close_connection_pool=True)
46+
47+
3848
def decode_hosts(hosts):
3949
"""
4050
Takes the value of the "hosts" argument and returns

tests/test_pubsub.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from asgiref.sync import async_to_sync
1010
from channels_redis.pubsub import RedisPubSubChannelLayer
11+
from channels_redis.utils import _close_redis
1112

1213
TEST_HOSTS = ["redis://localhost:6379"]
1314

@@ -239,10 +240,10 @@ async def test_auto_reconnect(channel_layer):
239240
channel_name3 = await channel_layer.new_channel(prefix="test-gr-chan-3")
240241
await channel_layer.group_add("test-group", channel_name1)
241242
await channel_layer.group_add("test-group", channel_name2)
242-
await channel_layer._shards[0]._redis.close(close_connection_pool=True)
243+
await _close_redis(channel_layer._shards[0]._redis)
243244
await channel_layer.group_add("test-group", channel_name3)
244245
await channel_layer.group_discard("test-group", channel_name2)
245-
await channel_layer._shards[0]._redis.close(close_connection_pool=True)
246+
await _close_redis(channel_layer._shards[0]._redis)
246247
await asyncio.sleep(1)
247248
await channel_layer.group_send("test-group", {"type": "message.1"})
248249
# Make sure we get the message on the two channels that were in

tests/test_pubsub_sentinel.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from asgiref.sync import async_to_sync
88
from channels_redis.pubsub import RedisPubSubChannelLayer
9+
from channels_redis.utils import _close_redis
910

1011
SENTINEL_MASTER = "sentinel"
1112
SENTINEL_KWARGS = {"password": "channels_redis"}
@@ -188,10 +189,10 @@ async def test_auto_reconnect(channel_layer):
188189
channel_name3 = await channel_layer.new_channel(prefix="test-gr-chan-3")
189190
await channel_layer.group_add("test-group", channel_name1)
190191
await channel_layer.group_add("test-group", channel_name2)
191-
await channel_layer._shards[0]._redis.close(close_connection_pool=True)
192+
await _close_redis(channel_layer._shards[0]._redis)
192193
await channel_layer.group_add("test-group", channel_name3)
193194
await channel_layer.group_discard("test-group", channel_name2)
194-
await channel_layer._shards[0]._redis.close(close_connection_pool=True)
195+
await _close_redis(channel_layer._shards[0]._redis)
195196
await asyncio.sleep(1)
196197
await channel_layer.group_send("test-group", {"type": "message.1"})
197198
# Make sure we get the message on the two channels that were in

0 commit comments

Comments
 (0)