diff options
-rw-r--r-- | include/tgbot/net/HttpClient.h | 12 | ||||
-rw-r--r-- | src/Api.cpp | 15 |
2 files changed, 23 insertions, 4 deletions
diff --git a/include/tgbot/net/HttpClient.h b/include/tgbot/net/HttpClient.h index 024c8ad..90b93dd 100644 --- a/include/tgbot/net/HttpClient.h +++ b/include/tgbot/net/HttpClient.h @@ -28,6 +28,18 @@ public: virtual std::string makeRequest(const Url& url, const std::vector<HttpReqArg>& args) const = 0; std::int32_t _timeout = 25; + + virtual int getRequestMaxRetries() const { + return requestMaxRetries; + } + + virtual int getRequestBackoff() const { + return requestBackoff; + } + +private: + int requestMaxRetries = 3; + int requestBackoff = 1; }; } diff --git a/src/Api.cpp b/src/Api.cpp index dcb2b36..b6dba9a 100644 --- a/src/Api.cpp +++ b/src/Api.cpp @@ -2499,7 +2499,9 @@ boost::property_tree::ptree Api::sendRequest(const std::string& method, const st url += "/"; url += method; - while(1) + int requestRetryBackoff = _httpClient.getRequestBackoff(); + int retries = 0; + while (1) { std::string serverResponse = _httpClient.makeRequest(url, args); if (!serverResponse.compare(0, 6, "<html>")) { @@ -2511,9 +2513,14 @@ boost::property_tree::ptree Api::sendRequest(const std::string& method, const st if (result.get<bool>("ok", false)) { return result.get_child("result"); } else { - std::this_thread::sleep_for(std::chrono::seconds(1)); - continue; - //throw TgException(result.get("description", "")); + if (retries == _httpClient.getRequestMaxRetries()) { + throw TgException(result.get("description", "")); + } + else { + std::this_thread::sleep_for(std::chrono::seconds(requestRetryBackoff)); + retries++; + continue; + } } } catch (boost::property_tree::ptree_error& e) { throw TgException("tgbot-cpp library can't parse json response. " + std::string(e.what())); |