summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorngie-eign <1574099+ngie-eign@users.noreply.github.com>2019-06-23 22:39:18 -0700
committerAsif Saif Uddin <auvipy@gmail.com>2019-06-24 11:39:18 +0600
commitec4313b0348d7637e51af833d0cf874a0c504c29 (patch)
tree8e191599273102c70be33d37fdd4cb1efe69e7dc
parentee4e7bd29bf47e5b0d1259fcb5d83a55c7f8bf74 (diff)
downloadkombu-ec4313b0348d7637e51af833d0cf874a0c504c29.tar.gz
Allow users to switch URLs while omitting the resource identifier (#1032)
Prior to this change, one needed to specify a URL using a URI identifier, e.g., `pyamqp://foo.bar`. This change makes it so calling `.switch(..)` again results in switching the host, not switching the resource identifier. This simplifies setting up connections with just hostnames specifying the resource identifier once, separately. Signed-off-by: Enji Cooper <yaneurabeya@gmail.com>
-rw-r--r--kombu/connection.py12
-rw-r--r--t/unit/test_connection.py10
2 files changed, 19 insertions, 3 deletions
diff --git a/kombu/connection.py b/kombu/connection.py
index 33090588..7cefb26d 100644
--- a/kombu/connection.py
+++ b/kombu/connection.py
@@ -220,16 +220,22 @@ class Connection(object):
self.declared_entities = set()
- def switch(self, url):
- """Switch connection parameters to use a new URL.
+ def switch(self, conn_str):
+ """Switch connection parameters to use a new URL or hostname.
Note:
Does not reconnect!
+
+ Arguments:
+ conn_str (str): either a hostname or URL.
"""
self.close()
self.declared_entities.clear()
self._closed = False
- self._init_params(**dict(self._initial_params, **parse_url(url)))
+ conn_params = (
+ parse_url(conn_str) if "://" in conn_str else {"hostname": conn_str}
+ )
+ self._init_params(**dict(self._initial_params, **conn_params))
def maybe_switch_next(self):
"""Switch to next URL given by the current failover strategy."""
diff --git a/t/unit/test_connection.py b/t/unit/test_connection.py
index a2489bc3..9161338e 100644
--- a/t/unit/test_connection.py
+++ b/t/unit/test_connection.py
@@ -299,6 +299,16 @@ class test_Connection:
assert c.hostname == 'foo'
assert c.transport_cls, ('librabbitmq', 'pyamqp' in 'amqp')
+ def test_switch_without_uri_identifier(self):
+ c = Connection('amqp://foo')
+ assert c.hostname == 'foo'
+ assert c.transport_cls, ('librabbitmq', 'pyamqp' in 'amqp')
+ c._closed = True
+ c.switch('example.com')
+ assert not c._closed
+ assert c.hostname == 'example.com'
+ assert c.transport_cls, ('librabbitmq', 'pyamqp' in 'amqp')
+
def test_heartbeat_check(self):
c = Connection(transport=Transport)
c.transport.heartbeat_check = Mock()