Skip to content

Commit 12d415b

Browse files
authored
Retry unsuccessful HTTP requests on 500 and 503 errors by default. (#623)
The error code 500 Internal Error indicates that a server is unable to handle the request at that time. The error code 503 Slow Down typically means that the request rate to the server bucket is too high. Hence, it makes sense to resolve such errors by retrying the request. Related-To: OLPEDGE-1398 Signed-off-by: Kirill Zhuchkov <[email protected]>
1 parent fb9ab1d commit 12d415b

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

olp-cpp-sdk-core/src/client/OlpClientSettings.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,23 @@
1919

2020
#include "olp/core/client/OlpClientSettings.h"
2121
#include "olp/core/client/HttpResponse.h"
22+
#include "olp/core/http/HttpStatusCode.h"
2223

2324
namespace olp {
2425
namespace client {
2526
unsigned int DefaultBackdownPolicy(unsigned int milliseconds) {
2627
return milliseconds;
2728
}
2829

29-
bool DefaultRetryCondition(const HttpResponse&) { return false; }
30+
bool DefaultRetryCondition(const HttpResponse& response) {
31+
switch (response.status) {
32+
case http::HttpStatusCode::INTERNAL_SERVER_ERROR:
33+
case http::HttpStatusCode::SERVICE_UNAVAILABLE:
34+
return true;
35+
default:
36+
return false;
37+
}
38+
}
3039

3140
} // namespace client
3241
} // namespace olp

olp-cpp-sdk-core/tests/client/OlpClientTest.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <chrono>
2424
#include <future>
2525
#include <string>
26+
#include <queue>
2627

2728
#include <olp/core/client/ApiError.h>
2829
#include <olp/core/client/OlpClient.h>
@@ -216,17 +217,29 @@ TEST_P(OlpClientTest, ZeroAttempts) {
216217
}
217218

218219
TEST_P(OlpClientTest, DefaultRetryCondition) {
219-
client_settings_.retry_settings.max_attempts = 6;
220+
client_settings_.retry_settings.max_attempts = 3;
220221

221222
auto network = std::make_shared<NetworkMock>();
222223
client_settings_.network_request_handler = network;
224+
225+
auto attempt_statuses = std::queue<int>{{500, 503, 200}};
223226
EXPECT_CALL(*network, Send(_, _, _, _, _))
224-
.WillOnce([&](olp::http::NetworkRequest request,
225-
olp::http::Network::Payload payload,
226-
olp::http::Network::Callback callback,
227-
olp::http::Network::HeaderCallback header_callback,
228-
olp::http::Network::DataCallback data_callback) {
229-
callback(olp::http::NetworkResponse().WithStatus(429));
227+
.Times(attempt_statuses.size())
228+
.WillRepeatedly([&attempt_statuses](
229+
olp::http::NetworkRequest request,
230+
olp::http::Network::Payload payload,
231+
olp::http::Network::Callback callback,
232+
olp::http::Network::HeaderCallback header_callback,
233+
olp::http::Network::DataCallback data_callback) {
234+
if (attempt_statuses.empty()) {
235+
ADD_FAILURE_AT(__FILE__, __LINE__) << "Unexpected retry attempt";
236+
return olp::http::SendOutcome(olp::http::ErrorCode::UNKNOWN_ERROR);
237+
}
238+
239+
auto status = attempt_statuses.front();
240+
attempt_statuses.pop();
241+
callback(olp::http::NetworkResponse().WithStatus(status));
242+
230243
return olp::http::SendOutcome(olp::http::RequestId(5));
231244
});
232245

@@ -237,7 +250,7 @@ TEST_P(OlpClientTest, DefaultRetryCondition) {
237250
std::multimap<std::string, std::string>(),
238251
std::multimap<std::string, std::string>(), nullptr, std::string());
239252

240-
ASSERT_EQ(429, response.status);
253+
ASSERT_EQ(200, response.status);
241254
}
242255

243256
TEST_P(OlpClientTest, RetryCondition) {

0 commit comments

Comments
 (0)