@@ -13,7 +13,9 @@ class RedisPubSubChannelLayer:
13
13
Channel Layer that uses Redis's pub/sub functionality.
14
14
"""
15
15
16
- def __init__ (self , hosts = None , prefix = "asgi" , ** kwargs ):
16
+ def __init__ (
17
+ self , hosts = None , prefix = "asgi" , on_disconnect = None , on_reconnect = None , ** kwargs
18
+ ):
17
19
if hosts is None :
18
20
hosts = [("localhost" , 6379 )]
19
21
assert (
@@ -22,6 +24,9 @@ def __init__(self, hosts=None, prefix="asgi", **kwargs):
22
24
23
25
self .prefix = prefix
24
26
27
+ self .on_disconnect = on_disconnect
28
+ self .on_reconnect = on_reconnect
29
+
25
30
# Each consumer gets its own *specific* channel, created with the `new_channel()` method.
26
31
# This dict maps `channel_name` to a queue of messages for that channel.
27
32
self .channels = {}
@@ -278,6 +283,7 @@ async def _get_sub_conn(self):
278
283
if self ._sub_conn is not None and self ._sub_conn .closed :
279
284
self ._put_redis_conn (self ._sub_conn )
280
285
self ._sub_conn = None
286
+ self ._notify_consumers (self .channel_layer .on_disconnect )
281
287
if self ._sub_conn is None :
282
288
if self ._receive_task is not None :
283
289
self ._receive_task .cancel ()
@@ -309,6 +315,7 @@ async def _get_sub_conn(self):
309
315
self ._receiver .channel (name ) for name in self ._subscribed_to
310
316
]
311
317
await self ._sub_conn .subscribe (* resubscribe_to )
318
+ self ._notify_consumers (self .channel_layer .on_reconnect )
312
319
return self ._sub_conn
313
320
314
321
async def _do_receiving (self ):
@@ -325,6 +332,11 @@ async def _do_receiving(self):
325
332
if channel_name in self .channel_layer .channels :
326
333
self .channel_layer .channels [channel_name ].put_nowait (message )
327
334
335
+ def _notify_consumers (self , mtype ):
336
+ if mtype is not None :
337
+ for channel in self .channel_layer .channels .values ():
338
+ channel .put_nowait (msgpack .packb ({"type" : mtype }))
339
+
328
340
async def _ensure_redis (self ):
329
341
if self ._redis is None :
330
342
if self .master_name is None :
0 commit comments