Retry when server responds 503 too.

That's "Service Unavailable", it's
probably a temporary failure.
This commit is contained in:
King_DuckZ 2020-10-20 22:18:44 +02:00
parent 7d1011405a
commit dc0ba2984e
2 changed files with 17 additions and 8 deletions

View file

@ -36,7 +36,7 @@ std::size_t append_to_char_arary (std::string_view in, char* out, std::size_t of
ServerError::ServerError (const nap::HttpResponse& resp) :
std::runtime_error(
std::string("Server replied: ") + std::to_string(resp.code) + " (" +
std::string("Server replied: ") + std::to_string(resp.code) + " (HTTP " +
std::string(resp.http_ver) + ", \"" + std::string(resp.code_desc) +
"\")"
),

View file

@ -22,6 +22,7 @@
# include "lzma.hpp"
#endif
#include "base64.hpp"
#include "magic_enum.hpp"
#if defined(OROTOOL_WITH_RESTCCPP)
# include <restc-cpp/error.h>
#elif defined(OROTOOL_WITH_NAP)
@ -70,7 +71,7 @@ inline TimerOroApi<Op>::TimerOroApi (
template<oro::DBOperation Op>
inline void TimerOroApi<Op>::fetch_data (oro::SourceFormat store_mode) {
int status_code = 200;
bool schedule_retry = false;
try {
auto results = invoke_api_func<Op>(oro_api(), store_mode);
set_next_timer(results.header);
@ -96,8 +97,9 @@ inline void TimerOroApi<Op>::fetch_data (oro::SourceFormat store_mode) {
}
#if defined(OROTOOL_WITH_RESTCCPP)
catch (const restc_cpp::RequestFailedWithErrorException& err) {
status_code = err.http_response.status_code;
const int status_code = err.http_response.status_code;
if (429 == err.http_response.status_code) {
schedule_retry = true;
//server received too many requests, give up for now
}
else {
@ -106,10 +108,17 @@ inline void TimerOroApi<Op>::fetch_data (oro::SourceFormat store_mode) {
}
#elif defined(OROTOOL_WITH_NAP)
catch (const oro::ServerError& err) {
status_code = err.error_code();
if (429 == status_code) {
}
else {
const int status_code = err.error_code();
switch (status_code) {
case 503:
std::cerr << "Server returned 503 during request for " <<
magic_enum::enum_name(Op) << '\n';
schedule_retry = true;
break;
case 429:
schedule_retry = true;
break;
default:
throw;
}
}
@ -134,7 +143,7 @@ inline void TimerOroApi<Op>::fetch_data (oro::SourceFormat store_mode) {
#endif
m_retries_left = error_retries();
if (429 == status_code) {
if (schedule_retry) {
oro::Header head;
head.date.ts = std::chrono::time_point_cast<oro::timestamp_t::duration>(std::chrono::system_clock::now());
head.rate_limit = 1;