Skip to content
This repository has been archived by the owner on Jan 15, 2025. It is now read-only.

Commit

Permalink
(PE-37481) Add retry with exp backoff on err 32
Browse files Browse the repository at this point in the history
  • Loading branch information
tlehman committed Feb 26, 2024
1 parent 13a1f22 commit 66a9117
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- [Sample Library CMakeLists.txt file](#sample-library-cmakeliststxt-file)
- [Vendoring Other Libraries](#vendoring-other-libraries)
- [How To Release](#how-to-release)
- [How To Run Tests](#how-to-run-tests)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

Expand Down Expand Up @@ -414,3 +415,16 @@ the `nowide` and `catch` CMake files are both solid examples.
[1]: https://github.com/philsquared/Catch
[2]: https://github.com/miloyip/rapidjson
[3]: json_container/README.md

## How to run tests

To run the leatherman tests, you need `cmake` and a C++ compiler.

```
# at the root of the leatherman/ directory
mkdir build
cd build
cmake ..
make leatherman_test
bin/leatherman_test
```
27 changes: 27 additions & 0 deletions curl/src/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <boost/algorithm/string.hpp>
#include <boost/nowide/iostream.hpp>
#include <boost/nowide/fstream.hpp>
#include <boost/chrono.hpp>
#include <boost/thread.hpp>
#include <sstream>

// Mark string for translation (alias for leatherman::locale::format)
Expand Down Expand Up @@ -128,11 +130,36 @@ namespace leatherman { namespace curl {
return _fp;
}

bool error_file_in_use(const boost::system::error_code& ec) {
bool same = (ec == boost::system::error_code(32, boost::system::system_category()));
LOG_DEBUG("error_file_in_use({1}), same = {2}", ec, same);
return same;
}

// Handle special case where rename fails on windows due to error code 32
void write_retry_with_exp_backoff(boost::filesystem::path const& _temp_path, boost::filesystem::path const& _file_path) {
boost::system::error_code ec;
int max_retries = 5;
int wait_millis = 100;
do {
LOG_DEBUG("Failed rename with error 32, retrying up to {1} more times", max_retries);
boost::this_thread::sleep_for(boost::chrono::milliseconds(wait_millis));
fs::rename(_temp_path, _file_path, ec);
wait_millis *= 2;
max_retries -= 1;
} while (max_retries > 0 && error_file_in_use(ec));
}


void download_temp_file::write() {
LOG_DEBUG("Download completed, now writing result to file {1}", _file_path);
close_fp();
boost::system::error_code ec;
fs::rename(_temp_path, _file_path, ec);
if(error_file_in_use(ec)) {
write_retry_with_exp_backoff(_temp_path, _file_path);
}

if (ec) {
LOG_WARNING("Failed to write the results of the temporary file to the actual file {1}", _file_path);
throw http_file_operation_exception(_req, _file_path, make_file_err_msg(_("failed to move over the temporary file's downloaded contents")));
Expand Down

0 comments on commit 66a9117

Please sign in to comment.