中文字幕 另类精品,亚洲欧美一区二区蜜桃,日本在线精品视频免费,孩交精品乱子片免费

<sup id="3hn2b"></sup>

    1. <sub id="3hn2b"><ol id="3hn2b"></ol></sub><legend id="3hn2b"></legend>

      1. <xmp id="3hn2b"></xmp>

      2. "); //-->

        博客專(zhuān)欄

        EEPW首頁(yè) > 博客 > linux shell curl 超時(shí)與重試

        linux shell curl 超時(shí)與重試

        發(fā)布人:電子禪石 時(shí)間:2022-12-23 來(lái)源:工程師 發(fā)布文章

        curl 的功能非常強(qiáng)大, 參數(shù)也很繁多, 我們不僅常用于命令行, 在php中也有類(lèi)似 curl 拓展的實(shí)現(xiàn), 并且也對(duì) libcurl 庫(kù)提供了非常好的支持.

        curl 項(xiàng)目: github.com/curl/curl

        curl 關(guān)于時(shí)間控制和重試的參數(shù)

        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

        上面是我整理的一部分跟時(shí)間有關(guān)系的參數(shù), 蝦米啊我們依次實(shí)驗(yàn)下.

        連接超時(shí)參數(shù) connect-timeout

        說(shuō)明

        --connect-timeout SECONDS  Maximum time allowed for connection

        示例

        #這里我們?cè)O(shè)置超時(shí)時(shí)間為2s, 請(qǐng)求一個(gè)無(wú)法解析的地址
        curl --connect-timeout 2 --url http://xxx.com
         
        curl: (28) Connection timed out after 2002 milliseconds

        顯示連接超時(shí), 超時(shí)時(shí)間2002毫秒. 注意這個(gè) warning 的時(shí)間可能每次統(tǒng)計(jì)不太一樣, 一般會(huì)超過(guò)我們的預(yù)設(shè)值一點(diǎn).


        #對(duì)于一個(gè)對(duì)返回時(shí)間要求比較高的情況, 可以設(shè)置為浮點(diǎn)型精確到毫秒
        curl --connect-timeout 0.3 --url http://xxx.com
         
        curl: (28) Connection timed out after 300 milliseconds


        請(qǐng)求超時(shí)時(shí)間 --max-time

        說(shuō)明

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


        #這里我們?cè)O(shè)置超時(shí)時(shí)間為2s, 應(yīng)用程序中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 聯(lián)合使用:

        #這里我們使用了一個(gè)無(wú)法解析的地址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

        這里我們發(fā)現(xiàn)返回結(jié)果為連接超時(shí) 2001 毫秒, 當(dāng)共同使用時(shí), 連接以最小時(shí)間的為準(zhǔn), 而返回時(shí)間已 max-time 限制為準(zhǔn).

        請(qǐng)求重試 retry

        說(shuō)明

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

        示例

        #同樣,我們?nèi)フ?qǐng)求一個(gè) 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

        我們發(fā)現(xiàn)重試了3次, 但它并不是失敗后立刻重試, 而是第一次 1 s后重試, 第二次 2 s后重試, 第三次 4 s后重試,依次遞增 (每次重試受 max-time 限制).

        重試超時(shí)時(shí)間 retry-max-time

        我們發(fā)現(xiàn)我們的 max-time 只是對(duì)單次請(qǐng)求做了時(shí)間限制, 進(jìn)而去影響總的重試時(shí)間, 但是我們想在單位時(shí)間內(nèi)完成重試該怎么做呢. 這里 curl 也提供了重試的超時(shí)時(shí)間 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

        我們對(duì)重試總的超時(shí)時(shí)間設(shè)置為2s, 配置了3次重試, 但僅僅完成了兩次重試就超時(shí)結(jié)束了.

        重試延遲 retry-delay

        我們?cè)?請(qǐng)求重試 里面講到, 這里的重試并不是失敗后立刻重試的, 默認(rèn)重試時(shí)間遞增, 這里我們可以使用 retry-delay 控制重試的間隔.

        #這里我們?cè)O(shè)置重試時(shí)間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

        我們發(fā)現(xiàn) Will retry in 變成了 5 s一次



        *博客內(nèi)容為網(wǎng)友個(gè)人發(fā)布,僅代表博主個(gè)人觀(guān)點(diǎn),如有侵權(quán)請(qǐng)聯(lián)系工作人員刪除。



        關(guān)鍵詞: curl

        技術(shù)專(zhuān)區(qū)

        關(guān)閉