Fix 1716 - Retry on mirror list fetching (#2736)
* Fix 1716 - retry on http calls * Update * Update
This commit is contained in:
parent
69294e1f9f
commit
8ec1715eb8
|
|
@ -1,3 +1,4 @@
|
||||||
|
import time
|
||||||
import json
|
import json
|
||||||
import pathlib
|
import pathlib
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
|
|
@ -6,7 +7,7 @@ from typing import Dict, Any, List, Optional, TYPE_CHECKING
|
||||||
|
|
||||||
from .menu import AbstractSubMenu, Selector, MenuSelectionType, Menu, ListManager, TextInput
|
from .menu import AbstractSubMenu, Selector, MenuSelectionType, Menu, ListManager, TextInput
|
||||||
from .networking import fetch_data_from_url
|
from .networking import fetch_data_from_url
|
||||||
from .output import warn, FormattedOutput
|
from .output import FormattedOutput, debug
|
||||||
from .storage import storage
|
from .storage import storage
|
||||||
from .models.mirrors import MirrorStatusListV3, MirrorStatusEntryV3
|
from .models.mirrors import MirrorStatusListV3, MirrorStatusEntryV3
|
||||||
|
|
||||||
|
|
@ -324,17 +325,22 @@ def _parse_mirror_list(mirrorlist: str) -> Dict[str, List[MirrorStatusEntryV3]]:
|
||||||
|
|
||||||
|
|
||||||
def list_mirrors() -> Dict[str, List[MirrorStatusEntryV3]]:
|
def list_mirrors() -> Dict[str, List[MirrorStatusEntryV3]]:
|
||||||
regions: Dict[str, List[MirrorStatusEntryV3]] = {}
|
if not storage['arguments']['offline']:
|
||||||
|
|
||||||
if storage['arguments']['offline']:
|
|
||||||
with pathlib.Path('/etc/pacman.d/mirrorlist').open('r') as fp:
|
|
||||||
mirrorlist = fp.read()
|
|
||||||
else:
|
|
||||||
url = "https://archlinux.org/mirrors/status/json/"
|
url = "https://archlinux.org/mirrors/status/json/"
|
||||||
try:
|
attempts = 3
|
||||||
mirrorlist = fetch_data_from_url(url)
|
|
||||||
except ValueError as err:
|
|
||||||
warn(f'Could not fetch an active mirror-list: {err}')
|
|
||||||
return regions
|
|
||||||
|
|
||||||
return _parse_mirror_list(mirrorlist)
|
for attempt_nr in range(attempts):
|
||||||
|
try:
|
||||||
|
mirrorlist = fetch_data_from_url(url)
|
||||||
|
return _parse_mirror_list(mirrorlist)
|
||||||
|
except Exception as e:
|
||||||
|
debug(f'Error while fetching mirror list: {e}')
|
||||||
|
time.sleep(attempt_nr + 1)
|
||||||
|
|
||||||
|
debug('Unable to fetch mirror list remotely, falling back to local mirror list')
|
||||||
|
|
||||||
|
# we'll use the local mirror list if the offline flag is set
|
||||||
|
# or if fetching the mirror list remotely failed
|
||||||
|
with pathlib.Path('/etc/pacman.d/mirrorlist').open('r') as fp:
|
||||||
|
mirrorlist = fp.read()
|
||||||
|
return _parse_mirror_list(mirrorlist)
|
||||||
|
|
|
||||||
|
|
@ -133,8 +133,10 @@ def fetch_data_from_url(url: str, params: Optional[dict] = None) -> str:
|
||||||
response = urlopen(full_url, context=ssl_context)
|
response = urlopen(full_url, context=ssl_context)
|
||||||
data = response.read().decode('UTF-8')
|
data = response.read().decode('UTF-8')
|
||||||
return data
|
return data
|
||||||
except URLError:
|
except URLError as e:
|
||||||
raise ValueError(f'Unable to fetch data from url: {url}')
|
raise ValueError(f'Unable to fetch data from url: {url}\n{e}')
|
||||||
|
except Exception as e:
|
||||||
|
raise ValueError(f'Unexpected error when parsing response: {e}')
|
||||||
|
|
||||||
|
|
||||||
def calc_checksum(icmp_packet) -> int:
|
def calc_checksum(icmp_packet) -> int:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue