Skip to content

Commit 5fbd838

Browse files
authored
Automatically retry a few times on HTTP errors (#613)
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent e442e5f commit 5fbd838

8 files changed

Lines changed: 24 additions & 172 deletions

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,4 @@ jobs:
149149
- run: mkdir "$PWD/build"
150150
- run: TMPDIR="$PWD/build" ./test/lint/fail_lint.sh jsonschema
151151
- run: TMPDIR="$PWD/build" ./test/bundle/pass_resolve_single.sh jsonschema
152-
- run: TMPDIR="$PWD/build" ./test/ci/pass_validate_http_verbose.sh jsonschema
152+
- run: TMPDIR="$PWD/build" ./test/ci/pass_validate_http.sh jsonschema

src/resolver.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "logger.h"
2828

2929
#include <cassert> // assert
30+
#include <chrono> // std::chrono::seconds
31+
#include <cstdint> // std::uint8_t
3032
#include <filesystem> // std::filesystem
3133
#include <functional> // std::function, std::ref
3234
#include <iostream> // std::cerr
@@ -36,10 +38,13 @@
3638
#include <stdexcept> // std::runtime_error
3739
#include <string> // std::string
3840
#include <string_view> // std::string_view
41+
#include <thread> // std::this_thread::sleep_for
3942
#include <utility> // std::pair, std::piecewise_construct, std::forward_as_tuple
4043

4144
namespace sourcemeta::jsonschema {
4245

46+
static constexpr std::uint8_t HTTP_MAXIMUM_RETRIES{3};
47+
4348
static inline auto fallback_resolver(const sourcemeta::core::Options &options,
4449
std::string_view identifier)
4550
-> std::optional<sourcemeta::core::JSON> {
@@ -56,9 +61,24 @@ static inline auto fallback_resolver(const sourcemeta::core::Options &options,
5661
return std::nullopt;
5762
}
5863

59-
LOG_VERBOSE(options) << "Resolving over HTTP: " << identifier << "\n";
60-
const cpr::Response response{
61-
cpr::Get(cpr::Url{identifier}, cpr::Redirect{true})};
64+
cpr::Response response;
65+
for (std::uint8_t attempt{1}; attempt <= HTTP_MAXIMUM_RETRIES; ++attempt) {
66+
LOG_VERBOSE(options) << "Resolving over HTTP (attempt "
67+
<< static_cast<int>(attempt) << "/"
68+
<< static_cast<int>(HTTP_MAXIMUM_RETRIES)
69+
<< "): " << identifier << "\n";
70+
response = cpr::Get(cpr::Url{identifier}, cpr::Redirect{true});
71+
72+
if (response.status_code == 200) {
73+
break;
74+
}
75+
76+
if (attempt < HTTP_MAXIMUM_RETRIES) {
77+
LOG_VERBOSE(options) << "Request failed with HTTP "
78+
<< response.status_code << ", retrying...\n";
79+
std::this_thread::sleep_for(std::chrono::seconds(1));
80+
}
81+
}
6282

6383
if (response.status_code != 200) {
6484
std::ostringstream error;

test/CMakeLists.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -468,15 +468,10 @@ add_jsonschema_test_unix(decode/fail_no_output)
468468
# CI specific tests
469469
add_jsonschema_test_unix_ci(pass_bundle_http)
470470
add_jsonschema_test_unix_ci(fail_bundle_http_non_200)
471-
add_jsonschema_test_unix_ci(fail_bundle_http_non_200_verbose)
472471
add_jsonschema_test_unix_ci(fail_bundle_http_non_schema)
473-
add_jsonschema_test_unix_ci(fail_bundle_http_non_schema_verbose)
474472
add_jsonschema_test_unix_ci(fail_validate_http_non_200)
475-
add_jsonschema_test_unix_ci(fail_validate_http_non_200_verbose)
476473
add_jsonschema_test_unix_ci(fail_validate_http_non_schema)
477-
add_jsonschema_test_unix_ci(fail_validate_http_non_schema_verbose)
478474
add_jsonschema_test_unix_ci(pass_validate_http)
479-
add_jsonschema_test_unix_ci(pass_validate_http_verbose)
480475
add_jsonschema_test_unix_ci(precommit_lint)
481476

482477
# TODO: Make this test pass on Linux. It hangs for some reason

test/ci/fail_bundle_http_non_200_verbose.sh

Lines changed: 0 additions & 30 deletions
This file was deleted.

test/ci/fail_bundle_http_non_schema_verbose.sh

Lines changed: 0 additions & 31 deletions
This file was deleted.

test/ci/fail_validate_http_non_200_verbose.sh

Lines changed: 0 additions & 34 deletions
This file was deleted.

test/ci/fail_validate_http_non_schema_verbose.sh

Lines changed: 0 additions & 35 deletions
This file was deleted.

test/ci/pass_validate_http_verbose.sh

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)