Skip to content

Commit

Permalink
log: try-connect
Browse files Browse the repository at this point in the history
log: hide try connect fail log. print message only once
  • Loading branch information
yowpark authored Oct 26, 2023
2 parents 049721e + 5110a16 commit 181aef2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
7 changes: 7 additions & 0 deletions client/python/gamium/gamium/internal/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def verbose(self, message):
if hasattr(self._printable, "verbose"):
self._printable.verbose(message)

def dot(self):
print(".", end="", flush=True)

def newline(self):
print("", end="\n", flush=True)


class ConsolePrintable:
def __init__(self):
Expand All @@ -44,3 +50,4 @@ def debug(self, message):

def verbose(self, message):
print(f"VERBOSE: {message}")

16 changes: 11 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 @@ -87,13 +91,15 @@ 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
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 181aef2

Please sign in to comment.