]> git.ipfire.org Git - telemetry.git/commitdiff
ctx: Add a cURL share handle
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 10 Jul 2026 11:20:27 +0000 (11:20 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 10 Jul 2026 11:20:27 +0000 (11:20 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/ctx.c
src/daemon/ctx.h
src/daemon/request.c

index aef344c90b2cc809714387f5067b734e01d49242..58dc631e268cc15949c93c41534bed1b02c8a11b 100644 (file)
@@ -22,6 +22,8 @@
 #include <stdlib.h>
 #include <syslog.h>
 
+#include <curl/curl.h>
+
 #include "ctx.h"
 #include "logging.h"
 
@@ -36,6 +38,9 @@ struct td_ctx {
                td_log_callback callback;
                void* data;
        } log;
+
+       // cURL share handle
+       CURLSH* share;
 };
 
 static int td_ctx_setup_logging(td_ctx* ctx) {
@@ -48,8 +53,10 @@ static int td_ctx_setup_logging(td_ctx* ctx) {
        return 0;
 }
 
-static void td_ctx_free(td_ctx* ctx) {
-       free(ctx);
+static void td_ctx_free(td_ctx* self) {
+       if (self->share)
+               curl_share_cleanup(self->share);
+       free(self);
 }
 
 int td_ctx_create(td_ctx** ctx) {
@@ -124,3 +131,32 @@ void td_ctx_log(td_ctx* self, int level,
        self->log.callback(self->log.data, level, file, line, fn, format, args);
        va_end(args);
 }
+
+CURLSH* td_ctx_get_curl_share(td_ctx* self) {
+       int r;
+
+       if (!self->share) {
+               self->share = curl_share_init();
+               if (!self->share) {
+                       ERROR(self, "Could not setup cURL share handle\n");
+                       goto ERROR;
+               }
+
+               // Share connections between handles
+               r = curl_share_setopt(self->share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
+               if (r) {
+                       ERROR(self, "Could not configure the share handle: %s\n", curl_share_strerror(r));
+                       goto ERROR;
+               }
+       }
+
+       return self->share;
+
+ERROR:
+       if (self->share) {
+               curl_share_cleanup(self->share);
+               self->share = NULL;
+       }
+
+       return NULL;
+}
index 35f8a6e11dfaf960d387fa27b36930484ca0a8cf..6459430a205d1c1947cda7c9bc9acc45e15472f2 100644 (file)
@@ -21,6 +21,8 @@
 #ifndef TELEMETRY_CTX_H
 #define TELEMETRY_CTX_H
 
+#include <curl/curl.h>
+
 typedef struct td_ctx td_ctx;
 
 int td_ctx_create(td_ctx** ctx);
@@ -40,4 +42,6 @@ void td_ctx_set_log_callback(td_ctx* ctx,
 void td_ctx_log(td_ctx* self, int level,
        const char* file, int line, const char* fn, const char* format, ...);
 
+CURLSH* td_ctx_get_curl_share(td_ctx* self);
+
 #endif /* TELEMETRY_CTX_H */
index 7072fe64629d515a03e326f7e2a2826a4fa2fcfe..225a1351b9f89305ab4da021b100d456a7a8ae1e 100644 (file)
@@ -96,7 +96,16 @@ static size_t td_request_write(char* data, size_t size, size_t nmemb, void* p) {
 static int td_request_setup(td_request* self) {
        int r;
 
-       // XXX Share Handle
+       // Share Handle
+       CURLSH* share = td_ctx_get_curl_share(self->ctx);
+
+       // Configure the share handle
+       r = curl_easy_setopt(self->handle, CURLOPT_SHARE, share);
+       if (r) {
+               ERROR(self->ctx, "Could not configure cURL share handle: %s\n",
+                               curl_easy_strerror(r));
+               goto ERROR;
+       }
 
        // Be a good net citizen and set a user agent
        r = curl_easy_setopt(self->handle, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; "