Skip to content

Commit

Permalink
Add host_list parameter to Torstomp.
Browse files Browse the repository at this point in the history
The host_list parameter is an extension to a single host, port pair. The
host_list is a list of (host, port) tuples. In case of a connection
failure the reconnect attempt will cycle trough the host_list and try
another host. This makes failover configurations possible
  • Loading branch information
v0lk3r committed Mar 31, 2016
1 parent 6c4488e commit 13a0c39
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions torstomp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ class TorStomp(object):

VERSION = '1.1'

def __init__(self, host='localhost', port=61613, connect_headers={},
on_error=None, on_disconnect=None, on_connect=None,
reconnect_max_attempts=-1, reconnect_timeout=1000,
log_name='TorStomp'):
def __init__(self, host='localhost', port=61613, host_list=None,
connect_headers={}, on_error=None, on_disconnect=None,
on_connect=None, reconnect_max_attempts=-1,
reconnect_timeout=1000, log_name='TorStomp'):

self.host = host
self.port = port
# `host_list` is a list of (host, port) tuples. Overrides host, port attributes
# In case of a reconnection, the list is cycled until a reconnect succeeds
self.host_list = host_list
self.logger = logging.getLogger(log_name)

self._connect_headers = connect_headers
Expand All @@ -47,7 +50,11 @@ def connect(self):
self.stream = self._build_io_stream()

try:
yield self.stream.connect((self.host, self.port))
if self.host_list is not None:
_host_port = self.host_list[self._reconnect_attempts % len(self.host_list)]
else:
_host_port = (self.host, self.port)
yield self.stream.connect(_host_port)
self.logger.info('Stomp connection estabilished')
except socket.error as error:
self.logger.error(
Expand Down

0 comments on commit 13a0c39

Please # to comment.