2

My program uses libcurl to talk to a server (always the same one) over HTTPS. The easiest way is to always create a new curl handle for a request. But by doing so I'm not taking advantage of curl connection pooling/reuse, if I understand it right.

Could it make sense to create a pool of curl handles, so that any given thread could borrow a handle from, perform a request, and then put it back into the pool?

If the answer is yes, then a related question: given a curl handle, how to find out if for a new request it will reuse the connection, or will have to create a new one (hence it doesn't make sense to put it back into the pool in the scenario described above)?

1 Answer 1

4

You can't figure out before if a connection is there to be re-used as there's no libcurl API for that.

The reason is quite simply that there are many factors involved in judging that decision that aren't figured out until you ask for the transfer to happen - and the fact that it can still fail and then libcurl will (transparently) create a new connection anyway.

If you really must use multi-threading then I'd recommend you splitting up the requests/threads on some logic you have or know so that the different threads handle different hosts or similar, to improve the chances of connection-reuse for each handle.

If you can consider another option, using a single thread that runs the libcurl multi interface could be a way to increase your chances of connection re-use.

2
  • Hmm, interesting (will probably accept this answer after thinking it over, for now just upvoting). I haven't considered using the multi interface.
    – rincewind
    Apr 9, 2013 at 5:08
  • Hmm, interesting (will probably accept this answer after thinking it over, for now just upvoting). I haven't considered using the multi interface. The thing though - I'm not in control of what thread my code is being called on, although I can spawn additional threads. Basically, I'm calling libcurl from within a FUSE (osxfuse, to be exact) callbacks. For now (after skimming multi interface tutorial) I don't see how it's better than using a pool of libcurl handles, but as I said - need ti think it over a bit more. Thanks!
    – rincewind
    Apr 9, 2013 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.