Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(HTTP): Retry failed HTTP requests with exponential backoff #1098

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 39 additions & 10 deletions packager/file/http_file.cc
Expand Up @@ -6,6 +6,8 @@

#include <packager/file/http_file.h>

#include <thread>

#include <absl/flags/declare.h>
#include <absl/flags/flag.h>
#include <absl/log/check.h>
Expand Down Expand Up @@ -55,6 +57,10 @@ namespace {
constexpr const char* kBinaryContentType = "application/octet-stream";
constexpr const int kMinLogLevelForCurlDebugFunction = 2;

// The number of times to retry requesting the server before we give up.
constexpr const int kNumRequestErrorRetries = 5;
constexpr const int kFirstRetryDelayMilliseconds = 1000;

size_t CurlWriteCallback(char* buffer, size_t size, size_t nmemb, void* user) {
IoCache* cache = reinterpret_cast<IoCache*>(user);
size_t length = size * nmemb;
Expand Down Expand Up @@ -135,13 +141,9 @@ int CurlDebugCallback(CURL* /* handle */,

class LibCurlInitializer {
public:
LibCurlInitializer() {
curl_global_init(CURL_GLOBAL_DEFAULT);
}
LibCurlInitializer() { curl_global_init(CURL_GLOBAL_DEFAULT); }

~LibCurlInitializer() {
curl_global_cleanup();
}
~LibCurlInitializer() { curl_global_cleanup(); }

LibCurlInitializer(const LibCurlInitializer&) = delete;
LibCurlInitializer& operator=(const LibCurlInitializer&) = delete;
Expand Down Expand Up @@ -224,9 +226,6 @@ bool HttpFile::Open() {
}
// TODO: Try to connect initially so we can return connection error here.

// TODO: Implement retrying with exponential backoff, see
// "widevine_key_source.cc"

ThreadPool::instance.PostTask(std::bind(&HttpFile::ThreadMain, this));

return true;
Expand Down Expand Up @@ -357,8 +356,38 @@ void HttpFile::SetupRequest() {

void HttpFile::ThreadMain() {
SetupRequest();
CURLcode res;
int64_t sleep_duration = kFirstRetryDelayMilliseconds;

// Try sending the request multiple times if it fails.
for (int i = 0; i < kNumRequestErrorRetries; ++i) {
res = curl_easy_perform(curl_.get());

if (res == CURLE_HTTP_RETURNED_ERROR) {
long res_code = 0;
curl_easy_getinfo(curl_.get(), CURLINFO_RESPONSE_CODE, &res_code);
if (res_code == 408 || res_code == 429 || res_code >= 500) {
// FIXME: Should we clear download_cache_ and upload_cache_ here??
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you'll need to ensure that the inside of the loop has no side-effects. I haven't analyzed to see what the caches do here, but it's worth looking into more closely.

You may want to write test cases that verify the correct behavior for POST/PUT with data. In the cmake branch, you'll find HTTP file tests are all enabled and working. (In main, they are all disabled.) You should be able to verify retry behavior with httpbin, and see that the final failed retry still has a response that reflects the data from the POST/PUT.

I recommend you try the cmake branch in a separate clone, without using gclient. We have yet to update the docs for that, but this is what you'll want to try:

# Clone
git clone ...
# Use cmake branch
git checkout -b cmake upstream/cmake
# Get submodules
git submodule init && git submodule update
# Gen build files
cmake -S . -B build/ -DCMAKE_BUILD_TYPE=Debug
# Build
cmake --build build/ --config Debug --parallel
# Test
(cd build && ctest -C Debug -V)

If you have time, you could make a PR against the cmake branch, which is where most development is happening right now as we rewrite the build system and dependencies.

// Retry.
} else {
// Exponential backoff won't help for this response code, Stop retrying.
break;
}
} else if (res == CURLE_OPERATION_TIMEDOUT) {
// Retry.
} else {
// Either the request was successful or exponential backoff won't help.
// Stop retrying.
break;
}

// Exponential backoff.
if (i != kNumRequestErrorRetries - 1) {
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_duration));
sleep_duration *= 2;
}
}

CURLcode res = curl_easy_perform(curl_.get());
if (res != CURLE_OK) {
std::string error_message = curl_easy_strerror(res);
if (res == CURLE_HTTP_RETURNED_ERROR) {
Expand Down