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

{ACR} Add connection pooling with ACR registries. #30520

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from enum import Enum
from base64 import b64encode
import requests
from requests import Session
from requests import RequestException
from requests.utils import to_native_string
from msrest.http_logger import log_request, log_response
Expand All @@ -34,6 +35,7 @@


logger = get_logger(__name__)
session = Session()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of a global var, can we move this to parameters of request_data_from_registry()? The new parameter can default to None. We can make this PR scoped to repository listing and revisit other call sites of request_data_from_registry() that can benefit from connection pooling later.



EMPTY_GUID = '00000000-0000-0000-0000-000000000000'
Expand Down Expand Up @@ -593,7 +595,7 @@ def request_data_from_registry(http_method,
if file_payload:
with open(file_payload, 'rb') as data_payload:
logger.debug(add_timestamp("Sending a HTTP {} request to {}".format(http_method, url)))
response = requests.request(
response = session.request(
method=http_method,
url=url,
headers=headers,
Expand All @@ -604,7 +606,7 @@ def request_data_from_registry(http_method,
)
else:
logger.debug(add_timestamp("Sending a HTTP {} request to {}".format(http_method, url)))
response = requests.request(
response = session.request(
method=http_method,
url=url,
headers=headers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
class AcrMockCommandsTests(unittest.TestCase):

@mock.patch('azure.cli.command_modules.acr.repository.get_access_credentials', autospec=True)
@mock.patch('requests.request', autospec=True)
@mock.patch('azure.cli.command_modules.acr._docker_utils.session.request', autospec=True)
def test_repository_list(self, mock_requests_get, mock_get_access_credentials):
cmd = self._setup_cmd()

Expand Down Expand Up @@ -106,7 +106,7 @@ def test_repository_list(self, mock_requests_get, mock_get_access_credentials):
verify=mock.ANY)

@mock.patch('azure.cli.command_modules.acr.repository.get_access_credentials', autospec=True)
@mock.patch('requests.request', autospec=True)
@mock.patch('azure.cli.command_modules.acr._docker_utils.session.request', autospec=True)
def test_repository_list_deleted(self, mock_requests_get, mock_get_access_credentials):
cmd = self._setup_cmd()

Expand Down Expand Up @@ -157,7 +157,7 @@ def test_repository_list_deleted(self, mock_requests_get, mock_get_access_creden
verify=mock.ANY)

@mock.patch('azure.cli.command_modules.acr.repository.get_access_credentials', autospec=True)
@mock.patch('requests.request', autospec=True)
@mock.patch('azure.cli.command_modules.acr._docker_utils.session.request', autospec=True)
def test_repository_show_tags(self, mock_requests_get, mock_get_access_credentials):
cmd = self._setup_cmd()

Expand Down Expand Up @@ -213,7 +213,7 @@ def test_repository_show_tags(self, mock_requests_get, mock_get_access_credentia
verify=mock.ANY)

@mock.patch('azure.cli.command_modules.acr.repository.get_access_credentials', autospec=True)
@mock.patch('requests.request', autospec=True)
@mock.patch('azure.cli.command_modules.acr._docker_utils.session.request', autospec=True)
def test_repository_show_manifests(self, mock_requests_get, mock_get_access_credentials):
cmd = self._setup_cmd()

Expand Down Expand Up @@ -264,7 +264,7 @@ def test_repository_show_manifests(self, mock_requests_get, mock_get_access_cred
verify=mock.ANY)

@mock.patch('azure.cli.command_modules.acr.repository.get_access_credentials', autospec=True)
@mock.patch('requests.request', autospec=True)
@mock.patch('azure.cli.command_modules.acr._docker_utils.session.request', autospec=True)
def test_repository_show(self, mock_requests_get, mock_get_access_credentials):
cmd = self._setup_cmd()

Expand Down Expand Up @@ -319,7 +319,7 @@ def test_repository_show(self, mock_requests_get, mock_get_access_credentials):
verify=mock.ANY)

@mock.patch('azure.cli.command_modules.acr.repository.get_access_credentials', autospec=True)
@mock.patch('requests.request', autospec=True)
@mock.patch('azure.cli.command_modules.acr._docker_utils.session.request', autospec=True)
def test_repository_update(self, mock_requests_get, mock_get_access_credentials):
cmd = self._setup_cmd()

Expand Down Expand Up @@ -384,7 +384,7 @@ def test_repository_update(self, mock_requests_get, mock_get_access_credentials)

@mock.patch('azure.cli.command_modules.acr.repository.get_access_credentials', autospec=True)
@mock.patch('azure.cli.command_modules.acr.repository._get_manifest_digest', autospec=True)
@mock.patch('requests.request', autospec=True)
@mock.patch('azure.cli.command_modules.acr._docker_utils.session.request', autospec=True)
def test_repository_delete(self, mock_requests_delete, mock_get_manifest_digest, mock_get_access_credentials):
cmd = self._setup_cmd()

Expand Down Expand Up @@ -455,7 +455,7 @@ def test_repository_delete(self, mock_requests_delete, mock_get_manifest_digest,
@mock.patch('azure.cli.command_modules.acr.manifest.get_access_credentials', autospec=True)
@mock.patch('azure.cli.command_modules.acr.repository.get_access_credentials', autospec=True)
@mock.patch('azure.cli.command_modules.acr.repository._get_manifest_digest', autospec=True)
@mock.patch('requests.request', autospec=True)
@mock.patch('azure.cli.command_modules.acr._docker_utils.session.request', autospec=True)
def test_manifest_show(self, mock_requests_get, mock_get_manifest_digest, mock_get_access_credentials, mock_get_access_credentials_manifest):
cmd = self._setup_cmd()

Expand Down Expand Up @@ -581,7 +581,7 @@ def test_manifest_list(self, mock_obtain_manifest_from_registry, mock_obtain_dat
@mock.patch('azure.cli.command_modules.acr.manifest.get_access_credentials', autospec=True)
@mock.patch('azure.cli.command_modules.acr.repository.get_access_credentials', autospec=True)
@mock.patch('azure.cli.command_modules.acr.repository._get_manifest_digest', autospec=True)
@mock.patch('requests.request', autospec=True)
@mock.patch('azure.cli.command_modules.acr._docker_utils.session.request', autospec=True)
def test_manifest_list_referrers(self, mock_requests_get, mock_get_manifest_digest, mock_get_access_credentials, mock_get_access_credentials_manifest):
cmd = self._setup_cmd()

Expand Down Expand Up @@ -646,7 +646,7 @@ def test_manifest_list_referrers(self, mock_requests_get, mock_get_manifest_dige
@mock.patch('azure.cli.command_modules.acr.manifest.get_access_credentials', autospec=True)
@mock.patch('azure.cli.command_modules.acr.repository.get_access_credentials', autospec=True)
@mock.patch('azure.cli.command_modules.acr.repository._get_manifest_digest', autospec=True)
@mock.patch('requests.request', autospec=True)
@mock.patch('azure.cli.command_modules.acr._docker_utils.session.request', autospec=True)
def test_manifest_delete(self, mock_requests_delete, mock_get_manifest_digest, mock_get_access_credentials, mock_get_access_credentials_manifest):
cmd = self._setup_cmd()

Expand Down Expand Up @@ -704,7 +704,7 @@ def test_manifest_delete(self, mock_requests_delete, mock_get_manifest_digest, m
@mock.patch('azure.cli.command_modules.acr.manifest.get_access_credentials', autospec=True)
@mock.patch('azure.cli.command_modules.acr.repository.get_access_credentials', autospec=True)
@mock.patch('azure.cli.command_modules.acr.repository._get_manifest_digest', autospec=True)
@mock.patch('requests.request', autospec=True)
@mock.patch('azure.cli.command_modules.acr._docker_utils.session.request', autospec=True)
def test_manifest_metadata_show(self, mock_requests_get, mock_get_manifest_digest, mock_get_access_credentials, mock_get_access_credentials_manifest):
cmd = self._setup_cmd()

Expand Down Expand Up @@ -764,7 +764,7 @@ def test_manifest_metadata_show(self, mock_requests_get, mock_get_manifest_diges

@mock.patch('azure.cli.command_modules.acr.manifest.get_access_credentials', autospec=True)
@mock.patch('azure.cli.command_modules.acr.repository.get_access_credentials', autospec=True)
@mock.patch('requests.request', autospec=True)
@mock.patch('azure.cli.command_modules.acr._docker_utils.session.request', autospec=True)
def test_manifest_metadata_list(self, mock_requests_get, mock_get_access_credentials, mock_get_access_credentials_manifest):
cmd = self._setup_cmd()

Expand Down Expand Up @@ -824,7 +824,7 @@ def test_manifest_metadata_list(self, mock_requests_get, mock_get_access_credent
@mock.patch('azure.cli.command_modules.acr.manifest.get_access_credentials', autospec=True)
@mock.patch('azure.cli.command_modules.acr.repository.get_access_credentials', autospec=True)
@mock.patch('azure.cli.command_modules.acr.manifest._get_soft_delete_retention_days', autospec=True)
@mock.patch('requests.request', autospec=True)
@mock.patch('azure.cli.command_modules.acr._docker_utils.session.request', autospec=True)
def test_manifest_deleted_list(self, mock_requests_get, mock_get_soft_delete_retention_days, mock_get_access_credentials, mock_get_access_credentials_manifest):
cmd = self._setup_cmd()

Expand Down Expand Up @@ -886,7 +886,7 @@ def test_manifest_deleted_list(self, mock_requests_get, mock_get_soft_delete_ret
@mock.patch('azure.cli.command_modules.acr.manifest.get_access_credentials', autospec=True)
@mock.patch('azure.cli.command_modules.acr.repository.get_access_credentials', autospec=True)
@mock.patch('azure.cli.command_modules.acr.manifest._get_soft_delete_retention_days', autospec=True)
@mock.patch('requests.request', autospec=True)
@mock.patch('azure.cli.command_modules.acr._docker_utils.session.request', autospec=True)
def test_manifest_deleted_tags_list(self, mock_requests_get, mock_get_soft_delete_retention_days, mock_get_access_credentials, mock_get_access_credentials_manifest):
cmd = self._setup_cmd()

Expand Down Expand Up @@ -979,7 +979,7 @@ def test_manifest_deleted_tags_list(self, mock_requests_get, mock_get_soft_delet
@mock.patch('azure.cli.command_modules.acr.manifest.get_access_credentials', autospec=True)
@mock.patch('azure.cli.command_modules.acr.repository.get_access_credentials', autospec=True)
@mock.patch('azure.cli.command_modules.acr.manifest._obtain_data_from_registry', autospec=True)
@mock.patch('requests.request', autospec=True)
@mock.patch('azure.cli.command_modules.acr._docker_utils.session.request', autospec=True)
def test_manifest_deleted_restore(self, mock_requests_post, mock_obtain_data_from_registry, mock_get_access_credentials, mock_get_access_credentials_manifest):
cmd = self._setup_cmd()

Expand Down Expand Up @@ -1072,7 +1072,7 @@ def test_manifest_deleted_restore(self, mock_requests_post, mock_obtain_data_fro

@mock.patch('azure.cli.command_modules.acr.repository.get_access_credentials', autospec=True)
@mock.patch('azure.cli.command_modules.acr.repository._get_manifest_digest', autospec=True)
@mock.patch('requests.request', autospec=True)
@mock.patch('azure.cli.command_modules.acr._docker_utils.session.request', autospec=True)
def test_manifest_metadata_update(self, mock_requests_patch, mock_get_manifest_digest, mock_get_access_credentials):
cmd = self._setup_cmd()

Expand Down Expand Up @@ -1263,7 +1263,7 @@ def _validate_access_token_request(self, mock_requests_get, mock_requests_post,
verify=mock.ANY)

@mock.patch('azure.cli.command_modules.acr.helm.get_access_credentials', autospec=True)
@mock.patch('requests.request', autospec=True)
@mock.patch('azure.cli.command_modules.acr._docker_utils.session.request', autospec=True)
def test_helm_list(self, mock_requests_get, mock_get_access_credentials):
cmd = self._setup_cmd()

Expand Down Expand Up @@ -1301,7 +1301,7 @@ def test_helm_list(self, mock_requests_get, mock_get_access_credentials):
verify=mock.ANY)

@mock.patch('azure.cli.command_modules.acr.helm.get_access_credentials', autospec=True)
@mock.patch('requests.request', autospec=True)
@mock.patch('azure.cli.command_modules.acr._docker_utils.session.request', autospec=True)
def test_helm_show(self, mock_requests_get, mock_get_access_credentials):
cmd = self._setup_cmd()

Expand Down Expand Up @@ -1346,7 +1346,7 @@ def test_helm_show(self, mock_requests_get, mock_get_access_credentials):
verify=mock.ANY)

@mock.patch('azure.cli.command_modules.acr.helm.get_access_credentials', autospec=True)
@mock.patch('requests.request', autospec=True)
@mock.patch('azure.cli.command_modules.acr._docker_utils.session.request', autospec=True)
def test_helm_delete(self, mock_requests_get, mock_get_access_credentials):
cmd = self._setup_cmd()

Expand Down Expand Up @@ -1380,7 +1380,7 @@ def test_helm_delete(self, mock_requests_get, mock_get_access_credentials):
verify=mock.ANY)

@mock.patch('azure.cli.command_modules.acr.helm.get_access_credentials', autospec=True)
@mock.patch('requests.request', autospec=True)
@mock.patch('azure.cli.command_modules.acr._docker_utils.session.request', autospec=True)
def test_helm_push(self, mock_requests_get, mock_get_access_credentials):
cmd = self._setup_cmd()

Expand Down
Loading