1
0
mirror of https://github.com/ARM-software/devlib.git synced 2025-01-31 02:00:45 +00:00

ssh: Reduce number of opened channels

SSH servers seem to have a maximum number of opened channels, after
which paramiko will raise an exception:

    Could not open an SSH channel: ChannelException(2, 'Connect failed')

Memoizing the SFTPClient object based on the timeout setting leads to
many opened sessions, since the timeout is typically adjusted, e.g. to
match the size of the file when pulling an ftrace trace.dat file.

Solve that by memoizing the SFTPClient based only on the connection
object, with a maximum number of 1 cached object, and update its timeout
setting inplace.
This commit is contained in:
Douglas Raillard 2021-10-15 15:24:53 +01:00 committed by Marc Bonnici
parent 0e0417c6b3
commit 3f92d92a3f

View File

@ -410,8 +410,11 @@ class SshConnection(SshConnectionBase):
def _get_progress_cb(self): def _get_progress_cb(self):
return self.transfer_mgr.progress_cb if self.transfer_mgr is not None else None return self.transfer_mgr.progress_cb if self.transfer_mgr is not None else None
@functools.lru_cache() # Limit the number of opened channels to a low number, since some servers
def _get_sftp(self, timeout): # will reject more connections request. For OpenSSH, this is controlled by
# the MaxSessions config.
@functools.lru_cache(maxsize=1)
def _cached_get_sftp(self):
try: try:
sftp = self.client.open_sftp() sftp = self.client.open_sftp()
except paramiko.ssh_exception.SSHException as e: except paramiko.ssh_exception.SSHException as e:
@ -419,6 +422,10 @@ class SshConnection(SshConnectionBase):
raise TargetStableError('The SSH server does not support SFTP. Please install and enable appropriate module.') from e raise TargetStableError('The SSH server does not support SFTP. Please install and enable appropriate module.') from e
else: else:
raise raise
return sftp
def _get_sftp(self, timeout):
sftp = self._cached_get_sftp()
sftp.get_channel().settimeout(timeout) sftp.get_channel().settimeout(timeout)
return sftp return sftp