Skip to content

Commit 430f284

Browse files
committed
[docker-hub-rate] check if rate is a number. Ignore x-ratelimit-remaining in response.
1 parent 4218060 commit 430f284

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

src/docker_hub_rate.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ docker_hub_rate() {
6262
# Usage: echo "${LOGS}" | log_lines LEVLE
6363
log_lines() { local LEVEL="${1}"; while read -r LINE; do [ -z "${LINE}" ] && continue; log "${LEVEL}" "${LINE}"; done; }
6464
fi
65+
if ! type is_number 1>/dev/null 2>/dev/null; then
66+
is_number() { [ "${1}" -eq "${1}" ] 2>/dev/null; }
67+
fi
6568
if ! _curl_installed; then
6669
# wget has the following limitation:
6770
# 1. The `busybox wget` does not send a HEAD request (even with `--spider`), thus it will consume a docker hub rate.
@@ -89,9 +92,12 @@ docker_hub_rate() {
8992
_docker_hub_echo_error "GET RATE RESPONSE ERROR" "${RESPONSE}"
9093
return 1
9194
fi
95+
# example ${RESPONSE} :
96+
# HTTP/2 200 date: Mon, 22 Jun 2026 21:46:03 GMT content-type: application/vnd.docker.distribution.manifest.v2+json content-length: 527 docker-content-digest: sha256:<sha256> docker-distribution-api-version: registry/2.0 etag: "sha256:<sha256>" strict-transport-security: max-age=31536000 ratelimit-limit: 100;w=21600 ratelimit-remaining: 100;w=21600 docker-ratelimit-source: <ip address>
97+
# We also want to ignore "x-ratelimit-remaining: 100;".
9298
local RATE=
93-
RATE=$(echo "${RESPONSE}" | sed -n -E 's/.*ratelimit-remaining: (-?[0-9]+);.*/\1/p' )
94-
if [ -z "${RATE}" ]; then
99+
RATE=$(echo "${RESPONSE}" | sed -n -E 's/(^|.*[^-])ratelimit-remaining: (-?[0-9]+);.*/\2/p' )
100+
if [ -z "${RATE}" ] || ! is_number "${RATE}"; then
95101
_docker_hub_echo_error "PARSE RATE ERROR" "${RESPONSE}"
96102
return 1
97103
fi

0 commit comments

Comments
 (0)