Blame SOURCES/0004-curl-Enable-multi-conn-for-read-only-connections.patch

0b2b05
From 3f74004478d3590840d7eba97a590b7ec954957f Mon Sep 17 00:00:00 2001
0b2b05
From: "Richard W.M. Jones" <rjones@redhat.com>
0b2b05
Date: Thu, 2 Feb 2023 13:59:32 +0000
0b2b05
Subject: [PATCH] curl: Enable multi-conn for read-only connections
0b2b05
MIME-Version: 1.0
0b2b05
Content-Type: text/plain; charset=UTF-8
0b2b05
Content-Transfer-Encoding: 8bit
0b2b05
0b2b05
Comparing before and after this commit shows approximately double the
0b2b05
performance.  In other tests this allowed us to download files from
0b2b05
web servers at line speed.
0b2b05
0b2b05
  Benchmark 1:  nbdkit -r curl file:/var/tmp/jammy-server-cloudimg-amd64.raw --run "nbdcopy -p \$uri null:"
0b2b05
    Time (mean ± σ):     943.8 ms ±  18.8 ms    [User: 316.2 ms, System: 1029.7 ms]
0b2b05
    Range (min … max):   923.7 ms … 989.2 ms    10 runs
0b2b05
0b2b05
  Benchmark 2:  ~/d/nbdkit/nbdkit -r curl file:/var/tmp/jammy-server-cloudimg-amd64.raw --run "nbdcopy -p \$uri null:"
0b2b05
    Time (mean ± σ):     455.0 ms ±   6.2 ms    [User: 542.2 ms, System: 1824.7 ms]
0b2b05
    Range (min … max):   449.1 ms … 471.6 ms    10 runs
0b2b05
0b2b05
  Summary
0b2b05
    ' ~/d/nbdkit/nbdkit -r curl file:/var/tmp/jammy-server-cloudimg-amd64.raw --run "nbdcopy -p \$uri null:" ' ran
0b2b05
      2.07 ± 0.05 times faster than ' nbdkit -r curl file:/var/tmp/jammy-server-cloudimg-amd64.raw --run "nbdcopy -p \$uri null:" '
0b2b05
0b2b05
Multi-conn is enabled only when we know the connection is read-only:
0b2b05
0b2b05
  $ ./nbdkit -r curl file:/var/tmp/jammy-server-cloudimg-amd64.raw --run ' nbdinfo $uri ' | grep can_multi_conn
0b2b05
	can_multi_conn: true
0b2b05
  $ ./nbdkit curl file:/var/tmp/jammy-server-cloudimg-amd64.raw --run ' nbdinfo $uri ' | grep can_multi_conn
0b2b05
	can_multi_conn: false
0b2b05
0b2b05
See also:
0b2b05
https://listman.redhat.com/archives/libguestfs/2023-February/030581.html
0b2b05
0b2b05
Reviewed-by: Eric Blake <eblake@redhat.com>
0b2b05
(cherry picked from commit bb0f93ad7b9de451874d0c54188bf69cd37c5409)
0b2b05
---
0b2b05
 plugins/curl/curl.c     | 14 ++++++++++++++
0b2b05
 plugins/curl/curldefs.h |  1 +
0b2b05
 2 files changed, 15 insertions(+)
0b2b05
0b2b05
diff --git a/plugins/curl/curl.c b/plugins/curl/curl.c
0b2b05
index e89bea99..eeba5aa4 100644
0b2b05
--- a/plugins/curl/curl.c
0b2b05
+++ b/plugins/curl/curl.c
0b2b05
@@ -455,6 +455,7 @@ curl_open (int readonly)
0b2b05
     nbdkit_error ("calloc: %m");
0b2b05
     return NULL;
0b2b05
   }
0b2b05
+  h->readonly = readonly;
0b2b05
 
0b2b05
   h->c = curl_easy_init ();
0b2b05
   if (h->c == NULL) {
0b2b05
@@ -764,6 +765,18 @@ curl_get_size (void *handle)
0b2b05
   return h->exportsize;
0b2b05
 }
0b2b05
 
0b2b05
+/* Multi-conn is safe for read-only connections, but HTTP does not
0b2b05
+ * have any concept of flushing so we cannot use it for read-write
0b2b05
+ * connections.
0b2b05
+ */
0b2b05
+static int
0b2b05
+curl_can_multi_conn (void *handle)
0b2b05
+{
0b2b05
+  struct curl_handle *h = handle;
0b2b05
+
0b2b05
+  return !! h->readonly;
0b2b05
+}
0b2b05
+
0b2b05
 /* NB: The terminology used by libcurl is confusing!
0b2b05
  *
0b2b05
  * WRITEFUNCTION / write_cb is used when reading from the remote server
0b2b05
@@ -907,6 +920,7 @@ static struct nbdkit_plugin plugin = {
0b2b05
   .open              = curl_open,
0b2b05
   .close             = curl_close,
0b2b05
   .get_size          = curl_get_size,
0b2b05
+  .can_multi_conn    = curl_can_multi_conn,
0b2b05
   .pread             = curl_pread,
0b2b05
   .pwrite            = curl_pwrite,
0b2b05
 };
0b2b05
diff --git a/plugins/curl/curldefs.h b/plugins/curl/curldefs.h
0b2b05
index f3095f92..9d4949f3 100644
0b2b05
--- a/plugins/curl/curldefs.h
0b2b05
+++ b/plugins/curl/curldefs.h
0b2b05
@@ -64,6 +64,7 @@ extern const char *user_agent;
0b2b05
 /* The per-connection handle. */
0b2b05
 struct curl_handle {
0b2b05
   CURL *c;
0b2b05
+  int readonly;
0b2b05
   bool accept_range;
0b2b05
   int64_t exportsize;
0b2b05
   char errbuf[CURL_ERROR_SIZE];
0b2b05
-- 
0b2b05
2.31.1
0b2b05