Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse SSH connection #2970

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 18 additions & 24 deletions docker/transport/sshconn.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@
except ImportError:
import urllib3

RecentlyUsedContainer = urllib3._collections.RecentlyUsedContainer

class DontCloseStreamWrapper:
"""Make close() noop for the wrapped object."""

def __init__(self, obj):
self.obj = obj

def __getattr__(self, name):
def wrapper(*args, **kwargs):
if name != "close":
return getattr(self.obj, name)(*args, **kwargs)
return wrapper


class SSHSocket(socket.socket):
Expand Down Expand Up @@ -90,7 +101,7 @@ def makefile(self, mode):
self.connect()
self.proc.stdout.channel = self

return self.proc.stdout
return DontCloseStreamWrapper(self.proc.stdout)

def close(self):
if not self.proc or self.proc.stdin.closed:
Expand Down Expand Up @@ -164,7 +175,7 @@ def _get_conn(self, timeout):
class SSHHTTPAdapter(BaseHTTPAdapter):

__attrs__ = requests.adapters.HTTPAdapter.__attrs__ + [
'pools', 'timeout', 'ssh_client', 'ssh_params', 'max_pool_size'
'pool', 'timeout', 'ssh_client', 'ssh_params', 'max_pool_size'
]

def __init__(self, base_url, timeout=60,
Expand All @@ -181,10 +192,8 @@ def __init__(self, base_url, timeout=60,
self.ssh_host = base_url[len('ssh://'):]

self.timeout = timeout
self.pool = None
self.max_pool_size = max_pool_size
self.pools = RecentlyUsedContainer(
pool_connections, dispose_func=lambda p: p.close()
)
super().__init__()

def _create_paramiko_client(self, base_url):
Expand Down Expand Up @@ -223,31 +232,16 @@ def _connect(self):
self.ssh_client.connect(**self.ssh_params)

def get_connection(self, url, proxies=None):
if not self.ssh_client:
return SSHConnectionPool(
ssh_client=self.ssh_client,
timeout=self.timeout,
maxsize=self.max_pool_size,
host=self.ssh_host
)
with self.pools.lock:
pool = self.pools.get(url)
if pool:
return pool

# Connection is closed try a reconnect
if self.pool is None:
if self.ssh_client and not self.ssh_client.get_transport():
self._connect()

pool = SSHConnectionPool(
self.pool = SSHConnectionPool(
ssh_client=self.ssh_client,
timeout=self.timeout,
maxsize=self.max_pool_size,
host=self.ssh_host
)
self.pools[url] = pool

return pool
return self.pool

def close(self):
super().close()
Expand Down