Plainnotes

curl 的四种超时,别再只设一个

2025-02-19 · 2 分钟工具

脚本里 curl --max-time 10 看似安全,但它只管总时长。一个连接被防火墙静默丢弃的场景,会一直卡到 10 秒,即使你只想等 3 秒。

四个参数:

  • --connect-timeout 3:TCP 建连。
  • --max-time 10:整个请求的上限。
  • --speed-limit / --speed-time:低于某速度持续多久就放弃,适合大文件下载。
  • -m 和 --connect-timeout 同时设时,建连失败会立即返回。

实践中:

curl -sS --connect-timeout 3 --max-time 10      --retry 2 --retry-delay 1      -o /dev/null -w '%{http_code} %{time_total}\n' https://example.com/

--retry 默认只对连接错误和 5xx 重试,--retry-all-errors 才会对所有错误重试。