summaryrefslogtreecommitdiff
path: root/distbuild
diff options
context:
space:
mode:
authorRichard Ipsum <richard.ipsum@codethink.co.uk>2014-06-11 13:48:26 (GMT)
committerRichard Ipsum <richard.ipsum@codethink.co.uk>2014-06-11 15:33:16 (GMT)
commit33bef5a71770b25fe22053267551a10c95c71f7b (patch)
treeeab3224595092b777ae8087dce59e8013a282e03 /distbuild
parent13edeabd929b094ab2ec7288840e62d31d9feb67 (diff)
downloadmorph-33bef5a71770b25fe22053267551a10c95c71f7b.tar.gz
Add max_retries option
By default there is no limit on the number of reconnection attempts. We make the reconnect_interval a parameter, but the default interval remains 1 second.
Diffstat (limited to 'distbuild')
-rw-r--r--distbuild/connection_machine.py19
1 files changed, 14 insertions, 5 deletions
diff --git a/distbuild/connection_machine.py b/distbuild/connection_machine.py
index 6dec403..cce83a4 100644
--- a/distbuild/connection_machine.py
+++ b/distbuild/connection_machine.py
@@ -63,26 +63,29 @@ class ProxyEventSource(object):
class ConnectionMachine(distbuild.StateMachine):
- def __init__(self, addr, port, machine, extra_args):
+ def __init__(self, addr, port, machine, extra_args,
+ reconnect_interval=1, max_retries=float('inf')):
distbuild.StateMachine.__init__(self, 'connecting')
self._addr = addr
self._port = port
self._machine = machine
self._extra_args = extra_args
self._socket = None
- self.reconnect_interval = 1
+ self._reconnect_interval = reconnect_interval
+ self._numof_retries = 0
+ self._max_retries = max_retries
def setup(self):
self._sock_proxy = ProxyEventSource()
self.mainloop.add_event_source(self._sock_proxy)
self._start_connect()
- self._timer = distbuild.TimerEventSource(self.reconnect_interval)
+ self._timer = distbuild.TimerEventSource(self._reconnect_interval)
self.mainloop.add_event_source(self._timer)
spec = [
# state, source, event_class, new_state, callback
- ('connecting', self._sock_proxy, distbuild.SocketWriteable,
+ ('connecting', self._sock_proxy, distbuild.SocketWriteable,
'connected', self._connect),
('connecting', self, StopConnecting, None, self._stop),
('connected', self, Reconnect, 'connecting', self._reconnect),
@@ -119,7 +122,11 @@ class ConnectionMachine(distbuild.StateMachine):
'Failed to connect to %s:%s: %s' %
(self._addr, self._port, str(e)))
- self.mainloop.queue_event(self, ConnectError())
+ if self._numof_retries < self._max_retries:
+ self.mainloop.queue_event(self, ConnectError())
+ else:
+ self.mainloop.queue_event(self, StopConnecting())
+
return
self._sock_proxy.event_source = None
logging.info('Connected to %s:%s' % (self._addr, self._port))
@@ -129,6 +136,8 @@ class ConnectionMachine(distbuild.StateMachine):
def _reconnect(self, event_source, event):
logging.info('Reconnecting to %s:%s' % (self._addr, self._port))
+ self._numof_retries += 1
+
if self._socket is not None:
self._socket.close()
self._timer.stop()