From d6daad4276bd153c24f0d9f67fdfe368bfb4ada7 Mon Sep 17 00:00:00 2001 From: Marcin Muszynski Date: Wed, 2 Nov 2022 22:41:26 +0000 Subject: [PATCH] Allow passing extra connection kwargs with address --- README.rst | 18 ++++++++++++++++-- channels_redis/core.py | 3 ++- tests/test_core.py | 16 ++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 0e98ea1..8fd5bae 100644 --- a/README.rst +++ b/README.rst @@ -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 `_. +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 `_. 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": [ diff --git a/channels_redis/core.py b/channels_redis/core.py index 7c04ecd..30c94e0 100644 --- a/channels_redis/core.py +++ b/channels_redis/core.py @@ -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") + return aioredis.ConnectionPool.from_url(address, **host) elif "master_name" in host: sentinels = host.pop("sentinels") master_name = host.pop("master_name") diff --git a/tests/test_core.py b/tests/test_core.py index 6d2ff2a..db83252 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -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