From b65eed8135dfcc9c3fb19a9cfe392f6d8189be3e Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Sun, 8 Feb 2015 10:59:01 -0800 Subject: [PATCH] Use multi-host support built-in to kazoo The kazoo client already has the ability to use multiple zookeeper servers as a comma separated list so instead of sending only a single server in; send all the possible servers and alternatives in to the client to let it natively handle the servers to connect to. --- kombu/transport/zookeeper.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/kombu/transport/zookeeper.py b/kombu/transport/zookeeper.py index ab6d72f8..c00f9a2f 100644 --- a/kombu/transport/zookeeper.py +++ b/kombu/transport/zookeeper.py @@ -144,10 +144,27 @@ class Channel(virtual.Channel): def _open(self): conninfo = self.connection.client - port = conninfo.port or DEFAULT_PORT - conn_str = '%s:%s' % (conninfo.hostname, port) self.vhost = os.path.join('/', conninfo.virtual_host[0:-1]) - + hosts = [] + if conninfo.alt: + for host_port in conninfo.alt: + if host_port.startswith('zookeeper://'): + host_port = host_port[len('zookeeper://'):] + if not host_port: + continue + try: + host, port = host_port.split(":", 1) + host_port = (host, int(port)) + except ValueError: + if host_port == conninfo.hostname: + host_port = (host_port, conninfo.port or DEFAULT_PORT) + else: + host_port = (host_port, DEFAULT_PORT) + hosts.append(host_port) + host_port = (conninfo.hostname, conninfo.port or DEFAULT_PORT) + if host_port not in hosts: + hosts.insert(0, host_port) + conn_str = ",".join(["%s:%s" % (host, port) for (host, port) in hosts]) conn = KazooClient(conn_str) conn.start() return conn