refactor(connector.py): avoid recursion in connect()
This commit is contained in:
@@ -68,9 +68,12 @@ class Connector:
|
|||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def connect(self, retry_interval: int = 1) -> None:
|
def connect(self) -> None:
|
||||||
|
retry_interval: int = 1
|
||||||
|
connected: bool = False
|
||||||
|
while not connected:
|
||||||
self.logger.debug(
|
self.logger.debug(
|
||||||
f"Connecting to {self.host}:{self.port} with timeout {self.connect_timeout}s..."
|
f"[.] Connecting to {self.host}:{self.port} with timeout {self.connect_timeout}s..."
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
self.socket.settimeout(self.connect_timeout)
|
self.socket.settimeout(self.connect_timeout)
|
||||||
@@ -78,6 +81,7 @@ class Connector:
|
|||||||
self.socket.settimeout(self.socket_timeout)
|
self.socket.settimeout(self.socket_timeout)
|
||||||
self.logger.info(f"[+] Connected to {self.host}:{self.port}")
|
self.logger.info(f"[+] Connected to {self.host}:{self.port}")
|
||||||
self.send_hello()
|
self.send_hello()
|
||||||
|
connected = True
|
||||||
except (
|
except (
|
||||||
socket.timeout,
|
socket.timeout,
|
||||||
ConnectionRefusedError,
|
ConnectionRefusedError,
|
||||||
@@ -90,7 +94,9 @@ class Connector:
|
|||||||
f"[.] Retrying connection in {retry_interval} s..."
|
f"[.] Retrying connection in {retry_interval} s..."
|
||||||
)
|
)
|
||||||
time.sleep(retry_interval)
|
time.sleep(retry_interval)
|
||||||
self.connect(retry_interval=min(30, retry_interval * 2))
|
retry_interval = min(
|
||||||
|
retry_interval * 2, 30
|
||||||
|
) # exponential backoff
|
||||||
|
|
||||||
def send(self, data: bytes, no_check_ack: bool = False) -> None:
|
def send(self, data: bytes, no_check_ack: bool = False) -> None:
|
||||||
self.logger.debug(f"[>] Sending data: {data}")
|
self.logger.debug(f"[>] Sending data: {data}")
|
||||||
|
|||||||
Reference in New Issue
Block a user