• <s id="xpo4f"><samp id="xpo4f"><blockquote id="xpo4f"></blockquote></samp></s>
    <tbody id="xpo4f"></tbody>

    1. <dd id="xpo4f"></dd>
    2. <button id="xpo4f"></button>

      博客专栏

      EEPW首页 > 博客 > linux shell curl 超时与重试

      linux shell curl 超时与重试

      发布人:电子禅石 时间:2022-12-23 来源:工程师 发布文章

      curl 的功能非常强大, 参数也很繁多, 我们不仅常用于命令行, 在php中也有类似 curl 拓展的实现, 并且也对 libcurl 库提供了非常好的支持.

      curl 项目: github.com/curl/curl

      curl 关于时间控制和重试的参数
      curl --help
       
      --connect-timeout SECONDS  Maximum time allowed for connection
      -m, --max-time SECONDS  Maximum time allowed for the transfer
       
      ...
       
      --retry NUM   Retry request NUM times if transient problems occur
      --retry-delay SECONDS  Wait SECONDS between retries
      --retry-max-time SECONDS  Retry only within this period

      上面是我整理的一部分跟时间有关系的参数, 虾米啊我们依次实验下.

      连接超时参数 connect-timeout

      说明

      --connect-timeout SECONDS  Maximum time allowed for connection

      示例

      #这里我们设置超时时间为2s, 请求一个无法解析的地址
      curl --connect-timeout 2 --url http://xxx.com
       
      curl: (28) Connection timed out after 2002 milliseconds

      显示连接超时, 超时时间2002毫秒. 注意这个 warning 的时间可能每次统计不太一样, 一般会超过我们的预设值一点.


      #对于一个对返回时间要求比较高的情况, 可以设置为浮点型精确到毫秒
      curl --connect-timeout 0.3 --url http://xxx.com
       
      curl: (28) Connection timed out after 300 milliseconds


      请求超时时间 --max-time

      说明

      -m, --max-time SECONDS  Maximum time allowed for the transfer

      示例

      #这里我们设置超时时间为2s, 应用程序中sleep 2curl --max-time 2 --url http://www.shuai.comcurl: (28) Operation timed out after 2002 milliseconds with 0 bytes received

      connect-time 和 max-time 联合使用:

      #这里我们使用了一个无法解析的地址curl --connect-time 3  --max-time 2 --url http://xxx.com>  curl: (28) Connection timed out after 2001 millisecondscurl --connect-time 3  --max-time 4 --url http://xxx.com>  curl: (28) Operation timed out after 4002 milliseconds with 0 bytes received

      这里我们发现返回结果为连接超时 2001 毫秒, 当共同使用时, 连接以最小时间的为准, 而返回时间已 max-time 限制为准.

      请求重试 retry

      说明

      --retry NUM   Retry request NUM times if transient problems occur

      示例

      #同样,我们去请求一个 sleep 2 的地址curl --max-time 0.1 --retry 3  --url http://www.shuai.com> Warning: Transient problem: timeout Will retry in 1 seconds. 3 retries left.> Warning: Transient problem: timeout Will retry in 2 seconds. 2 retries left.> Warning: Transient problem: timeout Will retry in 4 seconds. 1 retries left.> curl: (28) Operation timed out after 100 milliseconds with 0 bytes received

      我们发现重试了3次, 但它并不是失败后立刻重试, 而是第一次 1 s后重试, 第二次 2 s后重试, 第三次 4 s后重试,依次递增 (每次重试受 max-time 限制).

      重试超时时间 retry-max-time

      我们发现我们的 max-time 只是对单次请求做了时间限制, 进而去影响总的重试时间, 但是我们想在单位时间内完成重试该怎么做呢. 这里 curl 也提供了重试的超时时间 retry-max-time

      curl --retry 3 --retry-max-time 2  --max-time 0.1 --url http://www.shuai.com> Warning: Transient problem: timeout Will retry in 1 seconds. 3 retries left.> Warning: Transient problem: timeout Will retry in 2 seconds. 2 retries left.> curl: (28) Operation timed out after 101 milliseconds with 0 bytes received

      我们对重试总的超时时间设置为2s, 配置了3次重试, 但仅仅完成了两次重试就超时结束了.

      重试延迟 retry-delay

      我们在 请求重试 里面讲到, 这里的重试并不是失败后立刻重试的, 默认重试时间递增, 这里我们可以使用 retry-delay 控制重试的间隔.

      #这里我们设置重试时间5s,重试3次curl --retry 3 --retry-delay 5 --max-time 0.1  --url http://xxx.com> Warning: Transient problem: timeout Will retry in 5 seconds. 3 retries left.> Warning: Transient problem: timeout Will retry in 5 seconds. 2 retries left.> Warning: Transient problem: timeout Will retry in 5 seconds. 1 retries left.> curl: (28) Connection timed out after 101 milliseconds

      我们发现 Will retry in 变成了 5 s一次



      *博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。



      关键词: curl

      技术专区

      关闭
      乱婬AV片免费影院观看
    3. <s id="xpo4f"><samp id="xpo4f"><blockquote id="xpo4f"></blockquote></samp></s>
      <tbody id="xpo4f"></tbody>

      1. <dd id="xpo4f"></dd>
      2. <button id="xpo4f"></button>