我正在编写一个使用线程池的服务器。每个线程连接到SQL数据库,然后在启动时调用同一服务器套接字上的accept
,并等待客户端连接。我发现当且仅当我产生太多线程时,curl_easy_perform
函数在第一次调用时失败,返回错误代码56(CURL_RECV_ERROR
)。限制在950到970个线程之间,但我的计算机允许我为程序创建多达2047个线程,所以这个限制对我来说似乎是任意的。我的内存也不低。
只有一个线程在它给我错误之前调用cURL函数一次,所以我在这个测试中没有同时执行多个cURL请求。我做错了什么,或者我必须接受cURL的线程限制吗?
这些是我发送HTTPS请求和使用cURL获取响应的函数:
size_t write_data(void *ptr, size_t size, size_t nmemb, struct url_data *s){ // callback function I have to use with Curl
size_t new_len = s->len + size*nmemb;
s->ptr = erealloc(s->ptr, new_len+1);
memcpy(s->ptr+s->len, ptr, size*nmemb);
s->ptr[new_len] = '\0';
s->len = new_len;
return size*nmemb;
}
char* curlHTTPS(char* url){ // returns the response as a string or NULL upon error
url = strdup(url);
CURL* curl = curl_easy_init();
struct url_data* response = emalloc(sizeof(struct url_data));
response->len = 0;
response->ptr = emalloc(4096*sizeof(char));
curl_easy_setopt(curl, CURLOPT_URL, url); // Define target site
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
curl_easy_setopt(curl, CURLOPT_USERAGENT, userAgent);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_HEADER, false);
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, response);
CURLcode curlCode = curl_easy_perform(curl);
if (curlCode!=CURLE_OK){
if (logLevel >= LOG_ERROR) printf("Curl failed! Error: %d\n", curlCode);
curl_easy_cleanup(curl);
efree(response->ptr);
efree(response);
return NULL;
}
char* ret = strdup(response->ptr);
curl_easy_cleanup(curl);
efree(response);
return ret;
}