Add multi_curl handle to ALPM global context

To be able to run multiple download in parallel efficiently we need to
use curl_multi interface [1]. It introduces a set of APIs over new type
of handler 'CURLM'.

Create CURLM object at the application start and set it to global ALPM
context.

The 'single-download' CURL handle moves to payload struct. A new CURL
handle is created for each payload with intention to be processed by CURLM.

Note that curl_download_internal() is not ported to CURLM interface due
to the fact that the function will go away soon.

[1] https://curl.haxx.se/libcurl/c/libcurl-multi.html

Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
Anatol Pomozov 2020-03-09 15:23:12 -07:00 committed by Allan McRae
parent a8a1a1bb3e
commit dc98d0ea09
5 changed files with 17 additions and 20 deletions

View file

@ -70,6 +70,11 @@ alpm_handle_t SYMEXPORT *alpm_initialize(const char *root, const char *dbpath,
goto cleanup; goto cleanup;
} }
#ifdef HAVE_LIBCURL
curl_global_init(CURL_GLOBAL_ALL);
myhandle->curlm = curl_multi_init();
#endif
#ifdef ENABLE_NLS #ifdef ENABLE_NLS
bindtextdomain("libalpm", LOCALEDIR); bindtextdomain("libalpm", LOCALEDIR);
#endif #endif
@ -104,13 +109,14 @@ int SYMEXPORT alpm_release(alpm_handle_t *myhandle)
ret = -1; ret = -1;
} }
_alpm_handle_unlock(myhandle);
_alpm_handle_free(myhandle);
#ifdef HAVE_LIBCURL #ifdef HAVE_LIBCURL
curl_multi_cleanup(myhandle->curlm);
curl_global_cleanup(); curl_global_cleanup();
#endif #endif
_alpm_handle_unlock(myhandle);
_alpm_handle_free(myhandle);
return ret; return ret;
} }

View file

@ -72,15 +72,6 @@ static char *get_fullpath(const char *path, const char *filename,
return filepath; return filepath;
} }
static CURL *get_libcurl_handle(alpm_handle_t *handle)
{
if(!handle->curl) {
curl_global_init(CURL_GLOBAL_SSL);
handle->curl = curl_easy_init();
}
return handle->curl;
}
enum { enum {
ABORT_SIGINT = 1, ABORT_SIGINT = 1,
ABORT_OVER_MAXFILESIZE ABORT_OVER_MAXFILESIZE
@ -241,7 +232,7 @@ static size_t dload_parseheader_cb(void *ptr, size_t size, size_t nmemb, void *u
} }
} }
curl_easy_getinfo(payload->handle->curl, CURLINFO_RESPONSE_CODE, &respcode); curl_easy_getinfo(payload->curl, CURLINFO_RESPONSE_CODE, &respcode);
if(payload->respcode != respcode) { if(payload->respcode != respcode) {
payload->respcode = respcode; payload->respcode = respcode;
} }
@ -377,8 +368,9 @@ static int curl_download_internal(struct dload_payload *payload,
struct sigaction orig_sig_pipe, orig_sig_int; struct sigaction orig_sig_pipe, orig_sig_int;
/* shortcut to our handle within the payload */ /* shortcut to our handle within the payload */
alpm_handle_t *handle = payload->handle; alpm_handle_t *handle = payload->handle;
CURL *curl = get_libcurl_handle(handle); CURL *curl = curl_easy_init();
handle->pm_errno = ALPM_ERR_OK; handle->pm_errno = ALPM_ERR_OK;
payload->curl = curl;
/* make sure these are NULL */ /* make sure these are NULL */
FREE(payload->tempfile_name); FREE(payload->tempfile_name);
@ -592,6 +584,9 @@ cleanup:
unlink(payload->tempfile_name); unlink(payload->tempfile_name);
} }
curl_easy_cleanup(curl);
payload->curl = NULL;
/* restore the old signal handlers */ /* restore the old signal handlers */
unmask_signal(SIGINT, &orig_sig_int); unmask_signal(SIGINT, &orig_sig_int);
unmask_signal(SIGPIPE, &orig_sig_pipe); unmask_signal(SIGPIPE, &orig_sig_pipe);

View file

@ -44,6 +44,7 @@ struct dload_payload {
int trust_remote_name; int trust_remote_name;
int cb_initialized; int cb_initialized;
#ifdef HAVE_LIBCURL #ifdef HAVE_LIBCURL
CURL *curl;
CURLcode curlerr; /* last error produced by curl */ CURLcode curlerr; /* last error produced by curl */
#endif #endif
}; };

View file

@ -64,11 +64,6 @@ void _alpm_handle_free(alpm_handle_t *handle)
closelog(); closelog();
} }
#ifdef HAVE_LIBCURL
/* release curl handle */
curl_easy_cleanup(handle->curl);
#endif
#ifdef HAVE_LIBGPGME #ifdef HAVE_LIBGPGME
FREELIST(handle->known_keys); FREELIST(handle->known_keys);
#endif #endif

View file

@ -59,7 +59,7 @@ struct __alpm_handle_t {
#ifdef HAVE_LIBCURL #ifdef HAVE_LIBCURL
/* libcurl handle */ /* libcurl handle */
CURL *curl; /* reusable curl_easy handle */ CURLM *curlm;
unsigned short disable_dl_timeout; unsigned short disable_dl_timeout;
unsigned int parallel_downloads; /* number of download streams */ unsigned int parallel_downloads; /* number of download streams */
#endif #endif