Skip to content

Commit

Permalink
Add the select_best_mirror function to select the best mirror for y…
Browse files Browse the repository at this point in the history
…our location
  • Loading branch information
ysdragon committed Nov 21, 2024
1 parent 79d1042 commit cd34e5b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ RUN apt-get update && \
curl \
ca-certificates \
iproute2 \
iputils-ping \
bc \
xz-utils \
bzip2 \
sudo \
Expand Down
15 changes: 7 additions & 8 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,19 @@ select_best_mirror() {
)

local best_mirror=""
local lowest_ping=9999
local lowest_time=9999

for mirror in "${mirrors[@]}"; do
local ping_result=$(ping -c 1 -W 1 "$mirror" 2>/dev/null | grep "time=" | awk -F'=' '{print $4}' | cut -d' ' -f1)
local time_result=$(curl -o /dev/null -s -w "%{time_total}" "https://$mirror" 2>/dev/null)

if [[ -z "$ping_result" ]]; then
echo "Could not ping $mirror"
if [[ -z "$time_result" ]]; then
continue
fi

local ping_time=$(printf "%.0f" "$ping_result")
if (( ping_time > 0 && ping_time < lowest_ping )); then
lowest_ping=$ping_time
local connect_time=$(printf "%.0f" $(echo "$time_result * 1000" | bc))

if (( connect_time > 0 && connect_time < lowest_time )); then
lowest_time=$connect_time
best_mirror=$mirror
fi
done
Expand Down

0 comments on commit cd34e5b

Please sign in to comment.