summaryrefslogtreecommitdiff
path: root/kafka/conn.py
diff options
context:
space:
mode:
authorOmar <omar.ghishan@rd.io>2014-02-26 16:55:28 -0800
committerOmar <omar.ghishan@rd.io>2014-02-26 16:55:28 -0800
commitab89a44ecf9c93b116fcc8516cfc21749df74507 (patch)
tree5e318c4c7541bade1da687d2cf5fd145c594dc2b /kafka/conn.py
parente5fdc1c7b22c8ad2aaa66a486871d0ed65977e3d (diff)
parent51910f981843dfa967d24659cdb46117210c832d (diff)
downloadkafka-python-ab89a44ecf9c93b116fcc8516cfc21749df74507.tar.gz
Merge pull request #122 from mrtheb/multihosts
Support for multiple hosts on KafkaClient boostrap (improves on #70)
Diffstat (limited to 'kafka/conn.py')
-rw-r--r--kafka/conn.py28
1 files changed, 27 insertions, 1 deletions
diff --git a/kafka/conn.py b/kafka/conn.py
index 2b8f1c2..749cc02 100644
--- a/kafka/conn.py
+++ b/kafka/conn.py
@@ -2,6 +2,7 @@ import copy
import logging
import socket
import struct
+from random import shuffle
from threading import local
from kafka.common import ConnectionError
@@ -9,6 +10,31 @@ from kafka.common import ConnectionError
log = logging.getLogger("kafka")
DEFAULT_SOCKET_TIMEOUT_SECONDS = 120
+DEFAULT_KAFKA_PORT = 9092
+
+
+def collect_hosts(hosts, randomize=True):
+ """
+ Collects a comma-separated set of hosts (host:port) and optionnaly
+ randomize the returned list.
+ """
+
+ if isinstance(hosts, str):
+ hosts = hosts.strip().split(',')
+
+ result = []
+ for host_port in hosts:
+
+ res = host_port.split(':')
+ host = res[0]
+ port = int(res[1]) if len(res) > 1 else DEFAULT_KAFKA_PORT
+ result.append((host.strip(), port))
+
+ if randomize:
+ shuffle(result)
+
+ return result
+
class KafkaConnection(local):
"""
@@ -81,7 +107,7 @@ class KafkaConnection(local):
sent = self._sock.sendall(payload)
if sent is not None:
self._raise_connection_error()
- except socket.error, e:
+ except socket.error:
log.exception('Unable to send payload to Kafka')
self._raise_connection_error()