Skip to content

Commit

Permalink
remove some sync_to_async calls
Browse files Browse the repository at this point in the history
[noissue]
  • Loading branch information
ipanova committed May 17, 2023
1 parent 4a0a780 commit 15aca2f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
8 changes: 3 additions & 5 deletions pulp_container/app/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ async def get_tag(self, request):
accepted_media_types = get_accepted_media_types(request.headers)

try:
tag = await sync_to_async(Tag.objects.select_related("tagged_manifest").get)(
tag = await Tag.objects.select_related("tagged_manifest").aget(
pk__in=await sync_to_async(repository_version.get_content)(), name=tag_name
)
except ObjectDoesNotExist:
Expand Down Expand Up @@ -170,7 +170,7 @@ async def dispatch_tag(self, request, tag, response_headers):
"""
try:
artifact = await sync_to_async(tag.tagged_manifest._artifacts.get)()
artifact = await tag.tagged_manifest._artifacts.aget()
except ObjectDoesNotExist:
ca = await sync_to_async(lambda x: x[0])(tag.tagged_manifest.contentartifact_set.all())
return await self._stream_content_artifact(request, web.StreamResponse(), ca)
Expand Down Expand Up @@ -230,9 +230,7 @@ async def get_by_digest(self, request):
if digest == EMPTY_BLOB:
return await Registry._empty_blob()
try:
ca = await sync_to_async(
ContentArtifact.objects.select_related("artifact", "content").get
)(
ca = await ContentArtifact.objects.select_related("artifact", "content").aget(
content__in=await sync_to_async(repository_version.get_content)(),
relative_path=digest,
)
Expand Down
41 changes: 21 additions & 20 deletions pulp_container/app/tasks/sync_stages.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@
}


def _save_artifact_blocking(artifact_attributes):
async def _save_artifact(artifact_attributes):
saved_artifact = Artifact(**artifact_attributes)
try:
saved_artifact.save()
await saved_artifact.asave()
except IntegrityError:
del artifact_attributes["file"]
saved_artifact = Artifact.objects.get(**artifact_attributes)
saved_artifact.touch()
saved_artifact = await Artifact.objects.aget(**artifact_attributes)
await sync_to_async(saved_artifact.touch)()
return saved_artifact


Expand All @@ -82,12 +82,11 @@ def __init__(self, remote, signed_only):
self.manifest_dcs = []
self.signature_dcs = []

def _get_content_data_blocking(self, manifest):
saved_artifact = manifest.contentartifact_set.first().artifact
def _get_content_data_blocking(self, saved_artifact):
raw_data = saved_artifact.file.read()
content_data = json.loads(raw_data)
saved_artifact.file.close()
return saved_artifact, content_data, raw_data
return content_data, raw_data

async def _download_and_save_artifact_data(self, manifest_url):
downloader = self.remote.get_downloader(url=manifest_url)
Expand All @@ -96,7 +95,7 @@ async def _download_and_save_artifact_data(self, manifest_url):
raw_data = content_file.read()
response.artifact_attributes["file"] = response.path

saved_artifact = await sync_to_async(_save_artifact_blocking)(response.artifact_attributes)
saved_artifact = await _save_artifact(response.artifact_attributes)
content_data = json.loads(raw_data)

return saved_artifact, content_data, raw_data, response
Expand All @@ -107,13 +106,14 @@ async def _check_for_existing_manifest(self, download_tag):
digest = response.headers.get("docker-content-digest")

if digest and (
manifest := await sync_to_async(
Manifest.objects.prefetch_related("contentartifact_set").filter(digest=digest).first
)()
manifest := await Manifest.objects.prefetch_related("contentartifact_set")
.filter(digest=digest)
.afirst()
):
saved_artifact, content_data, raw_data = await sync_to_async(
self._get_content_data_blocking
)(manifest)
saved_artifact = await manifest._artifacts.aget()
content_data, raw_data = await sync_to_async(self._get_content_data_blocking)(
saved_artifact
)

else:
(
Expand Down Expand Up @@ -478,12 +478,13 @@ async def create_listed_manifest(self, manifest_data):
)
manifest_url = urljoin(self.remote.url, relative_url)

if manifest := await sync_to_async(
Manifest.objects.prefetch_related("contentartifact_set").filter(digest=digest).first
)():
saved_artifact, content_data, _ = await sync_to_async(self._get_content_data_blocking)(
manifest
)
if (
manifest := await Manifest.objects.prefetch_related("contentartifact_set")
.filter(digest=digest)
.afirst()
):
saved_artifact = await manifest._artifacts.aget()
content_data, _ = await sync_to_async(self._get_content_data_blocking)(saved_artifact)

else:
saved_artifact, content_data, _, response = await self._download_and_save_artifact_data(
Expand Down

0 comments on commit 15aca2f

Please sign in to comment.