Skip to content

Commit

Permalink
ota_cache: integrate to use multidict
Browse files Browse the repository at this point in the history
  • Loading branch information
Bodong-Yang committed Dec 2, 2024
1 parent 91fce8e commit dc27d78
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/ota_proxy/ota_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from urllib.parse import SplitResult, quote, urlsplit

import aiohttp
from multidict import CIMultiDictProxy
from multidict import CIMultiDict, CIMultiDictProxy

from otaclient_common.common import get_backoff
from otaclient_common.typing import StrOrPath
Expand Down Expand Up @@ -409,7 +409,7 @@ async def _do_request() -> AsyncIterator[bytes]:

async def _retrieve_file_by_cache(
self, cache_identifier: str, *, retry_cache: bool
) -> Optional[Tuple[AsyncIterator[bytes], Mapping[str, str]]]:
) -> tuple[AsyncIterator[bytes], CIMultiDict[str]] | None:
"""
Returns:
A tuple of bytes iterator and headers dict for back to client.
Expand Down Expand Up @@ -458,7 +458,8 @@ async def _retrieve_file_by_cache(

async def _retrieve_file_by_external_cache(
self, client_cache_policy: OTAFileCacheControl
) -> tuple[AsyncIterator[bytes], Mapping[str, str]] | None:
) -> tuple[AsyncIterator[bytes], CIMultiDict[str]] | None:
# skip if not external cache or otaclient doesn't sent valid file_sha256
if not self._external_cache_data_dir or not client_cache_policy.file_sha256:
return

Expand All @@ -469,26 +470,31 @@ async def _retrieve_file_by_external_cache(
)

if cache_file_zst.is_file():
return read_file(cache_file_zst, executor=self._executor), {
HEADER_OTA_FILE_CACHE_CONTROL: OTAFileCacheControl.export_kwargs_as_header(
_header = CIMultiDict()
_header[HEADER_OTA_FILE_CACHE_CONTROL] = (
OTAFileCacheControl.export_kwargs_as_header(
file_sha256=cache_identifier,
file_compression_alg=cfg.EXTERNAL_CACHE_STORAGE_COMPRESS_ALG,
)
}
elif cache_file.is_file():
return read_file(cache_file, executor=self._executor), {
HEADER_OTA_FILE_CACHE_CONTROL: OTAFileCacheControl.export_kwargs_as_header(
)
return read_file(cache_file_zst, executor=self._executor), _header

if cache_file.is_file():
_header = CIMultiDict()
_header[HEADER_OTA_FILE_CACHE_CONTROL] = (
OTAFileCacheControl.export_kwargs_as_header(
file_sha256=cache_identifier
)
}
)
return read_file(cache_file, executor=self._executor), _header

# exposed API

async def retrieve_file(
self,
raw_url: str,
headers_from_client: Dict[str, str],
) -> Optional[Tuple[AsyncIterator[bytes], Mapping[str, str]]]:
) -> tuple[AsyncIterator[bytes], CIMultiDict[str] | CIMultiDictProxy[str]] | None:
"""Retrieve a file descriptor for the requested <raw_url>.
This method retrieves a file descriptor for incoming client request.
Expand Down

0 comments on commit dc27d78

Please sign in to comment.