cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Problem with reusing persistent connections

From: Daniel Stenberg <daniel_at_haxx.se>
Date: Tue, 2 Oct 2007 12:06:03 +0200 (CEST)

On Tue, 25 Sep 2007, Stefan Krause wrote:

> I have one multi handle (MH). Up to two easy handles (EH1, EH2) may be added
> to the multi handle at a time.
> A segmented data transfer is done via EH1. Therefore EH1 is setup and added
> to the MH. After EH1 has been completed with the first segment (using the
> HTTP Content-Length header to retrieve a segment from the server), it is
> removed from the MH. That is repeated until all segments have been
> transferred. After the last segment has been transferred, the EH1 is
> destroyed by calling curl_easy_cleanup() on the handle. I expected that the
> socket connection of the handle would have been closed after. But that was
> not the case. The socket connection still exists.

Correct. Since 7.16.0, the connection pool is kept in the multi handle so that
easy handles can re-use them even more and easier than before. It was also
more or less necessary to allow pipelining.

> After I setup EH2 and add it to the MH, the socket connection which has been
> used by EH1 has also been used by EH2. Is the reason for that, that the EH1
> and EH2 does not have a private connection cache, but share the connection
> cache from the MH?. So curl_easy_cleanup() does not have the effect of
> closing open socket connections. I think I have to clean the MH with
> curl_multi_cleanup() in order to close all socket connections? Is that
> correct?

Yes.

> The sequence now to close all socket connections when using a multi handle
> will be the following according to my understanding:
>
> 1) Remove all EH from the MH (Is there a possibility to determine all
> currently added EH, or do I have to track them separately?)
> 2) Cleanup the MH
> 3) Cleanup all EH
>
>
> Now restore the privious state but without any open socket connection:
> 1) Create a new MH
> 2) Create new EH, restore their states
> 3) Add all EH to the MH
>
> Might that be an solution? Do you have any comments / recommendations
> concerning that idea?

I would rather say that you should work towards not requiring a close as it is
a lot better performance wise to keep using the connection(s) your application
already has.

But if you REALLY need to close the connections then yes, you need to kill the
multi handle for that.

-- 
  Commercial curl and libcurl Technical Support: http://haxx.se/curl.html
Received on 2007-10-02