Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

SMARTS_reconnect_envision_when_disconnected (#341) #545

Merged
merged 4 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
34 changes: 13 additions & 21 deletions envision/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ class QueueDone:
def __init__(
self,
endpoint: str = None,
num_retries: int = 5,
wait_between_retries: float = 0.5,
output_dir: str = None,
sim_name: str = None,
Expand Down Expand Up @@ -103,7 +102,6 @@ def __init__(
self._thread = self._connect(
endpoint=f"{endpoint}/simulations/{client_id}/broadcast",
queue=self._state_queue,
num_retries=num_retries,
wait_between_retries=wait_between_retries,
)
self._thread.start()
Expand Down Expand Up @@ -135,14 +133,9 @@ def read_and_send(
path: str,
endpoint: str = "ws://localhost:8081",
timestep_sec: float = 0.1,
num_retries: int = 5,
wait_between_retries: float = 0.5,
):
client = Client(
endpoint=endpoint,
num_retries=num_retries,
wait_between_retries=wait_between_retries,
)
client = Client(endpoint=endpoint, wait_between_retries=wait_between_retries,)
with open(path, "r") as f:
for line in f:
line = line.rstrip("\n")
Expand All @@ -153,11 +146,7 @@ def read_and_send(
logging.info("Finished Envision data replay")

def _connect(
self,
endpoint,
queue,
num_retries: int = 50,
wait_between_retries: float = 0.05,
self, endpoint, queue, wait_between_retries: float = 0.05,
):
threadlocal = threading.local()

Expand Down Expand Up @@ -191,14 +180,15 @@ def on_open(ws):

optionally_serialize_and_write(state, ws)

def run_socket(endpoint, num_retries, wait_between_retries, threadlocal):
def run_socket(endpoint, wait_between_retries, threadlocal):
connection_established = False

tries = 1
while tries <= num_retries and not connection_established:
while True:
ws = websocket.WebSocketApp(
endpoint, on_error=on_error, on_close=on_close, on_open=on_open
)
self._log.info("Connected to Envision")

with warnings.catch_warnings():
# XXX: websocket-client library seems to have leaks on connection
Expand All @@ -211,16 +201,18 @@ def run_socket(endpoint, num_retries, wait_between_retries, threadlocal):
)

if not connection_established:
self._log.info(
f"Attempting to connect to Envision tries={tries}/{num_retries}"
)
self._log.info(f"Attempting to connect to Envision tries={tries}")
else:
# when connection closed, retry again every 5 seconds
wait_between_retries = 5
tries = 0
Copy link
Member

@Adaickalavan Adaickalavan Feb 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I think don't need to reset tries=0 since number of tries is not being checked in the while loop.

  • The wait_between_retries value can simply be inherited from the def _connect() input parameter and need not be set here. The default wait_between_retries value in def _connect() can be set as 1s.

Copy link
Contributor Author

@JingfeiPeng JingfeiPeng Feb 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wait_between_retries is from _connect, the default there is 0.5 seconds, which is pretty fast. This is good for trying to establish the initial connection. But I guess when connection is lost, we don't have to try reconnect that frequently. So I reset it to 5s.


tries += 1
time.sleep(wait_between_retries)
tries += 1
time.sleep(wait_between_retries)

return threading.Thread(
target=run_socket,
args=(endpoint, num_retries, wait_between_retries, threadlocal),
args=(endpoint, wait_between_retries, threadlocal),
daemon=True, # If False, the proc will not terminate until this thread stops
)

Expand Down
4 changes: 2 additions & 2 deletions envision/web/dist/main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion envision/web/dist/main.js.map

Large diffs are not rendered by default.

Loading