curl-library
Problem regarding OPENSOCKETFUNCTION & CLOSESOCKETFUNCTION
Date: Wed, 4 Jan 2012 02:32:19 +0000
Hi,
I am using CURLOPT_OPENSOCKETFUNCTION and CURLOPT_CLOSESOCKETFUNCTION callbacks to control the number of maximum connections allowed to use.
In my code, for each easy handle, I set these two callbacks as below.
curl_easy_setopt(m_curl, CURLOPT_OPENSOCKETFUNCTION, doOpenSocket);
curl_easy_setopt(m_curl, CURLOPT_OPENSOCKETDATA, this);
curl_easy_setopt(m_curl, CURLOPT_CLOSESOCKETFUNCTION, doCloseSocket);
curl_easy_setopt(m_curl, CURLOPT_CLOSESOCKETDATA, this);
In the OPENSOCKETFUNCTION callback, if maximum sockets are already opened, it queues this request up and return CURL_SOCKET_BAD.
curl_socket_t HttpClientTransfer::doOpenSocket(void* userp, curlsocktype purpose, struct curl_sockaddr* addr)
{
...
if (m_connectionsInUse >= getMaxConnections())
{
m_queue.push_back(tr);
tr->setQueued(true);
hcmWarn("Maximum sockets reached, request queued up (%d in queue) [worker %p]\n", m_queue.size(), this);
return CURL_SOCKET_BAD;
}
curl_socket_t sd = socket(addr->family, addr->socktype, addr->protocol);
if (sd >= 0)
{
++m_connectionsInUse;
hcmInfo("Open socket %d (%d in use now) [worker %p]\n", sd, m_connectionsInUse, this);
return sd;
}
else
{
hcmError("socket(): %s [worker %p]\n", strerror(errno), this);
return CURL_SOCKET_BAD;
}
}
int HttpClientTransfer::doCloseSocket(void* userp, curl_socket_t sd)
{
...
if (sd >= 0)
{
close(sd);
--m_connectionsInUse;
hcmInfo("Close socket %d (%d remain in use) [worker %p]\n", sd, m_connectionsInUse, this);
return 0;
}
else
{
hcmError("Invalid socket %d to close [worker %p]\n", sd, this);
return 1;
}
}
The problem I run into is the number of OPENSOCKET being called isn't equal to that of CLOSESOCKET, so we sometimes saw a situation that m_connectionsInUse becomes minus (shown in log as below).
<07:35:48.453 INF HCM 11686:11799 7:0>[closeSocket(HttpClientWorker.cpp:125)] Close socket 80 (-22 remain in use) [worker 0x1946ed0]
Any problem of the way that I'm using these two callbacks?
thanks,
yamin
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2012-01-04