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.
This commit is contained in:
Joshua Harlow 2015-02-08 10:59:01 -08:00
parent d3a36c6750
commit b65eed8135
1 changed files with 20 additions and 3 deletions

View File

@ -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