Skip to content
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
2 changes: 1 addition & 1 deletion archinstall/lib/mirror/mirror_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def get_status_by_region(self, region: str, speed_sort: bool) -> list[MirrorStat
# Only sort if we have remote mirror data with score/speed info
# Local mirrors lack this data and can be modified manually before-hand
# Or reflector potentially ran already
if self._fetched_remote and speed_sort:
if self._fetched_remote and speed_sort and region_list:
info('Sorting your selected mirror list based on the speed between you and the individual mirrors (this might take a while)')
# Sort by speed descending (higher is better in bitrate form core.db download)
return sorted(region_list, key=lambda mirror: -mirror.speed)
Expand Down
48 changes: 14 additions & 34 deletions archinstall/lib/models/mirrors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import datetime
import http.client
import datetime as dt
import urllib.error
import urllib.parse
import urllib.request
Expand Down Expand Up @@ -30,7 +29,7 @@ class MirrorStatusEntryV3(BaseModel):
ipv6: bool
details: str
delay: int | None = None
last_sync: datetime.datetime | None = None
last_sync: dt.datetime | None = None
duration_avg: float | None = None
duration_stddev: float | None = None
completion_pct: float | None = None
Expand All @@ -48,38 +47,19 @@ def server_url(self) -> str:
@property
def speed(self) -> float:
if self._speed is None:
if not self._speedtest_retries:
self._speedtest_retries = 3
elif self._speedtest_retries < 1:
self._speedtest_retries = 1

retry = 0
while retry < self._speedtest_retries and self._speed is None:
debug(f'Checking download speed of {self._hostname}[{self.score}] by fetching: {self.url}core/os/x86_64/core.db')
req = urllib.request.Request(url=f'{self.url}core/os/x86_64/core.db')

try:
with urllib.request.urlopen(req, None, 5) as handle, DownloadTimer(timeout=5) as timer:
debug(f'Checking download speed of {self._hostname}[{self.score}] by fetching: {self.url}core/os/x86_64/core.db')
req = urllib.request.Request(url=f'{self.url}core/os/x86_64/core.db')

try:
with DownloadTimer(timeout=3) as timer:
with urllib.request.urlopen(req, None, 3) as handle:
size = len(handle.read())

assert timer.time is not None
self._speed = size / timer.time
debug(f' speed: {self._speed} ({int(self._speed / 1024 / 1024 * 100) / 100}MiB/s)')
# Do not retry error
except urllib.error.URLError as error:
debug(f' speed: <undetermined> ({error}), skip')
self._speed = 0
# Do retry error
except (http.client.IncompleteRead, ConnectionResetError) as error:
debug(f' speed: <undetermined> ({error}), retry')
# Catch all
except Exception as error:
debug(f' speed: <undetermined> ({error}), skip')
self._speed = 0

retry += 1

if self._speed is None:
assert timer.time is not None
self._speed = size / timer.time if timer.time > 0 else 0
debug(f' speed: {self._speed} ({int(self._speed / 1024 / 1024 * 100) / 100}MiB/s)')
except Exception as error:
debug(f' speed: <undetermined> ({error}), skip')
self._speed = 0

return self._speed
Expand Down Expand Up @@ -120,7 +100,7 @@ def debug_output(self, info: ValidationInfo) -> Self:

class MirrorStatusListV3(BaseModel):
cutoff: int
last_check: datetime.datetime
last_check: dt.datetime
num_checks: int
urls: list[MirrorStatusEntryV3]
version: int
Expand Down
37 changes: 23 additions & 14 deletions archinstall/lib/networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,37 @@ def raise_timeout(self, _signl: int, _frame: FrameType | None) -> None:

def __enter__(self) -> Self:
if self.timeout > 0:
self.previous_handler = signal.signal(signal.SIGALRM, self.raise_timeout) # type: ignore[assignment]
self.previous_timer = signal.alarm(self.timeout)
try:
self.previous_handler = signal.signal(signal.SIGALRM, self.raise_timeout) # type: ignore[assignment]
self.previous_timer = signal.alarm(self.timeout)
except ValueError, AttributeError:
pass

self.start_time = time.monotonic()
return self

def __exit__(self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None) -> None:
if self.start_time:
time_delta = time.monotonic() - self.start_time
signal.alarm(0)
try:
signal.alarm(0)
except AttributeError, ValueError:
pass
self.time = time_delta
if self.timeout > 0:
signal.signal(signal.SIGALRM, self.previous_handler)

previous_timer = self.previous_timer
if previous_timer and previous_timer > 0:
remaining_time = int(previous_timer - time_delta)
# The alarm should have been raised during the download.
if remaining_time <= 0:
signal.raise_signal(signal.SIGALRM)
else:
signal.alarm(remaining_time)
if self.timeout > 0 and self.previous_handler is not None:
try:
signal.signal(signal.SIGALRM, self.previous_handler)

previous_timer = self.previous_timer
if previous_timer and previous_timer > 0:
remaining_time = int(previous_timer - time_delta)
# The alarm should have been raised during the download.
if remaining_time <= 0:
signal.raise_signal(signal.SIGALRM)
else:
signal.alarm(remaining_time)
except AttributeError, ValueError:
pass
self.start_time = None


Expand Down
99 changes: 99 additions & 0 deletions tests/test_mirrorlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,102 @@ def test_mirrorlist_multiple_countries(mirrorlist_multiple_countries_fixture: Pa
assert regions[1].urls == [
'https://au.mirror.pkgbuild.com/$repo/os/$arch',
]


def test_get_status_by_region_speed_sort(mirrorlist_with_country_fixture: Path) -> None:
from unittest.mock import MagicMock

from archinstall.lib.models.mirrors import MirrorStatusEntryV3

handler = MirrorListHandler(local_mirrorlist=mirrorlist_with_country_fixture)
handler._fetched_remote = True

m1 = MagicMock(spec=MirrorStatusEntryV3)
m1.speed = 10.0
m2 = MagicMock(spec=MirrorStatusEntryV3)
m2.speed = 50.0
m3 = MagicMock(spec=MirrorStatusEntryV3)
m3.speed = 5.0

handler._status_mappings = {
'France': [m1, m2, m3],
}

result = handler.get_status_by_region('France', speed_sort=True)
assert result == [m2, m1, m3]

result_unsorted = handler.get_status_by_region('France', speed_sort=False)
assert result_unsorted == [m1, m2, m3]

handler._fetched_remote = False
result_local = handler.get_status_by_region('France', speed_sort=True)
assert result_local == [m1, m2, m3]


def test_mirror_speed_handling() -> None:
from unittest.mock import MagicMock, patch
from urllib.error import URLError

from archinstall.lib.exceptions import DownloadTimeoutError
from archinstall.lib.models.mirrors import MirrorStatusEntryV3

entry = MirrorStatusEntryV3(
url='https://mirror.example.com/archlinux/',
protocol='https',
active=True,
country='France',
country_code='FR',
isos=True,
ipv4=True,
ipv6=True,
details='https://archlinux.org/mirrors/example/1/',
)

# 1. Success case: 1000 bytes in 0.5s -> 2000.0 B/s
mock_response = MagicMock()
mock_response.read.return_value = b'x' * 1000
mock_response.__enter__.return_value = mock_response
mock_response.__exit__.return_value = None

with patch('urllib.request.urlopen', return_value=mock_response), patch('archinstall.lib.models.mirrors.DownloadTimer') as mock_timer_cls:
timer_instance = MagicMock()
timer_instance.time = 0.5
timer_instance.__enter__.return_value = timer_instance
timer_instance.__exit__.return_value = None
mock_timer_cls.return_value = timer_instance

entry._speed = None
assert entry.speed == 2000.0

# 2. Zero-division guard: timer.time == 0 -> speed = 0.0
with patch('urllib.request.urlopen', return_value=mock_response), patch('archinstall.lib.models.mirrors.DownloadTimer') as mock_timer_cls:
timer_instance = MagicMock()
timer_instance.time = 0.0
timer_instance.__enter__.return_value = timer_instance
timer_instance.__exit__.return_value = None
mock_timer_cls.return_value = timer_instance

entry._speed = None
assert entry.speed == 0.0

# 3. Timeout error: DownloadTimeoutError -> speed = 0.0
with patch('urllib.request.urlopen', side_effect=DownloadTimeoutError('timed out')):
entry._speed = None
assert entry.speed == 0.0

# 4. Connection failure: URLError -> speed = 0.0
with patch('urllib.request.urlopen', side_effect=URLError('unreachable')):
entry._speed = None
assert entry.speed == 0.0


def test_download_timer() -> None:
import time

from archinstall.lib.networking import DownloadTimer

with DownloadTimer(timeout=2) as timer:
time.sleep(0.01)

assert timer.time is not None
assert timer.time > 0