mirror of
https://github.com/django/daphne.git
synced 2025-07-10 08:02:16 +03:00
Python 3 / unicode fixes
This commit is contained in:
parent
f3c3a239b9
commit
cac848fc89
|
@ -37,6 +37,8 @@ class RedisChannelBackend(BaseChannelBackend):
|
||||||
Maps the value to a node value between 0 and 4095
|
Maps the value to a node value between 0 and 4095
|
||||||
using MD5, then down to one of the ring nodes.
|
using MD5, then down to one of the ring nodes.
|
||||||
"""
|
"""
|
||||||
|
if isinstance(value, six.text_type):
|
||||||
|
value = value.encode("utf8")
|
||||||
bigval = binascii.crc32(value) & 0xffffffff
|
bigval = binascii.crc32(value) & 0xffffffff
|
||||||
return (bigval // 0x100000) // self.ring_divisor
|
return (bigval // 0x100000) // self.ring_divisor
|
||||||
|
|
||||||
|
@ -77,7 +79,7 @@ class RedisChannelBackend(BaseChannelBackend):
|
||||||
connection = self.connection(None)
|
connection = self.connection(None)
|
||||||
# Write out message into expiring key (avoids big items in list)
|
# Write out message into expiring key (avoids big items in list)
|
||||||
# TODO: Use extended set, drop support for older redis?
|
# TODO: Use extended set, drop support for older redis?
|
||||||
key = self.prefix + uuid.uuid4().get_hex()
|
key = self.prefix + uuid.uuid4().hex
|
||||||
connection.set(
|
connection.set(
|
||||||
key,
|
key,
|
||||||
json.dumps(message),
|
json.dumps(message),
|
||||||
|
@ -113,7 +115,7 @@ class RedisChannelBackend(BaseChannelBackend):
|
||||||
while True:
|
while True:
|
||||||
# Select a random connection to use
|
# Select a random connection to use
|
||||||
# TODO: Would we be better trying to do this truly async?
|
# TODO: Would we be better trying to do this truly async?
|
||||||
index = random.choice(indexes.keys())
|
index = random.choice(list(indexes.keys()))
|
||||||
connection = self.connection(index)
|
connection = self.connection(index)
|
||||||
channels = indexes[index]
|
channels = indexes[index]
|
||||||
# Shuffle channels to avoid the first ones starving others of workers
|
# Shuffle channels to avoid the first ones starving others of workers
|
||||||
|
@ -134,6 +136,7 @@ class RedisChannelBackend(BaseChannelBackend):
|
||||||
seconds (expiry defaults to message expiry if not provided).
|
seconds (expiry defaults to message expiry if not provided).
|
||||||
"""
|
"""
|
||||||
key = "%s:group:%s" % (self.prefix, group)
|
key = "%s:group:%s" % (self.prefix, group)
|
||||||
|
key = key.encode("utf8")
|
||||||
self.connection(self.consistent_hash(group)).zadd(
|
self.connection(self.consistent_hash(group)).zadd(
|
||||||
key,
|
key,
|
||||||
**{channel: time.time() + (expiry or self.expiry)}
|
**{channel: time.time() + (expiry or self.expiry)}
|
||||||
|
@ -145,6 +148,7 @@ class RedisChannelBackend(BaseChannelBackend):
|
||||||
does nothing otherwise (does not error)
|
does nothing otherwise (does not error)
|
||||||
"""
|
"""
|
||||||
key = "%s:group:%s" % (self.prefix, group)
|
key = "%s:group:%s" % (self.prefix, group)
|
||||||
|
key = key.encode("utf8")
|
||||||
self.connection(self.consistent_hash(group)).zrem(
|
self.connection(self.consistent_hash(group)).zrem(
|
||||||
key,
|
key,
|
||||||
channel,
|
channel,
|
||||||
|
@ -155,15 +159,16 @@ class RedisChannelBackend(BaseChannelBackend):
|
||||||
Returns an iterable of all channels in the group.
|
Returns an iterable of all channels in the group.
|
||||||
"""
|
"""
|
||||||
key = "%s:group:%s" % (self.prefix, group)
|
key = "%s:group:%s" % (self.prefix, group)
|
||||||
|
key = key.encode("utf8")
|
||||||
connection = self.connection(self.consistent_hash(group))
|
connection = self.connection(self.consistent_hash(group))
|
||||||
# Discard old channels
|
# Discard old channels
|
||||||
connection.zremrangebyscore(key, 0, int(time.time()) - 10)
|
connection.zremrangebyscore(key, 0, int(time.time()) - 10)
|
||||||
# Return current lot
|
# Return current lot
|
||||||
return connection.zrange(
|
return [x.decode("utf8") for x in connection.zrange(
|
||||||
key,
|
key,
|
||||||
0,
|
0,
|
||||||
-1,
|
-1,
|
||||||
)
|
)]
|
||||||
|
|
||||||
# TODO: send_group efficient implementation using Lua
|
# TODO: send_group efficient implementation using Lua
|
||||||
|
|
||||||
|
|
|
@ -23,8 +23,8 @@ class ConsumerRegistry(object):
|
||||||
module_name, variable_name = routing.rsplit(".", 1)
|
module_name, variable_name = routing.rsplit(".", 1)
|
||||||
try:
|
try:
|
||||||
routing = getattr(importlib.import_module(module_name), variable_name)
|
routing = getattr(importlib.import_module(module_name), variable_name)
|
||||||
except (ImportError, AttributeError):
|
except (ImportError, AttributeError) as e:
|
||||||
raise ImproperlyConfigured("Cannot import channel routing %r" % routing)
|
raise ImproperlyConfigured("Cannot import channel routing %r: %s" % (routing, e))
|
||||||
# Load consumers into us
|
# Load consumers into us
|
||||||
for channel, handler in routing.items():
|
for channel, handler in routing.items():
|
||||||
self.add_consumer(handler, [channel])
|
self.add_consumer(handler, [channel])
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from ..channel import Channel
|
from ..channel import Channel
|
||||||
from ..backends.database import DatabaseChannelBackend
|
from ..backends.database import DatabaseChannelBackend
|
||||||
|
@ -44,16 +46,16 @@ class MemoryBackendTests(TestCase):
|
||||||
Tests that group addition and removal and listing works
|
Tests that group addition and removal and listing works
|
||||||
"""
|
"""
|
||||||
self.backend.group_add("tgroup", "test")
|
self.backend.group_add("tgroup", "test")
|
||||||
self.backend.group_add("tgroup", "test2")
|
self.backend.group_add("tgroup", "test2€")
|
||||||
self.backend.group_add("tgroup2", "test3")
|
self.backend.group_add("tgroup2", "test3")
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
set(self.backend.group_channels("tgroup")),
|
set(self.backend.group_channels("tgroup")),
|
||||||
{"test", "test2"},
|
{"test", "test2€"},
|
||||||
)
|
)
|
||||||
self.backend.group_discard("tgroup", "test2")
|
self.backend.group_discard("tgroup", "test2€")
|
||||||
self.backend.group_discard("tgroup", "test2")
|
self.backend.group_discard("tgroup", "test2€")
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
self.backend.group_channels("tgroup"),
|
list(self.backend.group_channels("tgroup")),
|
||||||
["test"],
|
["test"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -76,7 +78,7 @@ class MemoryBackendTests(TestCase):
|
||||||
self.backend.group_add("tgroup", "test")
|
self.backend.group_add("tgroup", "test")
|
||||||
self.backend.group_add("tgroup", "test2")
|
self.backend.group_add("tgroup", "test2")
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
self.backend.group_channels("tgroup"),
|
list(self.backend.group_channels("tgroup")),
|
||||||
[],
|
[],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user