summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--linux/request.cpp25
-rw-r--r--linux/request.hpp4
2 files changed, 27 insertions, 2 deletions
diff --git a/linux/request.cpp b/linux/request.cpp
index b9066bf240..2c4ebd1bb1 100644
--- a/linux/request.cpp
+++ b/linux/request.cpp
@@ -51,15 +51,37 @@ Request::Request(std::string url, std::function<void(platform::Response&)> bg, s
llmr::util::threadpool->add(request, this);
}
+
+pthread_key_t Request::key;
+pthread_once_t Request::key_once = PTHREAD_ONCE_INIT;
+
+void Request::create_key() {
+ pthread_key_create(&key, delete_key);
+}
+
+void Request::delete_key(void *ptr) {
+ if (ptr != nullptr) {
+ curl_easy_cleanup(ptr);
+ }
+}
+
void Request::request(void *ptr) {
assert(curl_share);
Request *req = static_cast<Request *>(ptr);
Response res;
+ pthread_once(&key_once, create_key);
// TODO: use curl multi to be able to cancel, or to
- CURL *curl = curl_easy_init();
+ CURL *curl = nullptr;
+ if ((curl = pthread_getspecific(key)) == nullptr) {
+ curl = curl_easy_init();
+ pthread_setspecific(key, curl);
+ }
+
+ curl_easy_reset(curl);
+
CURLcode code;
curl_easy_setopt(curl, CURLOPT_URL, req->url.c_str());
@@ -72,7 +94,6 @@ void Request::request(void *ptr) {
curl_easy_setopt(curl, CURLOPT_SHARE, curl_share);
code = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &res.code);
- curl_easy_cleanup(curl);
if (code != CURLE_ABORTED_BY_CALLBACK) {
req->background_function(res);
diff --git a/linux/request.hpp b/linux/request.hpp
index ae673e0da7..a61437c942 100644
--- a/linux/request.hpp
+++ b/linux/request.hpp
@@ -21,6 +21,8 @@ public:
void cancel();
private:
+ static void create_key();
+ static void delete_key(void *ptr);
static void request(void *);
static size_t curl_write_callback(void *, size_t, size_t, void *);
static int curl_progress_callback(void *, double, double, double, double);
@@ -28,6 +30,8 @@ private:
static void curl_share_unlock(CURL *, curl_lock_data, void *);
public:
+ static pthread_key_t key;
+ static pthread_once_t key_once;
std::atomic<bool> done;
std::atomic<bool> cancelled;
const std::string url;