diff --git a/repertory/librepertory/include/comm/curl/curl_comm.hpp b/repertory/librepertory/include/comm/curl/curl_comm.hpp index 0ada9500..28520b26 100644 --- a/repertory/librepertory/include/comm/curl/curl_comm.hpp +++ b/repertory/librepertory/include/comm/curl/curl_comm.hpp @@ -56,6 +56,11 @@ private: private: bool use_s3_path_style_{false}; +public: + [[nodiscard]] static auto create_curl() -> CURL *; + + [[nodiscard]] static auto reset_curl(CURL *curl_handle) -> CURL *; + public: [[nodiscard]] static auto construct_url(CURL *curl, const std::string &relative_path, @@ -139,7 +144,7 @@ public: response_code = 0; - auto *curl = utils::create_curl(); + auto *curl = create_curl(); if (not request.set_method(curl, stop_requested)) { return false; } diff --git a/repertory/librepertory/include/utils/utils.hpp b/repertory/librepertory/include/utils/utils.hpp index 5d4ac1a2..643528b4 100644 --- a/repertory/librepertory/include/utils/utils.hpp +++ b/repertory/librepertory/include/utils/utils.hpp @@ -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 create_curl() -> CURL *; - [[nodiscard]] auto create_volume_label(const provider_type &prov) -> std::string; [[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 &action) -> bool; - -void spin_wait_for_mutex(std::function 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 #endif // INCLUDE_UTILS_UTILS_HPP_ diff --git a/repertory/librepertory/src/comm/curl/curl_comm.cpp b/repertory/librepertory/src/comm/curl/curl_comm.cpp index 964580f5..13ca92ea 100644 --- a/repertory/librepertory/src/comm/curl/curl_comm.cpp +++ b/repertory/librepertory/src/comm/curl/curl_comm.cpp @@ -92,6 +92,16 @@ auto curl_comm::construct_url(CURL *curl, const std::string &relative_path, 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, bool use_s3_path_style) -> host_config { host_config host_cfg{}; diff --git a/repertory/librepertory/src/utils/cli_utils.cpp b/repertory/librepertory/src/utils/cli_utils.cpp index 85418639..9d9cf1a0 100644 --- a/repertory/librepertory/src/utils/cli_utils.cpp +++ b/repertory/librepertory/src/utils/cli_utils.cpp @@ -38,7 +38,7 @@ void get_api_authentication_data(std::string &user, std::string &password, {"config.json"}); 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); }); diff --git a/repertory/librepertory/src/utils/utils.cpp b/repertory/librepertory/src/utils/utils.cpp index 1aa9cd9e..a71d5e72 100644 --- a/repertory/librepertory/src/utils/utils.cpp +++ b/repertory/librepertory/src/utils/utils.cpp @@ -68,16 +68,6 @@ auto convert_api_date(const std::string &date) -> std::uint64_t { return nanos + (static_cast(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 { 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 { return static_cast(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 &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 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(__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(__FUNCTION__, - * "spin_wait_for_mutex", text); */ - } - cond.wait_for(lock, 1s); - } - lock.unlock(); - } -} } // namespace repertory::utils diff --git a/repertory/repertory_test/src/file_manager_test.cpp b/repertory/repertory_test/src/file_manager_test.cpp index 5e316957..b75a6a35 100644 --- a/repertory/repertory_test/src/file_manager_test.cpp +++ b/repertory/repertory_test/src/file_manager_test.cpp @@ -738,7 +738,7 @@ TEST(file_manager, can_evict_file) { fm.close(handle); capture.wait_for_empty(); - EXPECT_TRUE(utils::retryable_action( + EXPECT_TRUE(utils::retry_action( [&fm]() -> bool { return not fm.is_processing("/test_evict.txt"); })); 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); - EXPECT_TRUE(utils::retryable_action( + EXPECT_TRUE(utils::retry_action( [&fm]() -> bool { return fm.is_processing("/test_evict.txt"); })); EXPECT_FALSE(fm.evict_file("/test_evict.txt")); }