summaryrefslogtreecommitdiff
path: root/setup-apkrepos.in
diff options
context:
space:
mode:
authorNatanael Copa <ncopa@alpinelinux.org>2013-06-11 10:45:27 +0000
committerNatanael Copa <ncopa@alpinelinux.org>2013-06-11 11:02:19 +0000
commit1ff7c0494c19623166632cd163d7aeb7c527f3d7 (patch)
tree29abc2e74286e497870674a8aa44224b3468f6dc /setup-apkrepos.in
parentf65ddba2cf01de088877fa635610c3c94aa6c9c9 (diff)
downloadalpine-conf-1ff7c0494c19623166632cd163d7aeb7c527f3d7.zip
setup-apkrepos: refactor mirror timing
- use apk update to test the download speed - print the times - use /proc/uptime instead of 'time'
Diffstat (limited to 'setup-apkrepos.in')
-rwxr-xr-xsetup-apkrepos.in63
1 files changed, 32 insertions, 31 deletions
diff --git a/setup-apkrepos.in b/setup-apkrepos.in
index 5bf5217..b99055b 100755
--- a/setup-apkrepos.in
+++ b/setup-apkrepos.in
@@ -33,38 +33,39 @@ add_random_mirror() {
add_mirror $mirror
}
+time_cmd() {
+ local start=$(cut -d ' ' -f1 /proc/uptime)
+ $@ >&2
+ awk -v start=$start -v end=$(cut -d ' ' -f1 /proc/uptime) \
+ 'BEGIN {print end - start; exit}'
+}
+
+find_fastest_mirror() {
+ export http_proxy=
+ local url=
+ for url in $MIRRORS; do
+ echo $(time_cmd apk update --quiet \
+ --repository $url/edge/main \
+ --repositories-file /dev/null) $url
+ done | awk ' {
+ if (!current) {
+ current=$1
+ url=$2
+ } else {
+ if ($1 < current) {
+ current=$1
+ url=$2
+ }
+ }
+ printf("%6.2f %s\n", $1, $2) > "/dev/stderr"
+ }
+ END { print url }'
+}
+
add_fastest_mirror() {
- local tmp_mirror_nslookup
- local tmp_mirror_time
- local tmp_mirror_time_failed
- local tmp_mirror_rtt
- local mirror_lowest_rtt
- local mirror_lowest_rtt_mirror
-
- echo -n "Finding fastest mirror... "
- mirror_lowest_rtt=-1
- for mirror in $MIRRORS; do
- tmp_mirror_time="`(time wget -qO - $mirror) 2>&1 || echo "E_MIRROR_FAILED"`"
- tmp_mirror_time_failed=`echo $tmp_mirror_time | grep "E_MIRROR_FAILED"`
- if [ ${#tmp_mirror_time_failed} -eq 0 ]; then
- tmp_mirror_rtt=`echo "$tmp_mirror_time" | grep -E "^real" | sed -r "s/^real[ ]+[0-9]+m[ ]+([0-9]+)\.([0-9]+)s$/\1\2/"`
- if [ $mirror_lowest_rtt -eq -1 ]; then
- mirror_lowest_rtt=$tmp_mirror_rtt
- mirror_lowest_rtt_mirror=$mirror
- else
- if [ $tmp_mirror_rtt -lt $mirror_lowest_rtt ]; then
- mirror_lowest_rtt=$tmp_mirror_rtt
- mirror_lowest_rtt_mirror=$mirror
- fi
- fi
- fi
- done
- mirror=$mirror_lowest_rtt_mirror
- if [ -z "$mirror" ]; then
- eecho "Failed to ping any mirror"
- return 1
- fi
- add_mirror "$mirror"
+ echo "Finding fastest mirror... "
+ local fastest=$(find_fastest_mirror)
+ add_mirror "$fastest"
}
# show mirrors and store how many in global mirror_count