Skip to content

Commit fb7535a

Browse files
authored
Merge pull request #262 from dmikushin/repeat-http-request-on-fail
Repeat the http request on fail
2 parents efe43f5 + 6da263a commit fb7535a

2 files changed

Lines changed: 50 additions & 13 deletions

File tree

include/tgbot/net/HttpClient.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@ class TGBOT_API HttpClient {
2929
virtual std::string makeRequest(const Url& url, const std::vector<HttpReqArg>& args) const = 0;
3030

3131
std::int32_t _timeout = 25;
32+
33+
/**
34+
* @brief Get the maximum number of makeRequest() retries before giving up and throwing an exception.
35+
*/
36+
virtual int getRequestMaxRetries() const {
37+
return requestMaxRetries;
38+
}
39+
40+
/**
41+
* @brief Get the makeRequest() backoff duration between retries, in seconds.
42+
*/
43+
virtual int getRequestBackoff() const {
44+
return requestBackoff;
45+
}
46+
47+
private:
48+
int requestMaxRetries = 3;
49+
int requestBackoff = 1;
3250
};
3351

3452
}

src/Api.cpp

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include "tgbot/Api.h"
22

3+
#include <chrono>
4+
#include <thread>
5+
36
namespace TgBot {
47

58
Api::Api(std::string token, const HttpClient& httpClient, const std::string& url)
@@ -2504,20 +2507,36 @@ boost::property_tree::ptree Api::sendRequest(const std::string& method, const st
25042507
url += "/";
25052508
url += method;
25062509

2507-
std::string serverResponse = _httpClient.makeRequest(url, args);
2508-
if (!serverResponse.compare(0, 6, "<html>")) {
2509-
throw TgException("tgbot-cpp library have got html page instead of json response. Maybe you entered wrong bot token.");
2510-
}
2511-
2512-
boost::property_tree::ptree result = _tgTypeParser.parseJson(serverResponse);
2513-
try {
2514-
if (result.get<bool>("ok", false)) {
2515-
return result.get_child("result");
2516-
} else {
2517-
throw TgException(result.get("description", ""));
2510+
int requestRetryBackoff = _httpClient.getRequestBackoff();
2511+
int retries = 0;
2512+
while (1)
2513+
{
2514+
try {
2515+
std::string serverResponse = _httpClient.makeRequest(url, args);
2516+
if (!serverResponse.compare(0, 6, "<html>")) {
2517+
throw TgException("tgbot-cpp library have got html page instead of json response. Maybe you entered wrong bot token.");
2518+
}
2519+
2520+
boost::property_tree::ptree result = _tgTypeParser.parseJson(serverResponse);
2521+
try {
2522+
if (result.get<bool>("ok", false)) {
2523+
return result.get_child("result");
2524+
} else {
2525+
throw TgException(result.get("description", ""));
2526+
}
2527+
} catch (boost::property_tree::ptree_error& e) {
2528+
throw TgException("tgbot-cpp library can't parse json response. " + std::string(e.what()));
2529+
}
2530+
} catch (...) {
2531+
int max_retries = _httpClient.getRequestMaxRetries();
2532+
if ((max_retries >= 0) && (retries == max_retries)) {
2533+
throw;
2534+
} else {
2535+
std::this_thread::sleep_for(std::chrono::seconds(requestRetryBackoff));
2536+
retries++;
2537+
continue;
2538+
}
25182539
}
2519-
} catch (boost::property_tree::ptree_error& e) {
2520-
throw TgException("tgbot-cpp library can't parse json response. " + std::string(e.what()));
25212540
}
25222541
}
25232542
}

0 commit comments

Comments
 (0)