curl / Mailing Lists / curl-library / Single Mail


Re: Understanding sharing of state with "multi" interface

From: Daniel Stenberg <>
Date: Thu, 26 Jan 2017 17:36:41 +0100 (CET)

On Wed, 25 Jan 2017, corey kasten wrote:

> 1. Does a multi handle have its own connection "cache"?

Yes it does.

> And, when an easy handle is added to a multi handle, does that easy handle
> cease to have its own local connection "cache"?

No, handles added to the multi handle will then use the common one in the
multi handle.

> 2. Suppose I have two easy handles that have the same URL set, and one multi
> handle to which I add both easy handles, and then do curl_multi_perform().
> Will two connections be opened, one corresponding to each easy handle?

It depends. There are a lot of options and specifics that can make you change
behavior, but if you don't change any of that, and you add two easy handles at
once using the same URL, libcurl will create two connections as it will prio
getting things done over saving connections.

> 3. Now, suppose I have two easy handles, A and B, that have the same URL
> set, and one multi handle. Then, I do the following in sequence:
> * Add A
> * Call multi_perform() (which successfully connections and completes
> transfer)
> * Wait for A's completion
> * Remove A from the multi handle
> * Add B
> * Call multi_perform() (which successfully connections and completes
> transfer)
> Will two connections have been opened? And does this depend timing?

Here, chances are big that the second transfer will be done by re-using the
first connection. It requires that the server didn't close the first
connection either immediately or after a time-out before you issue the second

> 4. Which of the following two are true:
> a). An open connection belonging to a multi handle will only be re-used in
> a curl_multi_perform() call if the connection is not currently being used
> for another one of its transfers in progress; and, if, in fact, it is
> currently being used for a transfer, curl_multi_perform() will open a new
> parallel connection to the same server, so as not to stall the initiation
> of the new transfer.

That's true for HTTP/1 without pipelining and FTP etc, but not necessarily
true with HTTP/2.

> b). An open connection belonging to a multi_handle will be re-used in a
> curl_multi_perform() call even if the connection is currently being used;
> in which case, curl_multi_perform will stall the new transfer until the
> completion of the previous transfer, in order not to have two parallel
> connections to the same server open.

As I said above, libcurl's default action will not wait for anyhing but it
will create a new connection if there isn't one to reuse to fulfill the
transfer being asked for.

You can however make this wait-for-connection happen by limiting how many
connections libcurl should be allowed to use, either as in total number or as
in number of connections to the same host.

Received on 2017-01-26