Skip to content

Commit

Permalink
Custom socket connect timeout in Session()
Browse files Browse the repository at this point in the history
  • Loading branch information
akhil-jha committed May 4, 2023
1 parent 33c9741 commit aedf3b3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 3 additions & 1 deletion broker/hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def __init__(self, hostname=None, name=None, from_dict=False, **kwargs):
self.port = kwargs.get("port", settings.HOST_SSH_PORT)
self.key_filename = kwargs.get("key_filename", settings.HOST_SSH_KEY_FILENAME)
self._session = None
self.socket_connect_timeout = kwargs.get("socket_connect_timeout", 60)

def __del__(self):
"""Try to close the connection on garbage collection of the host instance"""
Expand All @@ -45,7 +46,7 @@ def session(self):
return self._session

def connect(
self, username=None, password=None, timeout=None, port=22, key_filename=None
self, username=None, password=None, timeout=None, port=22, key_filename=None,
):
username = username or self.username
password = password or self.password
Expand All @@ -63,6 +64,7 @@ def connect(
password=password,
port=_port,
key_filename=key_filename,
socket_connect_timeout=self.socket_connect_timeout
)

def close(self):
Expand Down
3 changes: 2 additions & 1 deletion broker/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def __init__(self, **kwargs):
sock.settimeout(kwargs.get("timeout"))
port = kwargs.get("port", 22)
key_filename = kwargs.get("key_filename")
helpers.simple_retry(sock.connect, [(host, port)])
socket_connect_timeout = kwargs.get("socket_connect_timeout", 60)
helpers.simple_retry(sock.connect, [(host, port)], max_timeout=socket_connect_timeout)
self.session = ssh2_Session()
self.session.handshake(sock)
if key_filename:
Expand Down

0 comments on commit aedf3b3

Please sign in to comment.