From 9bd416e3b0bc71f9c89bfd7076adeb531847695e Mon Sep 17 00:00:00 2001 From: Sude Date: Sat, 22 Dec 2018 11:35:17 +0200 Subject: [PATCH] Add support for retry and wait to Galaxy API and downloader API --- src/api.cpp | 16 +++++++++++++--- src/galaxyapi.cpp | 16 +++++++++++++--- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/api.cpp b/src/api.cpp index 2dc2e00..87b8ccb 100644 --- a/src/api.cpp +++ b/src/api.cpp @@ -242,9 +242,19 @@ std::string API::getResponse(const std::string& url) curl_easy_setopt(curlhandle, CURLOPT_NOPROGRESS, 1); curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, writeMemoryCallback); curl_easy_setopt(curlhandle, CURLOPT_WRITEDATA, &memory); - CURLcode result = curl_easy_perform(curlhandle); - std::string response = memory.str(); - memory.str(std::string()); + + int retries = 0; + CURLcode result; + std::string response; + do + { + if (Globals::globalConfig.iWait > 0) + usleep(Globals::globalConfig.iWait); // Delay the request by specified time + result = curl_easy_perform(curlhandle); + response = memory.str(); + memory.str(std::string()); + } + while ((result != CURLE_OK) && response.empty() && (retries++ < Globals::globalConfig.iRetries)); if (result == CURLE_HTTP_RETURNED_ERROR) { diff --git a/src/galaxyapi.cpp b/src/galaxyapi.cpp index 21df601..695236e 100644 --- a/src/galaxyapi.cpp +++ b/src/galaxyapi.cpp @@ -117,9 +117,19 @@ std::string galaxyAPI::getResponse(const std::string& url, const bool& zlib_deco curl_easy_setopt(curlhandle, CURLOPT_NOPROGRESS, 1); curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, galaxyAPI::writeMemoryCallback); curl_easy_setopt(curlhandle, CURLOPT_WRITEDATA, &memory); - curl_easy_perform(curlhandle); - std::string response = memory.str(); - memory.str(std::string()); + + int retries = 0; + CURLcode result; + std::string response; + do + { + if (Globals::globalConfig.iWait > 0) + usleep(Globals::globalConfig.iWait); // Delay the request by specified time + result = curl_easy_perform(curlhandle); + response = memory.str(); + memory.str(std::string()); + } + while ((result != CURLE_OK) && response.empty() && (retries++ < Globals::globalConfig.iRetries)); curl_easy_setopt(curlhandle, CURLOPT_HTTPHEADER, NULL); curl_slist_free_all(header);