Use --speed-time over --max-time for API downloads

This commit is contained in:
Bo Anderson 2023-02-09 18:01:41 +00:00
parent c9496ae50f
commit a26a2d71a8
No known key found for this signature in database
GPG Key ID: 3DB94E204E137D65
2 changed files with 14 additions and 8 deletions

View File

@ -19,8 +19,10 @@ module Homebrew
HOMEBREW_CACHE_API = (HOMEBREW_CACHE/"api").freeze HOMEBREW_CACHE_API = (HOMEBREW_CACHE/"api").freeze
# Set a longer timeout just for large(r) files. # Timeout values to check for dead connections
JSON_API_MAX_TIME = 30 # We don't use --max-time to support slow connections
JSON_API_SPEED_MARGIN = 100 # bytes/sec
JSON_API_SPEED_TIME = 10 # seconds of downloading under the margin
sig { params(endpoint: String).returns(Hash) } sig { params(endpoint: String).returns(Hash) }
def fetch(endpoint) def fetch(endpoint)
@ -45,15 +47,14 @@ module Homebrew
retry_count = 0 retry_count = 0
url = "#{Homebrew::EnvConfig.api_domain}/#{endpoint}" url = "#{Homebrew::EnvConfig.api_domain}/#{endpoint}"
default_url = "#{HOMEBREW_API_DEFAULT_DOMAIN}/#{endpoint}" default_url = "#{HOMEBREW_API_DEFAULT_DOMAIN}/#{endpoint}"
curl_args = %w[--compressed --silent] curl_args = %W[--compressed --speed-limit #{JSON_API_SPEED_MARGIN} --speed-time #{JSON_API_SPEED_TIME}]
curl_args.prepend("--silent") unless Context.current.debug?
curl_args.prepend("--time-cond", target) if target.exist? && !target.empty? curl_args.prepend("--time-cond", target) if target.exist? && !target.empty?
begin begin
begin begin
# Disable retries here, we handle them ourselves below. # Disable retries here, we handle them ourselves below.
Utils::Curl.curl_download(*curl_args, url, to: target, Utils::Curl.curl_download(*curl_args, url, to: target, retries: 0, show_error: false)
max_time: JSON_API_MAX_TIME, retries: 0,
show_error: false)
rescue ErrorDuringExecution rescue ErrorDuringExecution
if url == default_url if url == default_url
raise unless target.exist? raise unless target.exist?

View File

@ -785,11 +785,16 @@ EOS
JSON_URLS+=("${HOMEBREW_API_DEFAULT_DOMAIN}/${formula_or_cask}.json") JSON_URLS+=("${HOMEBREW_API_DEFAULT_DOMAIN}/${formula_or_cask}.json")
for json_url in "${JSON_URLS[@]}" for json_url in "${JSON_URLS[@]}"
do do
time_cond=()
if [[ -s "${HOMEBREW_CACHE}/api/${formula_or_cask}.json" ]]
then
time_cond=("--time-cond" "${HOMEBREW_CACHE}/api/${formula_or_cask}.json")
fi
curl \ curl \
"${CURL_DISABLE_CURLRC_ARGS[@]}" \ "${CURL_DISABLE_CURLRC_ARGS[@]}" \
--fail --compressed --silent --max-time 30 \ --fail --compressed --silent --speed-limit 100 --speed-time 30 \
--location --remote-time --output "${HOMEBREW_CACHE}/api/${formula_or_cask}.json" \ --location --remote-time --output "${HOMEBREW_CACHE}/api/${formula_or_cask}.json" \
--time-cond "${HOMEBREW_CACHE}/api/${formula_or_cask}.json" \ "${time_cond[@]}" \
--user-agent "${HOMEBREW_USER_AGENT_CURL}" \ --user-agent "${HOMEBREW_USER_AGENT_CURL}" \
"${json_url}" "${json_url}"
curl_exit_code=$? curl_exit_code=$?