File tree Expand file tree Collapse file tree 3 files changed +34
-12
lines changed Expand file tree Collapse file tree 3 files changed +34
-12
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,8 @@ Unreleased
3
3
4
4
* Updated msgpack requirement to `~=1.0`.
5
5
6
+ * Ensured channel names are unique using UUIDs.
7
+
6
8
7
9
2.4.2 (2020-02-19)
8
10
------------------
Original file line number Diff line number Diff line change 5
5
import hashlib
6
6
import itertools
7
7
import logging
8
- import random
9
- import string
10
8
import sys
11
9
import time
12
10
import types
11
+ import uuid
13
12
14
13
import aioredis
15
14
import msgpack
@@ -216,10 +215,7 @@ def __init__(
216
215
self ._receive_index_generator = itertools .cycle (range (len (self .hosts )))
217
216
self ._send_index_generator = itertools .cycle (range (len (self .hosts )))
218
217
# Decide on a unique client prefix to use in ! sections
219
- # TODO: ensure uniqueness better, e.g. Redis keys with SETNX
220
- self .client_prefix = "" .join (
221
- random .choice (string .ascii_letters ) for i in range (8 )
222
- )
218
+ self .client_prefix = uuid .uuid4 ().hex
223
219
# Set up any encryption objects
224
220
self ._setup_encryption (symmetric_encryption_keys )
225
221
# Number of coroutines trying to receive right now
@@ -533,12 +529,7 @@ async def new_channel(self, prefix="specific"):
533
529
Returns a new channel name that can be used by something in our
534
530
process as a specific channel.
535
531
"""
536
- # TODO: Guarantee uniqueness better?
537
- return "%s.%s!%s" % (
538
- prefix ,
539
- self .client_prefix ,
540
- "" .join (random .choice (string .ascii_letters ) for i in range (12 )),
541
- )
532
+ return "%s.%s!%s" % (prefix , self .client_prefix , uuid .uuid4 ().hex ,)
542
533
543
534
### Flush extension ###
544
535
Original file line number Diff line number Diff line change 1
1
import asyncio
2
+ import random
2
3
3
4
import async_timeout
4
5
import pytest
@@ -416,3 +417,31 @@ async def test_receive_cancel(channel_layer):
416
417
await asyncio .wait_for (task , None )
417
418
except asyncio .CancelledError :
418
419
pass
420
+
421
+
422
+ @pytest .mark .asyncio
423
+ async def test_random_reset__channel_name (channel_layer ):
424
+ """
425
+ Makes sure resetting random seed does not make us reuse channel names.
426
+ """
427
+
428
+ channel_layer = RedisChannelLayer ()
429
+ random .seed (1 )
430
+ channel_name_1 = await channel_layer .new_channel ()
431
+ random .seed (1 )
432
+ channel_name_2 = await channel_layer .new_channel ()
433
+
434
+ assert channel_name_1 != channel_name_2
435
+
436
+
437
+ @pytest .mark .asyncio
438
+ async def test_random_reset__client_prefix (channel_layer ):
439
+ """
440
+ Makes sure resetting random seed does not make us reuse client_prefixes.
441
+ """
442
+
443
+ random .seed (1 )
444
+ channel_layer_1 = RedisChannelLayer ()
445
+ random .seed (1 )
446
+ channel_layer_2 = RedisChannelLayer ()
447
+ assert channel_layer_1 .client_prefix != channel_layer_2 .client_prefix
You can’t perform that action at this time.
0 commit comments