Skip to content

Commit ec4313b

Browse files
ngie-eignauvipy
authored andcommitted
Allow users to switch URLs while omitting the resource identifier (celery#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>
1 parent ee4e7bd commit ec4313b

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

kombu/connection.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,22 @@ def __init__(self, hostname='localhost', userid=None,
220220

221221
self.declared_entities = set()
222222

223-
def switch(self, url):
224-
"""Switch connection parameters to use a new URL.
223+
def switch(self, conn_str):
224+
"""Switch connection parameters to use a new URL or hostname.
225225
226226
Note:
227227
Does not reconnect!
228+
229+
Arguments:
230+
conn_str (str): either a hostname or URL.
228231
"""
229232
self.close()
230233
self.declared_entities.clear()
231234
self._closed = False
232-
self._init_params(**dict(self._initial_params, **parse_url(url)))
235+
conn_params = (
236+
parse_url(conn_str) if "://" in conn_str else {"hostname": conn_str}
237+
)
238+
self._init_params(**dict(self._initial_params, **conn_params))
233239

234240
def maybe_switch_next(self):
235241
"""Switch to next URL given by the current failover strategy."""

t/unit/test_connection.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,16 @@ def test_maybe_switch_next_no_cycle(self):
299299
assert c.hostname == 'foo'
300300
assert c.transport_cls, ('librabbitmq', 'pyamqp' in 'amqp')
301301

302+
def test_switch_without_uri_identifier(self):
303+
c = Connection('amqp://foo')
304+
assert c.hostname == 'foo'
305+
assert c.transport_cls, ('librabbitmq', 'pyamqp' in 'amqp')
306+
c._closed = True
307+
c.switch('example.com')
308+
assert not c._closed
309+
assert c.hostname == 'example.com'
310+
assert c.transport_cls, ('librabbitmq', 'pyamqp' in 'amqp')
311+
302312
def test_heartbeat_check(self):
303313
c = Connection(transport=Transport)
304314
c.transport.heartbeat_check = Mock()

0 commit comments

Comments
 (0)