cURL / Mailing Lists / curl-library / Single Mail

curl-library

SO_REUSEADDR and multi-threaded environment

From: Sachin Nikumbh <sanikumbh_at_gmail.com>
Date: Tue, 1 Jul 2014 13:18:48 -0400

Hi,

I have a client application that's using libcurl to communicate with a
proprietary server. I am testing the application in multi-threaded
environment and how it scales with the number of threads.

Since, it's documented that a CURL handle should not be shared across
multiple threads at the same time, each thread in my test gets its own CURL
handle. After a request, connection is closed by each thread. I see that a
lot of ports are being left in TIME_WAIT state after running my application
which I can understand.

At around 30000 number of threads, some of the requests started failing
with "Couldn't connect the server error" which I am assuming is happening
because my application is failing to find any more ports to bind to. With
the help from this group, I decided to set the SO_REUSEADDR socket option
in a hope to reuse the sockets in the TIME_WAIT state. Following is a
snippet of my code that I wrote to set SO_REUSEADDR socket option:

*******************************
int reuseaddr = 1;
curl_easy_setopt(curlHnd, CURLOPT_SOCKOPTDATA, &reuseaddr);
curl_easy_setopt(curlHnd, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
...
...
int sockopt_callback(void *clientp, curl_socket_t curlfd, curlsocktype
purpose)
{
    int status = setsockopt( curlfd, SOL_SOCKET, SO_REUSEADDR, clientp,
sizeof(int) );
    std::cout << status << " " << std::endl;
    return (status==0) ? CURL_SOCKOPT_OK : CURL_SOCKOPT_ERROR;
}
*******************************

But the behavior of my application is not changing. I still see that some
requests are failing with "Couldn't connect the server error". I am sure
that the call to setsockopt is successful as I am not seeing -1 for status.
Am I missing something? Is there anything else I need to do either in
sockopt_callback or is there any other option that I need to set to make
this work?

Any help will be greatly appreciated.

Thanks
Sachin

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2014-07-01