From c377caf754a0170eb717550b04ccd4f660a71c8d Mon Sep 17 00:00:00 2001 From: PBW99 Date: Thu, 26 Oct 2023 08:46:48 +0900 Subject: [PATCH 1/3] log: hide try connect fail log. print message only once --- client/python/gamium/gamium/internal/logger.py | 10 ++++++++++ .../python/gamium/gamium/tcp_gamium_service.py | 15 ++++++++++----- .../gamium/gamium/websocket_gamium_service.py | 16 ++++++++++------ 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/client/python/gamium/gamium/internal/logger.py b/client/python/gamium/gamium/internal/logger.py index 2f08dc44..9da6b174 100644 --- a/client/python/gamium/gamium/internal/logger.py +++ b/client/python/gamium/gamium/internal/logger.py @@ -1,3 +1,5 @@ +import sys + class Logger: def __init__(self): self._printable = ConsolePrintable() @@ -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): diff --git a/client/python/gamium/gamium/tcp_gamium_service.py b/client/python/gamium/gamium/tcp_gamium_service.py index 3f5e9e56..7bd98d89 100644 --- a/client/python/gamium/gamium/tcp_gamium_service.py +++ b/client/python/gamium/gamium/tcp_gamium_service.py @@ -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 @@ -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 @@ -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 diff --git a/client/python/gamium/gamium/websocket_gamium_service.py b/client/python/gamium/gamium/websocket_gamium_service.py index 4c06997f..70ee0c48 100644 --- a/client/python/gamium/gamium/websocket_gamium_service.py +++ b/client/python/gamium/gamium/websocket_gamium_service.py @@ -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 @@ -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: @@ -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 From 874368cdfc391a3eec7f88fab928ea823fafd150 Mon Sep 17 00:00:00 2001 From: PBW99 Date: Thu, 26 Oct 2023 09:15:51 +0900 Subject: [PATCH 2/3] fix: handle disconnection --- client/python/gamium/gamium/tcp_gamium_service.py | 1 + 1 file changed, 1 insertion(+) diff --git a/client/python/gamium/gamium/tcp_gamium_service.py b/client/python/gamium/gamium/tcp_gamium_service.py index 7bd98d89..f0ad96cd 100644 --- a/client/python/gamium/gamium/tcp_gamium_service.py +++ b/client/python/gamium/gamium/tcp_gamium_service.py @@ -91,6 +91,7 @@ def request(self, packet: PacketTypes[P, R]) -> R: data = self._socket.recv(1024) if len(data) == 0: self.disconnect() + raise Exception("Connection closed") self._recv_queue.pushBuffer(data) if self._recv_queue.has(): break From 5110a16cc85ad895521a9945d63da63a866953aa Mon Sep 17 00:00:00 2001 From: PBW99 Date: Thu, 26 Oct 2023 09:20:43 +0900 Subject: [PATCH 3/3] wip: use print without access to sys --- client/python/gamium/gamium/internal/logger.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/client/python/gamium/gamium/internal/logger.py b/client/python/gamium/gamium/internal/logger.py index 9da6b174..5771c568 100644 --- a/client/python/gamium/gamium/internal/logger.py +++ b/client/python/gamium/gamium/internal/logger.py @@ -1,5 +1,3 @@ -import sys - class Logger: def __init__(self): self._printable = ConsolePrintable() @@ -28,12 +26,10 @@ def verbose(self, message): self._printable.verbose(message) def dot(self): - sys.stdout.write(".") - sys.stdout.flush() + print(".", end="", flush=True) def newline(self): - sys.stdout.write("\n") - sys.stdout.flush() + print("", end="\n", flush=True) class ConsolePrintable: @@ -54,3 +50,4 @@ def debug(self, message): def verbose(self, message): print(f"VERBOSE: {message}") +