1. <nobr id="easjo"><address id="easjo"></address></nobr>

      <track id="easjo"><source id="easjo"></source></track>
      1. 
        

      2. <bdo id="easjo"><optgroup id="easjo"></optgroup></bdo>
      3. <track id="easjo"><source id="easjo"><em id="easjo"></em></source></track><option id="easjo"><span id="easjo"><em id="easjo"></em></span></option>
          貴州做網站公司
          貴州做網站公司~專業!靠譜!
          10年網站模板開發經驗,熟悉國內外開源網站程序,包括DEDECMS,WordPress,ZBlog,Discuz! 等網站程序,可為您提供網站建設,網站克隆,仿站,網頁設計,網站制作,網站推廣優化等服務。我們專注高端營銷型網站,企業官網,集團官網,自適應網站,手機網站,網絡營銷,網站優化,網站服務器環境搭建以及托管運維等。為客戶提供一站式網站解決方案?。?!

          CURL的超時與重試

          來源:互聯網轉載 時間:2024-01-29 08:31:21

          curl 的功能非常強大, 參數也很繁多, 我們不僅常用于命令行, 在php中也有類似 curl 拓展的實現, 并且也對 libcurl 庫提供了非常好的支持.

          curl 項目: https://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.comcurl: (28) Connection timed out after 2002 milliseconds

          “顯示連接超時, 超時時間2002毫秒. 注意這個 warning 的時間可能每次統計不太一樣, 一般會超過我們的預設值一點.

          #對于一個對返回時間要求比較高的情況, 可以設置為浮點型精確到毫秒curl --connect-timeout 0.3 --url http://xxx.comcurl: (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一次

          php 使用 guzzle 包

          Guzzle是一個PHP的HTTP客戶端,用來輕而易舉地發送請求,并集成到我們的WEB服務上.

          快速安裝

          {  "require": {    "guzzlehttp/guzzle": "~5.3|~6.0"  },  "repositories": {    "packagist": {      "type": "composer",      "url": "https://mirrors.aliyun.com/composer/"    }  }}

          執行 composer

          composer install

          示例代碼(超時機制)

          require "./vendor/autoload.php";$client = new GuzzleHttpClient();$res = $client->request('GET',    'http://xxx.com',    [        'connect_timeout' => 3,        'timeout'         => 2,    ]);echo $res->getBody() . PHP_EOL;

          output:

          PHP Fatal error:  Uncaught GuzzleHttpExceptionConnectException:cURL error 28: Connection timed out after 2002 milliseconds(see https://curl.haxx.se/libcurl/c/libcurl-errors.html)....

          “我們配置了 connect_timeout 超時時間 3 s, timeout超時時間 2 s

          guzzle 重試機制

          重試機制比較麻煩一點, 需要使用 Middleware 來實現, 但也很好理解

          require "./vendor/autoload.php";$handlerStack = GuzzleHttpHandlerStack::create();//綁定中間件, 重試三次$restryCount = 3;$handlerStack->push(GuzzleHttpMiddleware::retry(function () use (&$restryCount) {    if (--$restryCount <= 0) {        return false;    }    return true;}, function () use ($restryCount) {    return 1000; //毫秒}));$client = new GuzzleHttpClient(['handler' => $handlerStack]);$res = $client->request('GET',    'http://xxx.xxx.com',    [        'connect_timeout' => 3,        'timeout'         => 2,    ]);echo $res->getBody() . PHP_EOL;

          “在定義 retry 的時間, 你需要去實現是否繼續重試, 重試的時間等策略, 提供了巨大的重試靈活性.

          “值得注意的是 curl 的重試時間單位是秒, 而這里是設置的毫秒.

          網絡推廣與網站優化公司(網絡優化與推廣專家)作為數字營銷領域的核心服務提供方,其價值在于通過技術手段與策略規劃幫助企業提升線上曝光度、用戶轉化率及品牌影響力。這...

          在當今數字化時代,公司網站已成為企業展示形象、傳遞信息和開展業務的重要平臺。然而,對于許多公司來說,網站建設的價格是一個關鍵考量因素。本文將圍繞“公司網站建設價...

          在當今的數字化時代,企業網站已成為企業展示形象、吸引客戶和開展業務的重要平臺。然而,對于許多中小企業來說,高昂的網站建設費用可能會成為其發展的瓶頸。幸運的是,隨...

          有沒有人花888充值淘寶88會員?懂的人說說怎么樣吧?過來看看,有個傻女孩[羅斯],因為她一年前是88kai的會員,但續約時沒注意。當時規定1000分以下需要888分。我沒注意[遮住我的臉]。當我發現的時候,它已經在支付界面上了。當時,我的手機是華為榮耀。返回按鈕和指紋支付按鈕是相同的。當我想按返回按鈕時,付款成功了。后來聯系淘寶客服,他們還給我了??头χf不相信有人會買888。初衷是讓大家有一...

          太多HTTP重定向,怎么解決?退出app store,從Apple mobile phone中打開[settings],然后下拉界面找到[iTunes store and app store]選項,點擊open,然后點擊[app store]的personal account選項。然后,在彈出窗口中,單擊[注銷]按鈕。注銷后,長按手機電源按鈕,即可關閉手機并重新啟動。然后輸入設置并再次登錄Appl...

          2022個人所得稅計算器?個稅的計算除了工資,還要看社保和公積金的繳費基數、比例和專項附加扣除。如果社保和公積金也按照這個數額繳納,比例分別為5%和8%。3歲以下嬰幼兒,稅后工資14964元,可以根據自己的數據進行調整和重新計算。2022個人所得稅計算器?2022年個人所得稅的計算公式為:本月應扣繳的稅額=(本月應扣繳的預繳稅款收入累計額扣繳稅率-速算扣除)-累計減免稅額-累計扣繳的預繳稅款金額;...

          TOP
          国产初高中生视频在线观看|亚洲一区中文|久久亚洲欧美国产精品|黄色网站入口免费进人
          1. <nobr id="easjo"><address id="easjo"></address></nobr>

              <track id="easjo"><source id="easjo"></source></track>
              1. 
                

              2. <bdo id="easjo"><optgroup id="easjo"></optgroup></bdo>
              3. <track id="easjo"><source id="easjo"><em id="easjo"></em></source></track><option id="easjo"><span id="easjo"><em id="easjo"></em></span></option>