diff options
author | Omar <omar.ghishan@rd.io> | 2014-02-26 16:55:28 -0800 |
---|---|---|
committer | Omar <omar.ghishan@rd.io> | 2014-02-26 16:55:28 -0800 |
commit | ab89a44ecf9c93b116fcc8516cfc21749df74507 (patch) | |
tree | 5e318c4c7541bade1da687d2cf5fd145c594dc2b /kafka/conn.py | |
parent | e5fdc1c7b22c8ad2aaa66a486871d0ed65977e3d (diff) | |
parent | 51910f981843dfa967d24659cdb46117210c832d (diff) | |
download | kafka-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.py | 28 |
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() |