mirror of https://github.com/docker/docker-py.git
Merge 47620b5a32
into db7f8b8bb6
This commit is contained in:
commit
cf1ffe0d6f
|
@ -14,7 +14,18 @@ import urllib3.connection
|
||||||
from .. import constants
|
from .. import constants
|
||||||
from .basehttpadapter import BaseHTTPAdapter
|
from .basehttpadapter import BaseHTTPAdapter
|
||||||
|
|
||||||
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):
|
class SSHSocket(socket.socket):
|
||||||
|
@ -85,7 +96,7 @@ class SSHSocket(socket.socket):
|
||||||
self.connect()
|
self.connect()
|
||||||
self.proc.stdout.channel = self
|
self.proc.stdout.channel = self
|
||||||
|
|
||||||
return self.proc.stdout
|
return DontCloseStreamWrapper(self.proc.stdout)
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
if not self.proc or self.proc.stdin.closed:
|
if not self.proc or self.proc.stdin.closed:
|
||||||
|
@ -159,7 +170,7 @@ class SSHConnectionPool(urllib3.connectionpool.HTTPConnectionPool):
|
||||||
class SSHHTTPAdapter(BaseHTTPAdapter):
|
class SSHHTTPAdapter(BaseHTTPAdapter):
|
||||||
|
|
||||||
__attrs__ = requests.adapters.HTTPAdapter.__attrs__ + [
|
__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,
|
def __init__(self, base_url, timeout=60,
|
||||||
|
@ -176,10 +187,8 @@ class SSHHTTPAdapter(BaseHTTPAdapter):
|
||||||
self.ssh_host = base_url[len('ssh://'):]
|
self.ssh_host = base_url[len('ssh://'):]
|
||||||
|
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
|
self.pool = None
|
||||||
self.max_pool_size = max_pool_size
|
self.max_pool_size = max_pool_size
|
||||||
self.pools = RecentlyUsedContainer(
|
|
||||||
pool_connections, dispose_func=lambda p: p.close()
|
|
||||||
)
|
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
def _create_paramiko_client(self, base_url):
|
def _create_paramiko_client(self, base_url):
|
||||||
|
@ -218,31 +227,16 @@ class SSHHTTPAdapter(BaseHTTPAdapter):
|
||||||
self.ssh_client.connect(**self.ssh_params)
|
self.ssh_client.connect(**self.ssh_params)
|
||||||
|
|
||||||
def get_connection(self, url, proxies=None):
|
def get_connection(self, url, proxies=None):
|
||||||
if not self.ssh_client:
|
if self.pool is None:
|
||||||
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.ssh_client and not self.ssh_client.get_transport():
|
if self.ssh_client and not self.ssh_client.get_transport():
|
||||||
self._connect()
|
self._connect()
|
||||||
|
self.pool = SSHConnectionPool(
|
||||||
pool = SSHConnectionPool(
|
|
||||||
ssh_client=self.ssh_client,
|
ssh_client=self.ssh_client,
|
||||||
timeout=self.timeout,
|
timeout=self.timeout,
|
||||||
maxsize=self.max_pool_size,
|
maxsize=self.max_pool_size,
|
||||||
host=self.ssh_host
|
host=self.ssh_host
|
||||||
)
|
)
|
||||||
self.pools[url] = pool
|
return self.pool
|
||||||
|
|
||||||
return pool
|
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
super().close()
|
super().close()
|
||||||
|
|
Loading…
Reference in New Issue