libcurl與CLOSE_WAIT轉載自:http://blog.sunshow.net/2010/03/libcurl-and-close-wait/
調用libcurl下載,然后使用netstat查看發現有大量的TCP連接保持在CLOSE_WAIT狀態
查看libcurl的文檔說明,有這樣一個選項:
CURLOPT_FORBID_REUSE
Pass a long. Set to 1 to make the next transfer explicitly close the connection when done. Normally, libcurl keeps all connections alive when done with one transfer in case a succeeding one follows that can re-use them. This option should be used with caution and only if you understand what it does. Set to 0 to have libcurl keep the connection open for possible later re-use (default behavior).
也就是說,默認情況下libcurl完成一個任務以后,出于重用連接的考慮不會馬上關閉
如果沒有新的TCP請求來重用這個連接,那么只能等到CLOSE_WAIT超時,這個時間默認在7200秒甚至更高,太多的CLOSE_WAIT連接會導致性能問題
解決方法:
curl_easy_setopt(curl, CURLOPT_FORBID_REUSE, 1);
最好再修改一下TCP參數調低CLOSE_WAIT和TIME_WAIT的超時時間
libcurl 使用筆記轉載自:http://gcoder.blogbus.com/logs/54871550.html
libcurl 是一個很不錯的庫,支持http,ftp等很多的協議。使用庫最大的心得就是,不仔細看文檔,僅僅看著例子就寫程序,是一件危險的事情。我的程序崩潰了,我懷疑是自己代碼寫的問題,后來發現是庫沒用對。不仔細看文檔(有時候文檔本身也比較差勁,這時除了看仔細外,還要多動腦子,考慮它是怎么實現的),后果很嚴重。不加思索的使用別人的庫或者代碼,有時候很愜意,但是出問題時,卻是寢食難安的。
1. CURLcode curl_global_init(long flags); 在多線程應用中,需要在主線程中調用這個函數。這個函數設置libcurl所需的環境。通常情況,如果不顯式的調用它,第一次調用curl_easy_init()時,curl_easy_init 會調用 curl_global_init,在單線程環境下,這不是問題。但是多線程下就不行了,因為curl_global_init不是線程安全的。在多個線程中調用curl_easy_int,然后如果兩個線程同時發現curl_global_init還沒有被調用,同時調用curl_global_init,悲劇就發生了。這種情況發生的概率很小,但可能性是存在的。
2. libcurl 有個很好的特性,它甚至可以控制域名解析的超時。但是在默認情況下,它是使用alarm + siglongjmp 實現的。用alarm在多線程下做超時,本身就幾乎不可能。如果只是使用alarm,并不會導致程序崩潰,但是,再加上siglongjmp,就要命了(程序崩潰的很可怕,core中幾乎看不出有用信息),因為其需要一個sigjmp_buf型的全局變量,多線程修改它。(通常情況下,可以每個線程一個 sigjmp_buf 型的變量,這種情況下,多線程中使用 siglongjmp 是沒有問題的,但是libcurl只有一個全局變量,所有的線程都會用)。
具體是類似 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L) 的超時設置,導致alarm的使用(估計發生在域名解析階段),如前所述,這在多線程中是不行的。解決方式是禁用掉alarm這種超時, curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L)。
這樣,多線程中使用超時就安全了。但是域名解析就沒了超時機制,碰到很慢的域名解析,也很麻煩。文檔的建議是 Consider building libcurl with c-ares support to enable asynchronous DNS lookups, which enables nice timeouts for name resolves without signals. c-ares 是異步的 DNS 解決方案。
libcurl 多線程使用注意事項
轉載自:http://blog.csdn.net/jaylong35/article/details/6439549
1、問題來源,多線程使用Libcurl導致程序跑一段時間后自己退出,沒有明顯的異常。找不到合適的BUG。
最后通過查看資料和網上找的一些文章,發現,原來是信號處理的問題:
CURLOPT_NOSIGNAL
Pass a long. If it is 1, libcurl will not use any functions that install signal handlers or any functions that cause signals to be sent to the process. This option is mainly here to allow multi-threaded unix applications to still set/use all timeout options etc, without risking getting signals. (Added in 7.10)
If this option is set and libcurl has been built with the standard name resolver, timeouts will not occur while the name resolve takes place. Consider building libcurl with c-ares support to enable asynchronous DNS lookups, which enables nice timeouts for name resolves without signals.
Setting CURLOPT_NOSIGNAL to 1 makes libcurl NOT ask the system to ignore SIGPIPE signals, which otherwise are sent by the system when trying to send data to a socket which is closed in the other end. libcurl makes an effort to never cause such SIGPIPEs to trigger, but some operating systems have no way to avoid them and even on those that have there are some corner cases when they may still happen, contrary to our desire.
就是當多個線程都使用超時處理的時候,同時主線程中有sleep或是wait等操作。如果不設置這個選項,libcurl將會發信號打斷這個wait從而導致程序退出。
所以,在使用的時候把這個選項設置成1就可以了.
curl_setopt(curl, CURLOPT_NOSIGNAL, 1L);
2、關于libcurl庫的初始化和關閉:curl_global_init()和curl_global_cleanup()
這兩個函數并不是線程安全的。所以只能在主線程中進行一次的初始化和清除。
雖然這個不是一定就會有問題,但是如果不這樣處理還是有概率發生的。