Skip to content

Commit

Permalink
registry: Add aiohttp_retry to avoid connection problems
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick José Pereira <[email protected]>
  • Loading branch information
patrickelectric committed Aug 30, 2023
1 parent bb7531f commit f5b39eb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
11 changes: 7 additions & 4 deletions blueos_repository/registry.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Any, Dict, List, Optional

import aiohttp
import aiohttp_retry


class Registry:
Expand All @@ -13,6 +14,8 @@ class Registry:
docker_url: str = "https://hub.docker.com/"
token: Optional[str] = None

retry_options = aiohttp_retry.ExponentialRetry(attempts=5)

async def _get_token(self, repo: str) -> str:
"""[summary]
Gets a token for dockerhub.com
Expand All @@ -32,7 +35,7 @@ async def _get_token(self, repo: str) -> str:
}

auth_url = f"https://auth.docker.io/token?service=registry.docker.io&scope=repository:{repo}:pull"
async with aiohttp.ClientSession() as session:
async with aiohttp_retry.RetryClient(retry_options=self.retry_options) as session:
async with session.get(auth_url + "/token", params=payload) as resp:
if resp.status != 200:
print(f"Error status {resp.status}")
Expand All @@ -43,7 +46,7 @@ async def fetch_remote_tags(self, repository: str) -> List[str]:
"""Fetches the tags available for an image in DockerHub"""
print(f"fetching tags in {repository}")
self.token = await self._get_token(repository)
async with aiohttp.ClientSession() as session:
async with aiohttp_retry.RetryClient(retry_options=self.retry_options) as session:
async with session.get(
f"{self.docker_url}/v2/repositories/{repository}/tags/?page_size=25&page=1&ordering=last_updated"
) as resp:
Expand Down Expand Up @@ -79,7 +82,7 @@ async def fetch_digest(self, digest: str, repository: str) -> str:
"Authorization": f"Bearer {self.token}",
"Accept": "application/vnd.docker.distribution.manifest.v2+json,application/vnd.oci.image.manifest.v1+json,application/vnd.oci.image.index.v1+json",
}
async with aiohttp.ClientSession() as session:
async with aiohttp_retry.RetryClient(retry_options=self.retry_options) as session:
url = f"{self.index_url}/v2/{repository}/manifests/{digest}"
async with session.get(url, headers=header) as resp:
if resp.status != 200:
Expand All @@ -97,7 +100,7 @@ async def fetch_labels(self, repo: str) -> Dict[str, Any]:
}
repository, tag = repo.split(":")
print(f"fetching labels for {repository}:{tag}")
async with aiohttp.ClientSession() as session:
async with aiohttp_retry.RetryClient(retry_options=self.retry_options) as session:
url = f"{self.index_url}/v2/{repository}/manifests/{tag}"
async with session.get(url, headers=header) as resp:
if resp.status != 200:
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ packages = [{include = "blueos_repository"}]

[tool.poetry.dependencies]
aiohttp = "3.7.4"
aiohttp-retry = "2.8.3"
json-five = "1.1.1"
python = "^3.10"
semver = "^2.13.0"
Expand Down

0 comments on commit f5b39eb

Please sign in to comment.