Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add host_list parameter to Torstomp. #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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