Kaydet (Commit) be7d0f01 authored tarafından Joffrey F's avatar Joffrey F

Number of pools in adapter is configurable

Default increased from 10 to 25
Signed-off-by: 's avatarJoffrey F <joffrey@docker.com>
üst 72e7afe1
...@@ -40,7 +40,8 @@ class Client( ...@@ -40,7 +40,8 @@ class Client(
api.VolumeApiMixin): api.VolumeApiMixin):
def __init__(self, base_url=None, version=None, def __init__(self, base_url=None, version=None,
timeout=constants.DEFAULT_TIMEOUT_SECONDS, tls=False, timeout=constants.DEFAULT_TIMEOUT_SECONDS, tls=False,
user_agent=constants.DEFAULT_USER_AGENT): user_agent=constants.DEFAULT_USER_AGENT,
num_pools=constants.DEFAULT_NUM_POOLS):
super(Client, self).__init__() super(Client, self).__init__()
if tls and not base_url: if tls and not base_url:
...@@ -58,7 +59,9 @@ class Client( ...@@ -58,7 +59,9 @@ class Client(
base_url, constants.IS_WINDOWS_PLATFORM, tls=bool(tls) base_url, constants.IS_WINDOWS_PLATFORM, tls=bool(tls)
) )
if base_url.startswith('http+unix://'): if base_url.startswith('http+unix://'):
self._custom_adapter = UnixAdapter(base_url, timeout) self._custom_adapter = UnixAdapter(
base_url, timeout, num_pools=num_pools
)
self.mount('http+docker://', self._custom_adapter) self.mount('http+docker://', self._custom_adapter)
self._unmount('http://', 'https://') self._unmount('http://', 'https://')
self.base_url = 'http+docker://localunixsocket' self.base_url = 'http+docker://localunixsocket'
...@@ -68,7 +71,9 @@ class Client( ...@@ -68,7 +71,9 @@ class Client(
'The npipe:// protocol is only supported on Windows' 'The npipe:// protocol is only supported on Windows'
) )
try: try:
self._custom_adapter = NpipeAdapter(base_url, timeout) self._custom_adapter = NpipeAdapter(
base_url, timeout, num_pools=num_pools
)
except NameError: except NameError:
raise errors.DockerException( raise errors.DockerException(
'Install pypiwin32 package to enable npipe:// support' 'Install pypiwin32 package to enable npipe:// support'
...@@ -80,7 +85,9 @@ class Client( ...@@ -80,7 +85,9 @@ class Client(
if isinstance(tls, TLSConfig): if isinstance(tls, TLSConfig):
tls.configure_client(self) tls.configure_client(self)
elif tls: elif tls:
self._custom_adapter = ssladapter.SSLAdapter() self._custom_adapter = ssladapter.SSLAdapter(
num_pools=num_pools
)
self.mount('https://', self._custom_adapter) self.mount('https://', self._custom_adapter)
self.base_url = base_url self.base_url = base_url
......
...@@ -15,3 +15,4 @@ INSECURE_REGISTRY_DEPRECATION_WARNING = \ ...@@ -15,3 +15,4 @@ INSECURE_REGISTRY_DEPRECATION_WARNING = \
IS_WINDOWS_PLATFORM = (sys.platform == 'win32') IS_WINDOWS_PLATFORM = (sys.platform == 'win32')
DEFAULT_USER_AGENT = "docker-py/{0}".format(version) DEFAULT_USER_AGENT = "docker-py/{0}".format(version)
DEFAULT_NUM_POOLS = 25
import six import six
import requests.adapters import requests.adapters
from .. import constants
from .npipesocket import NpipeSocket from .npipesocket import NpipeSocket
if six.PY3: if six.PY3:
...@@ -33,9 +34,9 @@ class NpipeHTTPConnection(httplib.HTTPConnection, object): ...@@ -33,9 +34,9 @@ class NpipeHTTPConnection(httplib.HTTPConnection, object):
class NpipeHTTPConnectionPool(urllib3.connectionpool.HTTPConnectionPool): class NpipeHTTPConnectionPool(urllib3.connectionpool.HTTPConnectionPool):
def __init__(self, npipe_path, timeout=60): def __init__(self, npipe_path, timeout=60, maxsize=10):
super(NpipeHTTPConnectionPool, self).__init__( super(NpipeHTTPConnectionPool, self).__init__(
'localhost', timeout=timeout 'localhost', timeout=timeout, maxsize=maxsize
) )
self.npipe_path = npipe_path self.npipe_path = npipe_path
self.timeout = timeout self.timeout = timeout
...@@ -47,11 +48,12 @@ class NpipeHTTPConnectionPool(urllib3.connectionpool.HTTPConnectionPool): ...@@ -47,11 +48,12 @@ class NpipeHTTPConnectionPool(urllib3.connectionpool.HTTPConnectionPool):
class NpipeAdapter(requests.adapters.HTTPAdapter): class NpipeAdapter(requests.adapters.HTTPAdapter):
def __init__(self, base_url, timeout=60): def __init__(self, base_url, timeout=60,
num_pools=constants.DEFAULT_NUM_POOLS):
self.npipe_path = base_url.replace('npipe://', '') self.npipe_path = base_url.replace('npipe://', '')
self.timeout = timeout self.timeout = timeout
self.pools = RecentlyUsedContainer( self.pools = RecentlyUsedContainer(
10, dispose_func=lambda p: p.close() num_pools, dispose_func=lambda p: p.close()
) )
super(NpipeAdapter, self).__init__() super(NpipeAdapter, self).__init__()
......
...@@ -2,6 +2,8 @@ import six ...@@ -2,6 +2,8 @@ import six
import requests.adapters import requests.adapters
import socket import socket
from .. import constants
if six.PY3: if six.PY3:
import http.client as httplib import http.client as httplib
else: else:
...@@ -12,6 +14,7 @@ try: ...@@ -12,6 +14,7 @@ try:
except ImportError: except ImportError:
import urllib3 import urllib3
RecentlyUsedContainer = urllib3._collections.RecentlyUsedContainer RecentlyUsedContainer = urllib3._collections.RecentlyUsedContainer
...@@ -32,28 +35,31 @@ class UnixHTTPConnection(httplib.HTTPConnection, object): ...@@ -32,28 +35,31 @@ class UnixHTTPConnection(httplib.HTTPConnection, object):
class UnixHTTPConnectionPool(urllib3.connectionpool.HTTPConnectionPool): class UnixHTTPConnectionPool(urllib3.connectionpool.HTTPConnectionPool):
def __init__(self, base_url, socket_path, timeout=60): def __init__(self, base_url, socket_path, timeout=60, maxsize=10):
super(UnixHTTPConnectionPool, self).__init__( super(UnixHTTPConnectionPool, self).__init__(
'localhost', timeout=timeout 'localhost', timeout=timeout, maxsize=maxsize
) )
self.base_url = base_url self.base_url = base_url
self.socket_path = socket_path self.socket_path = socket_path
self.timeout = timeout self.timeout = timeout
def _new_conn(self): def _new_conn(self):
return UnixHTTPConnection(self.base_url, self.socket_path, return UnixHTTPConnection(
self.timeout) self.base_url, self.socket_path, self.timeout
)
class UnixAdapter(requests.adapters.HTTPAdapter): class UnixAdapter(requests.adapters.HTTPAdapter):
def __init__(self, socket_url, timeout=60): def __init__(self, socket_url, timeout=60,
num_pools=constants.DEFAULT_NUM_POOLS):
socket_path = socket_url.replace('http+unix://', '') socket_path = socket_url.replace('http+unix://', '')
if not socket_path.startswith('/'): if not socket_path.startswith('/'):
socket_path = '/' + socket_path socket_path = '/' + socket_path
self.socket_path = socket_path self.socket_path = socket_path
self.timeout = timeout self.timeout = timeout
self.pools = RecentlyUsedContainer(10, self.pools = RecentlyUsedContainer(
dispose_func=lambda p: p.close()) num_pools, dispose_func=lambda p: p.close()
)
super(UnixAdapter, self).__init__() super(UnixAdapter, self).__init__()
def get_connection(self, url, proxies=None): def get_connection(self, url, proxies=None):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment