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

Adding timeouts to all remote requests #43

Merged
merged 2 commits into from
May 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions docker_registry_client/AuthorizationService.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ class AuthorizationService(object):
authenticate to the registry. Token has to be renew each time we change
"scope".
"""
def __init__(self, registry, url="", auth=None, verify=False):
def __init__(self, registry, url="", auth=None, verify=False,
api_timeout=None):
# Registry ip:port
self.registry = urlsplit(registry).netloc
# Service url, ip:port
self.url = url
# Authentication (user, password) or None. Used by request to do
# basicauth
self.auth = auth
# Timeout for HTTP request
self.api_timeout = api_timeout

# Desired scope is the scope needed for the next operation on the
# registry
Expand All @@ -53,7 +56,8 @@ def __init__(self, registry, url="", auth=None, verify=False):
def get_new_token(self):
rsp = requests.get("%s/v2/token?service=%s&scope=%s" %
(self.url, self.registry, self.desired_scope),
auth=self.auth, verify=self.verify)
auth=self.auth, verify=self.verify,
timeout=self.api_timeout)
if not rsp.ok:
logger.error("Can't get token for authentication")
self.token = ""
Expand Down
6 changes: 4 additions & 2 deletions docker_registry_client/DockerRegistryClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class DockerRegistryClient(object):
def __init__(self, host, verify_ssl=None, api_version=None, username=None,
password=None, auth_service_url=""):
password=None, auth_service_url="", api_timeout=None):
"""
Constructor

Expand All @@ -18,12 +18,14 @@ def __init__(self, host, verify_ssl=None, api_version=None, username=None,
:param password: password to use for basic authentication
:param auth_service_url: authorization service URL (including scheme,
for v2 only)
:param api_timeout: timeout for external request
"""

self._base_client = BaseClient(host, verify_ssl=verify_ssl,
api_version=api_version,
username=username, password=password,
auth_service_url=auth_service_url)
auth_service_url=auth_service_url,
api_timeout=api_timeout)
self.api_version = self._base_client.version
self._repositories = {}
self._repositories_by_namespace = {}
Expand Down
15 changes: 10 additions & 5 deletions docker_registry_client/_BaseClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@


class CommonBaseClient(object):
def __init__(self, host, verify_ssl=None, username=None, password=None):
def __init__(self, host, verify_ssl=None, username=None, password=None,
api_timeout=None):
self.host = host

self.method_kwargs = {}
if verify_ssl is not None:
self.method_kwargs['verify'] = verify_ssl
if username is not None and password is not None:
self.method_kwargs['auth'] = (username, password)
if api_timeout is not None:
self.method_kwargs['timeout'] = api_timeout

def _http_response(self, url, method, data=None, **kwargs):
"""url -> full target url
Expand Down Expand Up @@ -157,6 +160,7 @@ def __init__(self, *args, **kwargs):
url=auth_service_url,
verify=self.method_kwargs.get('verify', False),
auth=self.method_kwargs.get('auth', None),
api_timeout=self.method_kwargs.get('api_timeout')
)

@property
Expand Down Expand Up @@ -266,22 +270,23 @@ def _http_response(self, url, method, data=None, content_type=None,


def BaseClient(host, verify_ssl=None, api_version=None, username=None,
password=None, auth_service_url=""):
password=None, auth_service_url="", api_timeout=None):
if api_version == 1:
return BaseClientV1(
host, verify_ssl=verify_ssl, username=username, password=password,
api_timeout=api_timeout,
)
elif api_version == 2:
return BaseClientV2(
host, verify_ssl=verify_ssl, username=username, password=password,
auth_service_url=auth_service_url,
auth_service_url=auth_service_url, api_timeout=api_timeout,
)
elif api_version is None:
# Try V2 first
logger.debug("checking for v2 API")
v2_client = BaseClientV2(
host, verify_ssl=verify_ssl, username=username, password=password,
auth_service_url=auth_service_url,
auth_service_url=auth_service_url, api_timeout=api_timeout,
)
try:
v2_client.check_status()
Expand All @@ -290,7 +295,7 @@ def BaseClient(host, verify_ssl=None, api_version=None, username=None,
logger.debug("falling back to v1 API")
return BaseClientV1(
host, verify_ssl=verify_ssl, username=username,
password=password,
password=password, api_timeout=api_timeout,
)

raise
Expand Down