From 00bf4eda2ad056d273af668462536d26c86afe3e Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 3 Dec 2024 15:07:47 +0100 Subject: [download] Refine download_retry timeout logic to stay in bounds The old implementation would overshoot the set --slx-time by up to 15 seconds. Fix this. --- modules/download.inc | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/modules/download.inc b/modules/download.inc index 8c717eb..46f4f14 100644 --- a/modules/download.inc +++ b/modules/download.inc @@ -1,25 +1,31 @@ #!/bin/ash download() { - curl --location --max-redirs 5 --max-time 15 --connect-timeout 2 "$@" + curl --location --max-redirs 5 --max-time 15 --connect-timeout 3 "$@" } download_retry() { - local delay max_time start end ret - max_time=8 + local delay total_time start end ret timeout + total_time=10 ret=100 if [ "$1" = "--slx-time" ]; then - max_time="$2" + total_time="$2" shift 2 fi + if ! [ "$total_time" -gt 0 ]; then + echo "download_retry: passed timeout is not > 0" >&2 + return 1 + fi start="$( date +%s )" - for delay in 1 2 2 3 3 3 4 4 4 5 5 5 5 5 5 5 5 5 ""; do - download "$@" + end="$start" + for delay in 0 1 1 2 2 3 3 3 4 4 4 5 5 5 5 5 5 5 5 5; do + timeout=$(( total_time - delay - (end - start) )) + [ "$timeout" -gt 0 ] || break + sleep "$delay" + download --max-time "$timeout" "$@" ret="$?" [ "$ret" = 0 ] && return 0 end="$( date +%s )" - [ "$(( end - start ))" -ge "$max_time" ] && break - [ -n "$delay" ] && sleep "$delay" done return "$ret" } -- cgit v1.2.3-55-g7522