在Linux系統下,開了200個線程分別curl同一個網站,結果程式爆錯了,一堆。
出錯資訊:Couldn't connect to server
開啟curl選項調試:
curl_easy_setopt(m_pCurl, CURLOPT_VERBOSE, 1);
也定位不了錯誤,後來發現200個線程,每次都有264個任務可以正常處理,而且程式的線程池會自動調度,空閑線程處理新任務,所以才會有264這個數字。
我訪問的網站是在Windows建立的一個網站後台,經過詢問,得知Windows的web伺服器使用的是apache。
此時錯誤問題可以初步診斷為:apache的最大串連數導致的問題。
於是經過一番修改(修改方法還是國外的,google偉大):
原文:
It seems that the httpd-mpm.conf file holds the answer. But I'm not sure what settings should be changed or even what module apache is running as.
httpd-mpm.conf:
# prefork MPM# StartServers: number of server processes to start# MinSpareServers: minimum number of server processes which are kept spare# MaxSpareServers: maximum number of server processes which are kept spare# MaxClients: maximum number of server processes allowed to start# MaxRequestsPerChild: maximum number of requests a server process serves<IfModule mpm_prefork_module> StartServers 5 MinSpareServers 5 MaxSpareServers 10 MaxClients 150 MaxRequestsPerChild 0</IfModule># worker MPM# StartServers: initial number of server processes to start# MaxClients: maximum number of simultaneous client connections# MinSpareThreads: minimum number of worker threads which are kept spare# MaxSpareThreads: maximum number of worker threads which are kept spare# ThreadsPerChild: constant number of worker threads in each server process# MaxRequestsPerChild: maximum number of requests a server process serves<IfModule mpm_worker_module> StartServers 2 MaxClients 150 MinSpareThreads 25 MaxSpareThreads 75 ThreadsPerChild 25 MaxRequestsPerChild 0</IfModule># BeOS MPM# StartThreads: how many threads do we initially spawn?# MaxClients: max number of threads we can have (1 thread == 1 client)# MaxRequestsPerThread: maximum number of requests each thread will process<IfModule mpm_beos_module> StartThreads 10 MaxClients 50 MaxRequestsPerThread 10000</IfModule># NetWare MPM# ThreadStackSize: Stack size allocated for each worker thread# StartThreads: Number of worker threads launched at server startup# MinSpareThreads: Minimum number of idle threads, to handle request spikes# MaxSpareThreads: Maximum number of idle threads# MaxThreads: Maximum number of worker threads alive at the same time# MaxRequestsPerChild: Maximum number of requests a thread serves. It is # recommended that the default value of 0 be set for this# directive on NetWare. This will allow the thread to # continue to service requests indefinitely. <IfModule mpm_netware_module> ThreadStackSize 65536 StartThreads 250 MinSpareThreads 25 MaxSpareThreads 250 MaxThreads 1000 MaxRequestsPerChild 0 MaxMemFree 100</IfModule># OS/2 MPM# StartServers: Number of server processes to maintain# MinSpareThreads: Minimum number of idle threads per process, # to handle request spikes# MaxSpareThreads: Maximum number of idle threads per process# MaxRequestsPerChild: Maximum number of connections per server process<IfModule mpm_mpmt_os2_module> StartServers 2 MinSpareThreads 5 MaxSpareThreads 10 MaxRequestsPerChild 0</IfModule># WinNT MPM# ThreadsPerChild: constant number of worker threads in the server process# MaxRequestsPerChild: maximum number of requests a server process serves<IfModule mpm_winnt_module> ThreadsPerChild 450 MaxRequestsPerChild 0</IfModule>
Answers:
The solution is to uncomment the MPM config include in httpd.conf
在httpd.conf檔案中,開啟該行注釋,否則修改無效:
# Server-pool management (MPM specific)
Include conf/extra/httpd-mpm.conf
apache有2種模式:prefork和worker
通過執行:httpd.exe -l,結果裡面沒有prefork.c檔案,說明原生apache使用的是worker模式,於是就修改:
<IfModule mpm_worker_module> StartServers 2 MaxClients 150 #最大串連數 MinSpareThreads 25 MaxSpareThreads 75 ThreadsPerChild 25 #每個子進程的服務線程數目 MaxRequestsPerChild 0 #單個子進程在其生命週期內處理的總請求數限制,當某個子進程處理過的總請求數到達這個限制後這個進程就會被回收,如果設為0,那麼這個進程永遠不會到期</IfModule>
修改MaxClients值,重啟apache服務,問題解決!