summaryrefslogtreecommitdiff
path: root/kazoo/hosts.py
diff options
context:
space:
mode:
authorBen Bangert <ben@groovie.org>2012-08-28 15:43:42 -0700
committerBen Bangert <ben@groovie.org>2012-08-28 15:43:42 -0700
commit72c90e919096c852310325f7d16d1d8b78f1e411 (patch)
tree41be87b192de269ddfa7d8edcfbe3f8749468e2b /kazoo/hosts.py
parent0c900ce5508be546d1f8efb1217f78a0b71cdec1 (diff)
downloadkazoo-72c90e919096c852310325f7d16d1d8b78f1e411.tar.gz
Most of the main refactor complete, basic connection and ping handling working with start/stop.
Diffstat (limited to 'kazoo/hosts.py')
-rw-r--r--kazoo/hosts.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/kazoo/hosts.py b/kazoo/hosts.py
new file mode 100644
index 0000000..2c21f89
--- /dev/null
+++ b/kazoo/hosts.py
@@ -0,0 +1,35 @@
+import random
+
+
+class RandomHostIterator:
+ """ An iterator that returns a randomly selected host. A host is
+ guaranteed to not be selected twice unless there is only one
+ host in the collection.
+ """
+
+ def __init__(self, hosts):
+ self.last = 0
+ self.hosts = hosts
+
+ def __iter__(self):
+ hostslist = self.hosts[:]
+ random.shuffle(hostslist)
+ for host in hostslist:
+ yield host
+
+ def __len__(self):
+ return len(self.hosts)
+
+
+def collect_hosts(hosts):
+ """ Collect a set of hosts and an optional chroot from a string.
+ """
+ host_ports, chroot = hosts.partition("/")[::2]
+ chroot = "/" + chroot if chroot else None
+
+ result = []
+ for host_port in host_ports.split(","):
+ host, port = host_port.partition(":")[::2]
+ port = int(port.strip()) if port else 2181
+ result.append((host.strip(), port))
+ return (RandomHostIterator(result), chroot)