cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Keepalive

From: Daniel Stenberg <daniel_at_haxx.se>
Date: Tue, 18 Mar 2008 00:28:29 +0100 (CET)

On Mon, 17 Mar 2008, Tim McClarren wrote:

> I am using libcurl for transport inside my application. I use a multihandle
> associated with a single background thread to handle network activity.
> easyhandles are added and removed from this multihandle to service requests.
>
> I have restructured the code a bit to attempt to reuse easy handles so that
> keepalive will work.

With recent libcurls there's no reason at all to twiddle around, as the
connection cache is then kept by the multi handle so you can kill and re-use
easy handles all you want and the re-using of connections should still work
fine!

> The behavior I'm seeing (using Sysinternals TCPView) is that libcurl is not
> keeping this sockets open. They go to the "CLOSE_WAIT" state sometime after
> completion (a few seconds).

libcurl doesn't explicitly keep them open. It just leaves them open and
expects them to remain so unless the server closes them.

> From there, any new requests that are created which are going to connect to
> the same host cause a new socket to open (I can tell because the source port
> is different).

When the time comes and libcurl wants to re-use a connection, it first checks
if the connection seems dead and if so it kills it and creates a fresh one
instead. At times it'll even try to use the connection only to find it died
and then it creates a new one.

> Is the behavior of how libcurl decides a particular easyhandle is "reusable"
> documented somewhere?

No. But there's nothing in that an application author in general should need
to consider or even bother about.

> How do I determine how long libcurl is going to keep a finished connection's
> socket "alive"?

libcurl keeps them around forever until the cache runs full and it needs to
kill off old connections.

> It seems as though the local end of the connection is closing, and not the
> server side.

Enable CURLOPT_VERBOSE and libcurl will tell you when it closes connections.

-- 
  Commercial curl and libcurl Technical Support: http://haxx.se/curl.html
Received on 2008-03-18