"); //-->
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
#这里我们使用了一个无法解析的地址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
请求重试 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
重试超时时间 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
重试延迟 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
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。