Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using libcurl multi interface for consecutive requests for same "easy" handle

My development organization has its own wrapper implementation for threads and select(). The application needs to be enhanced to perform HTTPS requests, and I've decided to use libcurl. After some research, I see that the curl_easy_perform is a blocking call, so I've decided to use the curl_multi_perform approach for non-blocking calls to allow other work in the threads.

The HTTPS request will need to be performed periodically to the same URL. I know I can keep the same curl_easy handle and give that to the curl_multi handle. I would perform the curl_multi_perform to get the results, but I would later need to use curl_multi_perform to resend the request, say in 5 minutes. So, this would be a consecutive request using the same easy handle. However, I'm not sure how the curl_easy interface tells the multi interface as to when to resend the request after I have received results from the first request. How do I accomplish this?

(Maybe drop the easy handle from the multi handle, and re-add it to the multi handle when a request is needed again?)

I presume that whatever technique is used, that the outgoing request will be using the same outgoing port.

like image 230
Jenner Avatar asked Nov 07 '22 21:11

Jenner


1 Answers

(Maybe drop the easy handle from the multi handle, and re-add it to the multi handle when a request is needed again?)

Correct. From the libcurl documentation:

When a single transfer is completed, the easy handle is still left added to the multi stack. You need to first remove the easy handle with curl_multi_remove_handle and then close it with curl_easy_cleanup, or possibly set new options to it and add it again with curl_multi_add_handle to start another transfer.

.

I presume that whatever technique is used, that the outgoing request will be using the same outgoing port

This is not guaranteed. libcurl will attempt to re-use existing connections that are associated with the easy handle, but if the previous connection has already died then a new connection with an unpredictable local port will be established.

like image 74
Andrew Lambert Avatar answered Nov 14 '22 22:11

Andrew Lambert