Sleep Rate Limit API Polling
Poll rate-limited APIs politely with sleep pacing, exponential spacing for empty results, and a hard attempt ceiling.
Code
# throttle the polling rate to stay under the API limit
#!/usr/bin/env bash
set -euo pipefail
readonly URL="https://api.example.com/jobs/${JOB_ID:-job-1}"
readonly BASE_INTERVAL=5
readonly MAX_INTERVAL=60
readonly MAX_ATTEMPTS=20
wait_for_completion() {
local interval=${BASE_INTERVAL}
local attempt=0
while (( attempt < MAX_ATTEMPTS )); do
(( attempt++ )) || true
local status body
body=$(curl -sf --max-time 8 "${URL}") || {
echo "请求失败,${interval}s 后重试" >&2
sleep "${interval}"
continue
}
status=$(echo "${body}" | jq -r '.status')
case "${status}" in
completed)
echo "${body}" | jq -r '.resultUrl'
return 0
;;
failed)
echo "Error: 任务失败:$(echo "${body}" | jq -r '.error')" >&2
return 1
;;
pending|running)
echo "状态 ${status},${interval}s 后再查 (${attempt}/${MAX_ATTEMPTS})" >&2
;;
*)
echo "未知状态: ${status}" >&2
return 2
;;
esac
# 未完成时退避增长:对 API 更礼貌
interval=$(( interval * 2 ))
(( interval > MAX_INTERVAL )) && interval=${MAX_INTERVAL}
sleep "${interval}"
done
echo "Error: 轮询 ${MAX_ATTEMPTS} 次仍未完成" >&2
return 1
}
wait_for_completion
What the code does
set -euo pipefail puts the script into strict mode: it stops on the first failing command, treats unset variables as errors, and fails a pipeline if any stage fails.
The work is delegated to readonly and sleep, chained through 8 pipes so each stage’s output feeds the next.
A helper function (wait_for_completion) keeps the repeated logic out of the main flow.
The script leans on parameter expansion 17 times, using forms like ${var:-default} so defaults, trimming, and casing happen inside the shell with no external tool.
Watch out for
- The pipeline is 8 stages deep — when it misbehaves, split the pipe and test the stages separately.
Statements that decide the outcome
while (( attempt < MAX_ATTEMPTS )); do
(( attempt++ )) || true
body=$(curl -sf --max-time 8 "${URL}") || {
pending|running)
Interactive Tools for This Topic
FAQ
- Does the script stop on the first error?
- Yes — `set -euo pipefail` aborts on the first failing command, treats unset variables as errors, and fails the whole pipeline if any stage fails. Nothing continues half-broken.
- Which external commands does the script rely on?
- `readonly`, `sleep` must be available on the PATH environment variable; everything else is shell built-in syntax.
Report a problem with this exampleOpens a pre-filled issue on GitHub.