diff --git a/src/aur/aur.cc b/src/aur/aur.cc index e312df9..997e231 100644 --- a/src/aur/aur.cc +++ b/src/aur/aur.cc @@ -40,8 +40,8 @@ class AurImpl : public Aur { void QueueCloneRequest(const CloneRequest& request, const CloneResponseCallback& callback) override; - // Wait for all pending requests to complete. Returns non-zero if any request - // failed or was cancelled by a callback. + // Wait for all pending requests to complete. Returns non-zero if the event + // loop fails or is terminated by a user callback. int Wait() override; private: @@ -53,12 +53,10 @@ class AurImpl : public Aur { const HttpRequest& request, const typename ResponseHandlerType::CallbackType& callback); - int FinishRequest(CURL* curl, CURLcode result, bool dispatch_callback); + int FinishRequest(CURL* curl, CURLcode result); int FinishRequest(sd_event_source* source); int CheckFinished(); - void CancelAll(); - void Cancel(const ActiveRequests::value_type& request); enum class DebugLevel { // No debugging. @@ -92,7 +90,6 @@ class AurImpl : public Aur { sigset_t saved_ss_{}; sd_event* event_ = nullptr; sd_event_source* timer_ = nullptr; - bool cancelled_ = false; DebugLevel debug_level_ = DebugLevel::NONE; std::ofstream debug_stream_; @@ -274,31 +271,6 @@ AurImpl::~AurImpl() { } } -void AurImpl::Cancel(const ActiveRequests::value_type& request) { - struct Visitor { - constexpr explicit Visitor(AurImpl* aur) : aur(aur) {} - - void operator()(CURL* curl) { - aur->FinishRequest(curl, CURLE_ABORTED_BY_CALLBACK, - /*dispatch_callback=*/false); - } - - void operator()(sd_event_source* source) { aur->FinishRequest(source); } - - AurImpl* aur; - }; - - std::visit(Visitor(this), request); -} - -void AurImpl::CancelAll() { - while (!active_requests_.empty()) { - Cancel(*active_requests_.begin()); - } - - cancelled_ = true; -} - // static int AurImpl::SocketCallback(CURLM*, curl_socket_t s, int action, void* userdata, void* sockptr) { @@ -401,8 +373,9 @@ int AurImpl::DispatchTimerCallback(long timeout_ms) { return 0; } - uint64_t usec = - absl::ToUnixMicros(absl::Now() + absl::Milliseconds(timeout_ms)); + uint64_t usec; + sd_event_now(event_, CLOCK_MONOTONIC, &usec); + usec += timeout_ms * 1000; if (timer_ != nullptr) { if (sd_event_source_set_time(timer_, usec) < 0) { @@ -413,7 +386,8 @@ int AurImpl::DispatchTimerCallback(long timeout_ms) { return -1; } } else { - if (sd_event_add_time(event_, &timer_, CLOCK_REALTIME, usec, 0, + // TODO: use sd_event_add_time_relative once its available. + if (sd_event_add_time(event_, &timer_, CLOCK_MONOTONIC, usec, 0, &AurImpl::OnCurlTimer, this) < 0) { return -1; } @@ -422,21 +396,15 @@ int AurImpl::DispatchTimerCallback(long timeout_ms) { return 0; } -int AurImpl::FinishRequest(CURL* curl, CURLcode result, - bool dispatch_callback) { +int AurImpl::FinishRequest(CURL* curl, CURLcode result) { ResponseHandler* handler; curl_easy_getinfo(curl, CURLINFO_PRIVATE, &handler); - int r = 0; - if (dispatch_callback) { - absl::Status status = - result == CURLE_OK ? StatusFromCurlHandle(curl) - : absl::UnknownError(handler->error_buffer.data()); + absl::Status status = result == CURLE_OK + ? StatusFromCurlHandle(curl) + : absl::UnknownError(handler->error_buffer.data()); - r = handler->RunCallback(std::move(status)); - } else { - delete handler; - } + int r = handler->RunCallback(std::move(status)); active_requests_.erase(curl); curl_multi_remove_handle(curl_multi_, curl); @@ -459,25 +427,27 @@ int AurImpl::CheckFinished() { return 0; } - auto r = FinishRequest(msg->easy_handle, msg->data.result, - /* dispatch_callback = */ true); + int r = FinishRequest(msg->easy_handle, msg->data.result); if (r < 0) { - CancelAll(); + sd_event_exit(event_, r); } return r; } int AurImpl::Wait() { - cancelled_ = false; - while (!active_requests_.empty()) { if (sd_event_run(event_, 1) < 0) { return -EIO; } } - return cancelled_ ? -ECANCELED : 0; + int r; + if (sd_event_get_exit_code(event_, &r) == 0) { + return r; + } + + return 0; } template