Curl

curl有超時嗎?

  • October 31, 2018

到目前為止,我真的找不到任何東西,但真的curl沒有真正超時嗎?

user@host:~# curl http://localhost/testdir/image.jpg

我之所以問,是因為我將對圖像的任何請求重定向testdir到一個單獨的 Apache 模組,該模組會即時生成這些圖片。圖片實際準備好並傳遞給請求的客戶可能需要長達 15 分鐘的時間。

curl始終等待(或者取決於配置)還是有任何超時?

是的。

超時參數

curl有兩個選擇:--connect-timeout--max-time

從手冊頁引用:

--connect-timeout <seconds>
   Maximum  time  in  seconds  that you allow the connection to the
   server to take.  This only limits  the  connection  phase,  once
   curl has connected this option is of no more use.  Since 7.32.0,
   this option accepts decimal values, but the actual timeout  will
   decrease in accuracy as the specified timeout increases in deci‐
   mal precision. See also the -m, --max-time option.

   If this option is used several times, the last one will be used.

和:

-m, --max-time <seconds>
   Maximum  time  in  seconds that you allow the whole operation to
   take.  This is useful for preventing your batch jobs from  hang‐
   ing  for  hours due to slow networks or links going down.  Since
   7.32.0, this option accepts decimal values, but the actual time‐
   out will decrease in accuracy as the specified timeout increases
   in decimal precision.  See also the --connect-timeout option.

   If this option is used several times, the last one will be used.

預設值

在這裡(在 Debian 上)它會在 2 分鐘後停止嘗試連接,無論使用指定的時間如何,儘管根據lib/connect.h中的宏,--connect-timeout預設連接超時值似乎是5 分鐘DEFAULT_CONNECT_TIMEOUT

--max-time似乎不存在預設值,curl如果初始連接成功,則永遠等待響應。

用什麼?

您可能對後一個選項感興趣,--max-time. 對於您的情況,將其設置為900(15 分鐘)。

將選項指定--connect-timeout60(一分鐘)也可能是一個好主意。否則curl會一次又一次地嘗試連接,顯然使用了一些退避算法。

引用自:https://unix.stackexchange.com/questions/94604