From 1aa8521e15af6d1fba5582f8adbe7aa78d1129e6 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 7 Feb 2023 10:22:07 +0100 Subject: [PATCH] Remove the lock. As we now build a new request handle for every request, we don't need a lock. libcurl itself is thread safe as long as we don't share a handle. --- src/aria2.cpp | 42 +++++++++++++++++------------------------- src/aria2.h | 4 +--- 2 files changed, 18 insertions(+), 28 deletions(-) diff --git a/src/aria2.cpp b/src/aria2.cpp index ee7f3b8ba..e58b2bd67 100644 --- a/src/aria2.cpp +++ b/src/aria2.cpp @@ -115,11 +115,6 @@ Aria2::Aria2(): } } -Aria2::~Aria2() -{ - std::unique_lock lock(m_lock); -} - void Aria2::close() { saveSession(); @@ -139,28 +134,25 @@ std::string Aria2::doRequest(const MethodCall& methodCall) std::stringstream outStream; CURLcode res; long response_code; - { - std::unique_lock lock(m_lock); - char curlErrorBuffer[CURL_ERROR_SIZE]; - CURL* p_curl = curl_easy_init(); - curl_easy_setopt(p_curl, CURLOPT_URL, "http://localhost/rpc"); - curl_easy_setopt(p_curl, CURLOPT_PORT, m_port); - curl_easy_setopt(p_curl, CURLOPT_POST, 1L); - curl_easy_setopt(p_curl, CURLOPT_ERRORBUFFER, curlErrorBuffer); - curl_easy_setopt(p_curl, CURLOPT_POSTFIELDSIZE, requestContent.size()); - curl_easy_setopt(p_curl, CURLOPT_POSTFIELDS, requestContent.c_str()); - curl_easy_setopt(p_curl, CURLOPT_WRITEFUNCTION, &write_callback_to_iss); - curl_easy_setopt(p_curl, CURLOPT_WRITEDATA, &outStream); - curlErrorBuffer[0] = 0; - res = curl_easy_perform(p_curl); - if (res != CURLE_OK) { - LOG_ARIA_ERROR(); - curl_easy_cleanup(p_curl); - throw std::runtime_error("Cannot perform request"); - } - curl_easy_getinfo(p_curl, CURLINFO_RESPONSE_CODE, &response_code); + char curlErrorBuffer[CURL_ERROR_SIZE]; + CURL* p_curl = curl_easy_init(); + curl_easy_setopt(p_curl, CURLOPT_URL, "http://localhost/rpc"); + curl_easy_setopt(p_curl, CURLOPT_PORT, m_port); + curl_easy_setopt(p_curl, CURLOPT_POST, 1L); + curl_easy_setopt(p_curl, CURLOPT_ERRORBUFFER, curlErrorBuffer); + curl_easy_setopt(p_curl, CURLOPT_POSTFIELDSIZE, requestContent.size()); + curl_easy_setopt(p_curl, CURLOPT_POSTFIELDS, requestContent.c_str()); + curl_easy_setopt(p_curl, CURLOPT_WRITEFUNCTION, &write_callback_to_iss); + curl_easy_setopt(p_curl, CURLOPT_WRITEDATA, &outStream); + curlErrorBuffer[0] = 0; + res = curl_easy_perform(p_curl); + if (res != CURLE_OK) { + LOG_ARIA_ERROR(); curl_easy_cleanup(p_curl); + throw std::runtime_error("Cannot perform request"); } + curl_easy_getinfo(p_curl, CURLINFO_RESPONSE_CODE, &response_code); + curl_easy_cleanup(p_curl); auto responseContent = outStream.str(); if (response_code != 200) { diff --git a/src/aria2.h b/src/aria2.h index ee4b58eb6..f6cd633b8 100644 --- a/src/aria2.h +++ b/src/aria2.h @@ -12,7 +12,6 @@ #include "xmlrpc.h" #include -#include #include namespace kiwix { @@ -24,12 +23,11 @@ class Aria2 int m_port; std::string m_secret; std::string m_downloadDir; - std::mutex m_lock; std::string doRequest(const MethodCall& methodCall); public: Aria2(); - virtual ~Aria2(); + virtual ~Aria2() = default; void close(); std::string addUri(const std::vector& uri, const std::vector>& options = {});