Skip to content

Commit 97b0eb4

Browse files
committed
Parameterizing the number of retries and backoff duration for HttpClient.makeRequest
1 parent 0519eba commit 97b0eb4

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

include/tgbot/net/HttpClient.h

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

3030
std::int32_t _timeout = 25;
31+
32+
virtual int getRequestMaxRetries() const {
33+
return requestMaxRetries;
34+
}
35+
36+
virtual int getRequestBackoff() const {
37+
return requestBackoff;
38+
}
39+
40+
private:
41+
int requestMaxRetries = 3;
42+
int requestBackoff = 1;
3143
};
3244

3345
}

src/Api.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2499,7 +2499,9 @@ boost::property_tree::ptree Api::sendRequest(const std::string& method, const st
24992499
url += "/";
25002500
url += method;
25012501

2502-
while(1)
2502+
int requestRetryBackoff = _httpClient.getRequestBackoff();
2503+
int retries = 0;
2504+
while (1)
25032505
{
25042506
std::string serverResponse = _httpClient.makeRequest(url, args);
25052507
if (!serverResponse.compare(0, 6, "<html>")) {
@@ -2511,9 +2513,14 @@ boost::property_tree::ptree Api::sendRequest(const std::string& method, const st
25112513
if (result.get<bool>("ok", false)) {
25122514
return result.get_child("result");
25132515
} else {
2514-
std::this_thread::sleep_for(std::chrono::seconds(1));
2515-
continue;
2516-
//throw TgException(result.get("description", ""));
2516+
if (retries == _httpClient.getRequestMaxRetries()) {
2517+
throw TgException(result.get("description", ""));
2518+
}
2519+
else {
2520+
std::this_thread::sleep_for(std::chrono::seconds(requestRetryBackoff));
2521+
retries++;
2522+
continue;
2523+
}
25172524
}
25182525
} catch (boost::property_tree::ptree_error& e) {
25192526
throw TgException("tgbot-cpp library can't parse json response. " + std::string(e.what()));

0 commit comments

Comments
 (0)