From: Michael Tremer Date: Fri, 10 Jul 2026 11:20:27 +0000 (+0000) Subject: ctx: Add a cURL share handle X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=42a45dc5f869c5441ba833356bad78d80e2a100c;p=collecty.git ctx: Add a cURL share handle Signed-off-by: Michael Tremer --- diff --git a/src/daemon/ctx.c b/src/daemon/ctx.c index aef344c..58dc631 100644 --- a/src/daemon/ctx.c +++ b/src/daemon/ctx.c @@ -22,6 +22,8 @@ #include #include +#include + #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; +} diff --git a/src/daemon/ctx.h b/src/daemon/ctx.h index 35f8a6e..6459430 100644 --- a/src/daemon/ctx.h +++ b/src/daemon/ctx.h @@ -21,6 +21,8 @@ #ifndef TELEMETRY_CTX_H #define TELEMETRY_CTX_H +#include + 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 */ diff --git a/src/daemon/request.c b/src/daemon/request.c index 7072fe6..225a135 100644 --- a/src/daemon/request.c +++ b/src/daemon/request.c @@ -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; "