Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

libcurl performance

Posted on 2011-09-21
3
Medium Priority
?
563 Views
Last Modified: 2012-05-12
Hi

I am using libcurl in C++  to call a webservice api and get back a json/xml response.

PLease see the attached code
I want to make sure i can do the following things

1) if the response takes too long and i am waiting on it, i want to time out
2) show i worry about anyothet failure cases, for example, if the called url does not exist?

any other problem areas i should be aware of?

size_t writer( void *ptr, size_t size, size_t nmemb, void *stream)
{
    buffer.append((char*)ptr, size*nmemb);
    return size*nmemb;
}

std::string mdi_body::DownloadWS(std::string URL, ext_api mode)
{
    CURL *curl;
    CURLcode res;
    std::ostringstream oss;
    curl = curl_easy_init();
    stringstream data;

    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());

        if (mode == CORE_IMAP) {
            char *data="{ \"groups\" : [\"F\"], \"content\" : true }";
            curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
        }

        curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, writer);

        res = curl_easy_perform(curl);
        if(CURLE_OK == res) {
            char *ct;
            res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
            if((CURLE_OK == res) && ct)
                return buffer;
            }
      }

}

Open in new window

0
Comment
Question by:Vlearns
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

by:Vlearns
ID: 36577023
added these

 46         /* Performance check: DNS timeouts*/
 47         curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, 50);
 48
 49         /* Performance check: Response timeouts */
 50         curl_easy_setopt(curl,CURLOPT_TIMEOUT_MS, 50);

what else should i worry about?
0
 

Author Comment

by:Vlearns
ID: 36577028
i am using

ports/curl-7.16.3_1
0
 
LVL 7

Accepted Solution

by:
tampnic earned 2000 total points
ID: 36579789
One thing I notice is after calls to the library you are only checking for success - you might want to write a function similar to below to log the performance of your code - call it whenever result of a call to the library is not CURLE_OK.

You may want to retry on timeouts, up to a maximum number of retries. Sometimes if a network request is timing out its better to retry from scratch again, rather than setting a longer timeout. Don't ask me why! Its just something I tried in the past that got me out of a hole :-) When making 3 network data requests at 5s intervals with short timeout the third one got through, but making one request with a timeout interval of 60s timed out. That wasn't using libcurl but the general principle might still apply (or not ... if you have timeout issues its a possible workaround).

N.B. I knocked the code up in two minutes - untested - you might want to check out the pages at http://curl.haxx.se/libcurl/c/ to make sure I'm setting up the CURLOPT_ERRORBUFFER option correctly.

Cheers,
  Chris
void OnCURLError(CURL *handle, CURLcode errornum)
{
  char sError[CURL_ERROR_SIZE];
  curl_easy_setopt(handle,CURLOPT_ERRORBUFFER,sError);
  cerr << curl_easy_strerror(errornum);
}

Open in new window

0

Featured Post

Simplify Your Workload with One Tool

How do you combat today’s intelligent hacker while managing multiple domains and platforms? By simplifying your workload with one tool. With Lunarpages hosting through Plesk Onyx, you can:

Automate SSL generation and installation with two clicks
Experience total server control

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question