Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing extra connection kwargs with host address #337

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,27 @@ Possible options for ``CONFIG`` are listed below.
``hosts``
~~~~~~~~~

The server(s) to connect to, as either URIs, ``(host, port)`` tuples, or dicts conforming to `redis Connection <https://redis-py.readthedocs.io/en/v4.3.3/connections.html#redis.connection.Connection>`_.
The server(s) to connect to.
Can be either:

- URIs with extra connection kwargs like

.. code-block:: python

{
"address": "redis://localhost:6379",
"ssl_cert_reqs": None,
}


- or ``(host, port)`` tuples
- or dicts conforming to `redis Connection <https://redis-py.readthedocs.io/en/v4.3.3/connections.html#redis.connection.Connection>`_.
Defaults to ``redis://localhost:6379``. Pass multiple hosts to enable sharding,
but note that changing the host list will lose some sharded data.

Sentinel connections require dicts conforming to:

.. code-block::
.. code-block:: python

{
"sentinels": [
Expand Down
3 changes: 2 additions & 1 deletion channels_redis/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ def create_pool(self, index):
host = self.hosts[index]

if "address" in host:
return aioredis.ConnectionPool.from_url(host["address"])
address = host.pop("address")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this to work, we shouldn't mutate the original dict. We need to deepcopy(host), and mutate that here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also see #341 which addresses the same mutation issue for the branch below.

It might make sense to just copy.deepcopy() on line 130 to avoid the risk of mutation in the downstream redis library too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be addressed in the pubsub layer (note that pubsub already performs a copy of host dict in RedisSingleShardConnection init):

pool = aioredis.ConnectionPool.from_url(self.host["address"])

return aioredis.ConnectionPool.from_url(address, **host)
elif "master_name" in host:
sentinels = host.pop("sentinels")
master_name = host.pop("master_name")
Expand Down
16 changes: 16 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,3 +652,19 @@ def test_deserialize():

assert isinstance(deserialized, dict)
assert deserialized == {"a": True, "b": None, "c": {"d": []}}


def test_create_pool_from_url():
"""
Test allowing passing additional parameters to the connection via host
configuration.
"""
hosts = [
{
"address": "rediss://localhost:6379/0",
"ssl_cert_reqs": None,
}
]
channel_layer = RedisChannelLayer(hosts=hosts)
pool = channel_layer.create_pool(0)
assert "ssl_cert_reqs" in pool.connection_kwargs