This commit is contained in:
2024-08-23 10:12:02 -05:00
parent 6dc10d2a11
commit 218326f812
6 changed files with 19 additions and 79 deletions

View File

@ -56,6 +56,11 @@ private:
private: private:
bool use_s3_path_style_{false}; bool use_s3_path_style_{false};
public:
[[nodiscard]] static auto create_curl() -> CURL *;
[[nodiscard]] static auto reset_curl(CURL *curl_handle) -> CURL *;
public: public:
[[nodiscard]] static auto [[nodiscard]] static auto
construct_url(CURL *curl, const std::string &relative_path, construct_url(CURL *curl, const std::string &relative_path,
@ -139,7 +144,7 @@ public:
response_code = 0; response_code = 0;
auto *curl = utils::create_curl(); auto *curl = create_curl();
if (not request.set_method(curl, stop_requested)) { if (not request.set_method(curl, stop_requested)) {
return false; return false;
} }

View File

@ -31,25 +31,10 @@ void calculate_allocation_size(bool directory, std::uint64_t file_size,
[[nodiscard]] auto convert_api_date(const std::string &date) -> std::uint64_t; [[nodiscard]] auto convert_api_date(const std::string &date) -> std::uint64_t;
[[nodiscard]] auto create_curl() -> CURL *;
[[nodiscard]] auto [[nodiscard]] auto
create_volume_label(const provider_type &prov) -> std::string; create_volume_label(const provider_type &prov) -> std::string;
[[nodiscard]] auto get_attributes_from_meta(const api_meta_map &meta) -> DWORD; [[nodiscard]] auto get_attributes_from_meta(const api_meta_map &meta) -> DWORD;
[[nodiscard]] auto reset_curl(CURL *curl_handle) -> CURL *;
[[nodiscard]] auto
retryable_action(const std::function<bool()> &action) -> bool;
void spin_wait_for_mutex(std::function<bool()> complete,
std::condition_variable &cond, std::mutex &mtx,
const std::string &text = "");
void spin_wait_for_mutex(bool &complete, std::condition_variable &cond,
std::mutex &mtx, const std::string &text = "");
} // namespace repertory::utils } // namespace repertory::utils
#endif // INCLUDE_UTILS_UTILS_HPP_ #endif // INCLUDE_UTILS_UTILS_HPP_

View File

@ -92,6 +92,16 @@ auto curl_comm::construct_url(CURL *curl, const std::string &relative_path,
relative_path, url); relative_path, url);
} }
auto curl_comm::create_curl() -> CURL * { return reset_curl(curl_easy_init()); }
auto curl_comm::reset_curl(CURL *curl_handle) -> CURL * {
curl_easy_reset(curl_handle);
#if defined(__APPLE__)
curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
#endif
return curl_handle;
}
auto curl_comm::create_host_config(const s3_config &cfg, auto curl_comm::create_host_config(const s3_config &cfg,
bool use_s3_path_style) -> host_config { bool use_s3_path_style) -> host_config {
host_config host_cfg{}; host_config host_cfg{};

View File

@ -38,7 +38,7 @@ void get_api_authentication_data(std::string &user, std::string &password,
{"config.json"}); {"config.json"});
json data; json data;
const auto success = utils::retryable_action([&]() -> bool { const auto success = utils::retry_action([&]() -> bool {
return utils::file::read_json_file(cfg_file_path, data); return utils::file::read_json_file(cfg_file_path, data);
}); });

View File

@ -68,16 +68,6 @@ auto convert_api_date(const std::string &date) -> std::uint64_t {
return nanos + (static_cast<std::uint64_t>(mktime(&tm1)) * NANOS_PER_SECOND); return nanos + (static_cast<std::uint64_t>(mktime(&tm1)) * NANOS_PER_SECOND);
} }
auto create_curl() -> CURL * {
static std::recursive_mutex mtx;
unique_recur_mutex_lock lock(mtx);
curl_global_init(CURL_GLOBAL_DEFAULT);
lock.unlock();
return reset_curl(curl_easy_init());
}
auto create_volume_label(const provider_type &prov) -> std::string { auto create_volume_label(const provider_type &prov) -> std::string {
return "repertory_" + app_config::get_provider_name(prov); return "repertory_" + app_config::get_provider_name(prov);
} }
@ -85,54 +75,4 @@ auto create_volume_label(const provider_type &prov) -> std::string {
auto get_attributes_from_meta(const api_meta_map &meta) -> DWORD { auto get_attributes_from_meta(const api_meta_map &meta) -> DWORD {
return static_cast<DWORD>(utils::string::to_uint32(meta.at(META_ATTRIBUTES))); return static_cast<DWORD>(utils::string::to_uint32(meta.at(META_ATTRIBUTES)));
} }
auto reset_curl(CURL *curl_handle) -> CURL * {
curl_easy_reset(curl_handle);
#if defined(__APPLE__)
curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
#endif
return curl_handle;
}
auto retryable_action(const std::function<bool()> &action) -> bool {
static constexpr const auto retry_count = 20U;
auto succeeded = false;
for (std::uint8_t i = 0U; not(succeeded = action()) && (i < retry_count);
i++) {
std::this_thread::sleep_for(100ms);
}
return succeeded;
}
void spin_wait_for_mutex(std::function<bool()> complete,
std::condition_variable &cond, std::mutex &mtx,
const std::string &text) {
while (not complete()) {
unique_mutex_lock lock(mtx);
if (not complete()) {
if (not text.empty()) {
/* event_system::instance().raise<DebugLog>(__FUNCTION__,
* "spin_wait_for_mutex", text); */
}
cond.wait_for(lock, 1s);
}
lock.unlock();
}
}
void spin_wait_for_mutex(bool &complete, std::condition_variable &cond,
std::mutex &mtx, const std::string &text) {
while (not complete) {
unique_mutex_lock lock(mtx);
if (not complete) {
if (not text.empty()) {
/* event_system::instance().raise<DebugLog>(__FUNCTION__,
* "spin_wait_for_mutex", text); */
}
cond.wait_for(lock, 1s);
}
lock.unlock();
}
}
} // namespace repertory::utils } // namespace repertory::utils

View File

@ -738,7 +738,7 @@ TEST(file_manager, can_evict_file) {
fm.close(handle); fm.close(handle);
capture.wait_for_empty(); capture.wait_for_empty();
EXPECT_TRUE(utils::retryable_action( EXPECT_TRUE(utils::retry_action(
[&fm]() -> bool { return not fm.is_processing("/test_evict.txt"); })); [&fm]() -> bool { return not fm.is_processing("/test_evict.txt"); }));
EXPECT_CALL(mp, get_item_meta(_, META_SOURCE, _)) EXPECT_CALL(mp, get_item_meta(_, META_SOURCE, _))
@ -1009,7 +1009,7 @@ TEST(file_manager, evict_file_fails_if_file_is_uploading) {
fm.close(handle); fm.close(handle);
EXPECT_TRUE(utils::retryable_action( EXPECT_TRUE(utils::retry_action(
[&fm]() -> bool { return fm.is_processing("/test_evict.txt"); })); [&fm]() -> bool { return fm.is_processing("/test_evict.txt"); }));
EXPECT_FALSE(fm.evict_file("/test_evict.txt")); EXPECT_FALSE(fm.evict_file("/test_evict.txt"));
} }