博客專欄

        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

        技術專區

        關閉
        主站蜘蛛池模板: 麦盖提县| 隆回县| 浠水县| 阿尔山市| 溧水县| 东至县| 定州市| 江津市| 奇台县| 淳安县| 元阳县| 深泽县| 荣成市| 电白县| 山丹县| 阿拉尔市| 齐齐哈尔市| 崇信县| 墨江| 柏乡县| 迁安市| 油尖旺区| 九龙城区| 剑阁县| 茶陵县| 巩义市| 西平县| 兖州市| 昭通市| 社旗县| 太原市| 南川市| 鄂伦春自治旗| 阿尔山市| 油尖旺区| 金华市| 砚山县| 五峰| 建瓯市| 都匀市| 永靖县|