cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: How to cleanly close the curl socket in case of multi interface?

From: nikhil ap <niks3089_at_gmail.com>
Date: Fri, 19 Sep 2014 18:49:30 +0530

"libcurl assumes persistent connections and will make its best to keep
connections alive and then possibly re-use them. You can use
CURLOPT_FORBID_REUSE to explicitly mark transfers to forbid subsequent
requests to re-use the connection that transfer uses."

CURLOPT_FORBID_REUSE closes the connection immediately and is suited if I
have to make 1 request / connection. If I have to make 5 requests/
connection using the same handle, then I cannot use this option because for
the 2nd request is going to create a new connection which is not what I
want.

"Related, but probably not what you want here, you can use
CURLOPT_FRESH_CONNECT on an easy handle to force it to not re-use an
existing connection."

I did use this option to set it when the first time the handle is
initialized with my connection structure. This ensures that all the handles
will create a new connection and I unset this option the second time the
connection had to send another request but again this means another handle
can use the same TCP to send its second request.

To overcome this, I thought I could set CURLOPT_MAXCONNECTS to 1 to force
the connection cache to be 1 but since I am using multi interface, I
used CURLMOPT_MAXCONNECTS
and set its value to 1.

This however closed the connection when there were more than one open
connection and according to the document,
"When the cache is full, curl closes the oldest one in the cache to prevent
the number of open connections from increasing" this is a right behavior.
Is there a way to avoid this behavior? Having easy handle re-using
different TCP connection messes with my stats.

Bottom line:

I would like to use and re-use each easy handle for just one connection in
multi-interface enviornment. Is this possible?

On Fri, Sep 19, 2014 at 5:04 PM, Daniel Stenberg <daniel_at_haxx.se> wrote:

> On Fri, 19 Sep 2014, nikhil ap wrote:
>
> I do like to keep the connection persistent in some cases and close in
>> some other.
>>
>
> libcurl assumes persistent connections and will make its best to keep
> connections alive and then possibly re-use them. You can use
> CURLOPT_FORBID_REUSE to explicitly mark transfers to forbid subsequent
> requests to re-use the connection that transfer uses.
>
> Related, but probably not what you want here, you can use
> CURLOPT_FRESH_CONNECT on an easy handle to force it to not re-use an
> existing connection.
>
> --
>
> / daniel.haxx.se
> -------------------------------------------------------------------
> List admin: http://cool.haxx.se/list/listinfo/curl-library
> Etiquette: http://curl.haxx.se/mail/etiquette.html

-- 
niks

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