Skip to content
This repository has been archived by the owner on Jun 28, 2023. It is now read-only.

Commit

Permalink
Ensure only one reconnect thread running at a time.
Browse files Browse the repository at this point in the history
  • Loading branch information
tacozMacleo committed May 13, 2020
1 parent c3800f6 commit dca286f
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions wappsto/connection/communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def __init__(self, rpc, data_manager, address, port, path_to_calling_file,
self.sending_thread.setDaemon(True)

self.connected = False
self.reconnect_inprogres = False
self.sending_queue = queue.Queue(maxsize=0)
self.rpc = rpc
self.event_storage = event_storage
Expand Down Expand Up @@ -247,7 +248,7 @@ def remove_id_from_confirm_list(self, _id):
def request_reconnect(self):
"""Reconnect if it is required (No blocking)."""
self.wapp_log.info("Reconnect Requested.")
if not self.connected:
if not self.connected and not self.reconnect_inprogres:
threading.Thread(target=self.reconnect).start()

def reconnect(self, retry_limit=None, send_reconnect=True):
Expand All @@ -256,28 +257,32 @@ def reconnect(self, retry_limit=None, send_reconnect=True):
Reconnection attempts in the instance of a connection being interrupted.
"""
self.wappsto_status.set_status(status.RECONNECTING)
self.connected = False
attempt = 0
while not self.connected and (retry_limit is None
or retry_limit > attempt):
attempt += 1
self.wapp_log.info("Trying to reconnect in 5 seconds")
time.sleep(5)
self.close()
self.set_sockets()
self.connect()

if self.connected is True:
self.wapp_log.info("Reconnected with " + str(attempt) + " attempts")
if send_reconnect:
reconnect = message_data.MessageData(message_data.SEND_RECONNECT)
self.sending_queue.put(reconnect)
else:
msg = ("Unable to connect to the server[IP: {}, Port: {}]"
.format(self.address, self.port)
)
raise wappsto_errors.ServerConnectionException(msg)
self.reconnect_inprogres = True
try:
self.wappsto_status.set_status(status.RECONNECTING)
self.connected = False
attempt = 0
while not self.connected and (retry_limit is None
or retry_limit > attempt):
attempt += 1
self.wapp_log.info("Trying to reconnect in 5 seconds")
time.sleep(5)
self.close()
self.set_sockets()
self.connect()

if self.connected is True:
self.wapp_log.info("Reconnected with " + str(attempt) + " attempts")
if send_reconnect:
reconnect = message_data.MessageData(message_data.SEND_RECONNECT)
self.sending_queue.put(reconnect)
else:
msg = ("Unable to connect to the server[IP: {}, Port: {}]"
.format(self.address, self.port)
)
raise wappsto_errors.ServerConnectionException(msg)
finally:
self.reconnect_inprogres = False

def get_object_without_none_values(self, encoded_object):
"""
Expand Down

0 comments on commit dca286f

Please sign in to comment.