From a871ad5dbcc5b5d850ca14779aed9de5e8f2540a Mon Sep 17 00:00:00 2001 From: Timothy Stack Date: Mon, 18 Jan 2021 15:04:24 -0800 Subject: [PATCH] [cleanup] some curl-related fixes --- src/command_executor.cc | 10 ++++----- src/curl_looper.cc | 46 ++++++++++++++++++----------------------- src/curl_looper.hh | 12 +++++------ src/lnav.cc | 4 ++-- src/lnav_commands.cc | 14 ++++++------- src/view_helpers.cc | 1 - 6 files changed, 40 insertions(+), 47 deletions(-) diff --git a/src/command_executor.cc b/src/command_executor.cc index e2d078be..acaa2fc1 100644 --- a/src/command_executor.cc +++ b/src/command_executor.cc @@ -679,13 +679,13 @@ void execute_init_commands(exec_context &ec, vector, if (!lnav_data.ld_pt_search.empty()) { #ifdef HAVE_LIBCURL - unique_ptr pt(new papertrail_proc( - lnav_data.ld_pt_search.substr(3), - lnav_data.ld_pt_min_time, - lnav_data.ld_pt_max_time)); + auto pt = make_shared( + lnav_data.ld_pt_search.substr(3), + lnav_data.ld_pt_min_time, + lnav_data.ld_pt_max_time); lnav_data.ld_active_files.fc_file_names[lnav_data.ld_pt_search] .with_fd(pt->copy_fd()); - lnav_data.ld_curl_looper.add_request(pt.release()); + lnav_data.ld_curl_looper.add_request(pt); #endif } diff --git a/src/curl_looper.cc b/src/curl_looper.cc index b828b82d..ff6ac18a 100644 --- a/src/curl_looper.cc +++ b/src/curl_looper.cc @@ -41,14 +41,14 @@ using namespace std; struct curl_request_eq { - curl_request_eq(const std::string &name) : cre_name(name) { + explicit curl_request_eq(const std::string &name) : cre_name(name) { }; - bool operator()(const curl_request *cr) const { + bool operator()(const std::shared_ptr& cr) const { return this->cre_name == cr->get_name(); }; - bool operator()(const pair &pair) const { + bool operator()(const pair> &pair) const { return this->cre_name == pair.second->get_name(); }; @@ -147,10 +147,10 @@ void curl_looper::requeue_requests(mstime_t up_to_time) { while (!this->cl_poll_queue.empty() && this->cl_poll_queue.front().first <= up_to_time) { - curl_request *cr = this->cl_poll_queue.front().second; + auto cr = this->cl_poll_queue.front().second; log_debug("%s:polling request is ready again -- %p", - cr->get_name().c_str(), cr); + cr->get_name().c_str(), cr.get()); this->cl_handle_to_request[cr->get_handle()] = cr; curl_multi_add_handle(this->cl_curl_multi, cr->get_handle()); this->cl_poll_queue.erase(this->cl_poll_queue.begin()); @@ -161,11 +161,11 @@ void curl_looper::check_for_new_requests() { std::unique_lock mg(this->cl_mutex); while (!this->cl_new_requests.empty()) { - curl_request *cr = this->cl_new_requests.back(); + auto cr = this->cl_new_requests.back(); log_info("%s:new curl request %p", cr->get_name().c_str(), - cr); + cr.get()); this->cl_handle_to_request[cr->get_handle()] = cr; curl_multi_add_handle(this->cl_curl_multi, cr->get_handle()); this->cl_new_requests.pop_back(); @@ -179,23 +179,20 @@ void curl_looper::check_for_new_requests() { log_info("attempting to close request -- %s", name.c_str()); if (all_iter != this->cl_all_requests.end()) { - map::iterator act_iter; - vector >::iterator poll_iter; - curl_request *cr = *all_iter; + auto cr = *all_iter; log_info("%s:closing request -- %p", - cr->get_name().c_str(), cr); + cr->get_name().c_str(), cr.get()); (*all_iter)->close(); - act_iter = this->cl_handle_to_request.find(cr); + auto act_iter = this->cl_handle_to_request.find(cr->get_handle()); if (act_iter != this->cl_handle_to_request.end()) { - this->cl_handle_to_request.erase(act_iter); curl_multi_remove_handle(this->cl_curl_multi, cr->get_handle()); - delete cr; + this->cl_handle_to_request.erase(act_iter); } - poll_iter = find_if(this->cl_poll_queue.begin(), - this->cl_poll_queue.end(), - curl_request_eq(name)); + auto poll_iter = find_if(this->cl_poll_queue.begin(), + this->cl_poll_queue.end(), + curl_request_eq(name)); if (poll_iter != this->cl_poll_queue.end()) { this->cl_poll_queue.erase(poll_iter); } @@ -228,33 +225,30 @@ void curl_looper::check_for_finished_requests() curl_multi_remove_handle(this->cl_curl_multi, easy); if (iter != this->cl_handle_to_request.end()) { - curl_request *cr = iter->second; + auto cr = iter->second; long delay_ms; this->cl_handle_to_request.erase(iter); delay_ms = cr->complete(msg->data.result); if (delay_ms < 0) { - vector::iterator all_iter; - log_info("%s:curl_request %p finished, deleting...", - cr->get_name().c_str(), cr); + cr->get_name().c_str(), cr.get()); { std::unique_lock mg(this->cl_mutex); - all_iter = find(this->cl_all_requests.begin(), - this->cl_all_requests.end(), - cr); + auto all_iter = find(this->cl_all_requests.begin(), + this->cl_all_requests.end(), + cr); if (all_iter != this->cl_all_requests.end()) { this->cl_all_requests.erase(all_iter); } this->cl_cond.notify_all(); } - delete cr; } else { log_debug("%s:curl_request %p is polling, requeueing in %d", cr->get_name().c_str(), - cr, + cr.get(), delay_ms); this->cl_poll_queue.emplace_back(getmstime() + delay_ms, cr); sort(this->cl_poll_queue.begin(), this->cl_poll_queue.end()); diff --git a/src/curl_looper.hh b/src/curl_looper.hh index 9b0d92b7..04f61c7b 100644 --- a/src/curl_looper.hh +++ b/src/curl_looper.hh @@ -52,7 +52,7 @@ class curl_looper { public: void start() { }; void stop() { }; - void add_request(curl_request *cr) { }; + void add_request(std::shared_ptr cr) { }; void close_request(const std::string &name) { }; void process_all() { }; }; @@ -186,7 +186,7 @@ public: } }; - void add_request(curl_request *cr) { + void add_request(std::shared_ptr cr) { std::unique_lock mg(this->cl_mutex); require(cr != nullptr); @@ -232,11 +232,11 @@ private: auto_mem cl_curl_multi; std::mutex cl_mutex; std::condition_variable cl_cond; - std::vector cl_all_requests; - std::vector cl_new_requests; + std::vector> cl_all_requests; + std::vector> cl_new_requests; std::vector cl_close_requests; - std::map cl_handle_to_request; - std::vector > cl_poll_queue; + std::map> cl_handle_to_request; + std::vector>> cl_poll_queue; }; #endif diff --git a/src/lnav.cc b/src/lnav.cc index db81911b..1c5a6ce9 100644 --- a/src/lnav.cc +++ b/src/lnav.cc @@ -2339,11 +2339,11 @@ SELECT tbl_name FROM sqlite_master WHERE sql LIKE 'CREATE VIRTUAL TABLE%' } #ifdef HAVE_LIBCURL else if (is_url(argv[lpc])) { - unique_ptr ul(new url_loader(argv[lpc])); + auto ul = std::make_shared(argv[lpc]); lnav_data.ld_active_files.fc_file_names[argv[lpc]] .with_fd(ul->copy_fd()); - lnav_data.ld_curl_looper.add_request(ul.release()); + lnav_data.ld_curl_looper.add_request(ul); } #endif else if (is_glob(argv[lpc])) { diff --git a/src/lnav_commands.cc b/src/lnav_commands.cc index d36ac75f..cad54a72 100644 --- a/src/lnav_commands.cc +++ b/src/lnav_commands.cc @@ -112,13 +112,13 @@ static string refresh_pt_search() if (lnav_data.ld_pt_search.empty()) { return "info: no papertrail query is active"; } - unique_ptr pt(new papertrail_proc( - lnav_data.ld_pt_search.substr(3), - lnav_data.ld_pt_min_time, - lnav_data.ld_pt_max_time)); + auto pt = std::make_shared( + lnav_data.ld_pt_search.substr(3), + lnav_data.ld_pt_min_time, + lnav_data.ld_pt_max_time); lnav_data.ld_active_files.fc_file_names[lnav_data.ld_pt_search] .with_fd(pt->copy_fd()); - lnav_data.ld_curl_looper.add_request(pt.release()); + lnav_data.ld_curl_looper.add_request(pt); ensure_view(&lnav_data.ld_views[LNV_LOG]); @@ -1933,11 +1933,11 @@ static Result com_open(exec_context &ec, string cmdline, vector< retval = "error: lnav was not compiled with libcurl"; #else if (!ec.ec_dry_run) { - auto ul = make_unique(fn); + auto ul = make_shared(fn); lnav_data.ld_active_files.fc_file_names[fn] .with_fd(ul->copy_fd()); - lnav_data.ld_curl_looper.add_request(ul.release()); + lnav_data.ld_curl_looper.add_request(ul); lnav_data.ld_files_to_front.emplace_back(fn, top); retval = "info: opened URL"; } else { diff --git a/src/view_helpers.cc b/src/view_helpers.cc index 72bab598..65e23569 100644 --- a/src/view_helpers.cc +++ b/src/view_helpers.cc @@ -346,7 +346,6 @@ static unordered_map EXAMPLE_RESULTS; void execute_examples() { - exec_context &ec = lnav_data.ld_exec_context; db_label_source &dls = lnav_data.ld_db_row_source; db_overlay_source &dos = lnav_data.ld_db_overlay; textview_curses &db_tc = lnav_data.ld_views[LNV_DB];