Skip to content

Commit

Permalink
log: hide try connect fail log. print message only once
Browse files Browse the repository at this point in the history
  • Loading branch information
yowpark committed Oct 25, 2023
1 parent 049721e commit c377caf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
10 changes: 10 additions & 0 deletions client/python/gamium/gamium/internal/logger.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys

class Logger:
def __init__(self):
self._printable = ConsolePrintable()
Expand Down Expand Up @@ -25,6 +27,14 @@ def verbose(self, message):
if hasattr(self._printable, "verbose"):
self._printable.verbose(message)

def dot(self):
sys.stdout.write(".")
sys.stdout.flush()

def newline(self):
sys.stdout.write("\n")
sys.stdout.flush()


class ConsolePrintable:
def __init__(self):
Expand Down
15 changes: 10 additions & 5 deletions client/python/gamium/gamium/tcp_gamium_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ def is_connected(self) -> bool:
def connect(self, try_count: int = 30) -> HelloResultT:
self._logger.info(f"Connecting to {self._host}:{self._port}")

last_error = None
for i in range(try_count):
try:
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._socket.settimeout(self._timeout_sec)
self._socket.connect((self._host, self._port))
except Exception as e:
self._logger.info(f"Failed to connect to {self._host}:{self._port}. count: ({i + 1}/{try_count}), error: {e}")
self._logger.dot()
last_error = e
time.sleep(1)
continue

Expand All @@ -54,12 +56,14 @@ def connect(self, try_count: int = 30) -> HelloResultT:
return res
except Exception as e:
self._socket.close()
stack_trace = "".join(traceback.format_tb(e.__traceback__))
self._logger.info(f"Failed to say hello to {self._host}:{self._port}. count: ({i + 1}/{try_count}), error: {e}. {stack_trace}")
self._logger.dot()
last_error = e
time.sleep(1)
continue

raise Exception(f"Failed to connect to {self._host}:{self._port}")
self._logger.newline()
stack_trace = "".join(traceback.format_tb(last_error.__traceback__))
raise Exception(f"Failed to connect to {self._host}:{self._port}. error: {last_error}, stack: {stack_trace}")

def disconnect(self):
self._is_connected = False
Expand Down Expand Up @@ -92,8 +96,9 @@ def request(self, packet: PacketTypes[P, R]) -> R:
break
return self.pop_message(req)

except ConnectionResetError:
except ConnectionResetError as e:
self.disconnect()
raise e
except Exception as e:
raise e

Expand Down
16 changes: 10 additions & 6 deletions client/python/gamium/gamium/websocket_gamium_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ def is_connected(self) -> bool:
def connect(self, try_count: int = 30) -> HelloResultT:
self._logger.info(f"Connecting to {self._url}")

last_error = None
for i in range(try_count):
try:
self._socket = connect(self._url, open_timeout=self._timeout_sec, close_timeout=self._timeout_sec)
except Exception as e:
self._logger.info(f"Failed to connect to {self._url}. count: ({i + 1}/{try_count}), error: {e}")
self._logger.dot()
last_error = e
time.sleep(1)
continue

Expand All @@ -54,12 +56,14 @@ def connect(self, try_count: int = 30) -> HelloResultT:
except Exception as e:
if None != self._socket:
self._socket.close()
stack_trace = "".join(traceback.format_tb(e.__traceback__))
self._logger.info(f"Failed to say hello to {self._url}. count: ({i + 1}/{try_count}), error: {e}. {stack_trace}")
self._logger.dot()
last_error = e
time.sleep(1)
continue

raise Exception(f"Failed to connect to {self._url}")

self._logger.newline()
stack_trace = "".join(traceback.format_tb(last_error.__traceback__))
raise Exception(f"Failed to connect to {self._url}. error: {last_error}, stack: {stack_trace}")

def disconnect(self):
if None == self._socket:
Expand Down Expand Up @@ -96,8 +100,8 @@ def request(self, packet: PacketTypes[P, R]) -> R:
return self.pop_message(req)
except ConnectionClosed as e:
self.disconnect()
raise e
except Exception as e:
self._logger.error(f"Failed to receive response: {e}")
raise e

return data
Expand Down

0 comments on commit c377caf

Please sign in to comment.