]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
Resize the connection cache upwards when adding more handles than what
authorDaniel Stenberg <daniel@haxx.se>
Sat, 16 Sep 2006 21:50:29 +0000 (21:50 +0000)
committerDaniel Stenberg <daniel@haxx.se>
Sat, 16 Sep 2006 21:50:29 +0000 (21:50 +0000)
currently fits in the cache, to make the cache work better especially for
pipelining cases but also for "mere" (persistent) connection re-use.

lib/multi.c
lib/url.c
lib/url.h
lib/urldata.h

index 77c6a9fb4e2303c47a0e20643e1c8ffb8a0f3a84..2f7f328708c09a53e79260ecb77c7bdd70f11204 100644 (file)
@@ -423,6 +423,18 @@ CURLMcode curl_multi_add_handle(CURLM *multi_handle,
 
   /* increase the node-counter */
   multi->num_easy++;
+
+  if((multi->num_easy+5) > multi->connc->num) {
+    /* we want the connection cache to have room for all easy transfers, and
+       some more so we have a margin of 5 for now, but we add the new amount
+       plus 10 to not have to do it for every new handle added */
+    CURLcode res = Curl_ch_connc(easy_handle, multi->connc,
+                                 multi->num_easy + 10);
+    if(res)
+      /* TODO: we need to do some cleaning up here! */
+      return res;
+  }
+
   /* increase the alive-counter */
   multi->num_alive++;
 
index f3d21e869c6a1db732b94c14a7201fe39ac675dc..3f59299be638390f937d49e4c9b30dbc2629f753 100644 (file)
--- a/lib/url.c
+++ b/lib/url.c
@@ -346,6 +346,49 @@ struct conncache *Curl_mk_connc(int type)
   return c;
 }
 
+/* Change number of entries of a connection cache */
+CURLcode Curl_ch_connc(struct SessionHandle *data,
+                       struct conncache *c,
+                       long newamount)
+{
+  int i;
+  struct connectdata **newptr;
+
+  if(newamount < c->num) {
+    /* Since this number is *decreased* from the existing number, we must
+       close the possibly open connections that live on the indexes that
+       are being removed!
+
+       NOTE: for conncache_multi cases we must make sure that we only
+       close handles not in use.
+    */
+    for(i=newamount; i< c->num; i++)
+      Curl_disconnect(c->connects[i]);
+
+    /* If the most recent connection is no longer valid, mark it
+       invalid. */
+    if(data->state.lastconnect <= newamount)
+      data->state.lastconnect = -1;
+  }
+  if(newamount > 0) {
+    newptr= (struct connectdata **)
+      realloc(c->connects, sizeof(struct connectdata *) * newamount);
+    if(!newptr)
+      /* we closed a few connections in vain, but so what? */
+      return CURLE_OUT_OF_MEMORY;
+
+    /* nullify the newly added pointers */
+    for(i=c->num; i<newamount; i++)
+      newptr[i] = NULL;
+
+    c->connects = newptr;
+    c->num = newamount;
+  }
+  /* we no longer support less than 1 as size for the connection cache, and
+     I'm not sure it ever worked to set it to zero */
+  return CURLE_OK;
+}
+
 /* Free a connection cache. This is called from Curl_close() and
    curl_multi_cleanup(). */
 void Curl_rm_connc(struct conncache *c)
@@ -521,45 +564,7 @@ CURLcode Curl_setopt(struct SessionHandle *data, CURLoption option,
      * Set the absolute number of maximum simultaneous alive connection that
      * libcurl is allowed to have.
      */
-    {
-      long newconnects= va_arg(param, long);
-      struct connectdata **newptr;
-      long i;
-
-      if(newconnects < data->state.connc->num) {
-        /* Since this number is *decreased* from the existing number, we must
-           close the possibly open connections that live on the indexes that
-           are being removed!
-
-           NOTE: for conncache_multi cases we must make sure that we only
-           close handles not in use.
-        */
-        for(i=newconnects; i< data->state.connc->num; i++)
-          Curl_disconnect(data->state.connc->connects[i]);
-
-        /* If the most recent connection is no longer valid, mark it
-           invalid. */
-        if(data->state.lastconnect <= newconnects)
-          data->state.lastconnect = -1;
-      }
-      if(newconnects > 0) {
-        newptr= (struct connectdata **)
-          realloc(data->state.connc->connects,
-                  sizeof(struct connectdata *) * newconnects);
-        if(!newptr)
-          /* we closed a few connections in vain, but so what? */
-          return CURLE_OUT_OF_MEMORY;
-
-        /* nullify the newly added pointers */
-        for(i=data->state.connc->num; i<newconnects; i++)
-          newptr[i] = NULL;
-
-        data->state.connc->connects = newptr;
-        data->state.connc->num = newconnects;
-      }
-      /* we no longer support less than 1 as size for the connection cache,
-         and I'm not sure it ever worked to set it to zero */
-    }
+    result = Curl_ch_connc(data, data->state.connc, va_arg(param, long));
     break;
   case CURLOPT_FORBID_REUSE:
     /*
@@ -3115,7 +3120,7 @@ static CURLcode CreateConnection(struct SessionHandle *data,
   conn->connectindex = -1;    /* no index */
 
   conn->bits.httpproxy = (bool)(data->change.proxy  /* http proxy or not */
-                             && *data->change.proxy 
+                             && *data->change.proxy
                              && (data->set.proxytype == CURLPROXY_HTTP));
 
   /* Default protocol-independent behavior doesn't support persistent
index 4b65a7ab734c22727704cb174bc1462ad810e75d..e1754c333fcbabf938ebd0c021880574a926c1a7 100644 (file)
--- a/lib/url.h
+++ b/lib/url.h
@@ -50,6 +50,10 @@ void Curl_safefree(void *ptr);
 struct conncache *Curl_mk_connc(int type);
 /* free a connection cache */
 void Curl_rm_connc(struct conncache *c);
+/* Change number of entries of a connection cache */
+CURLcode Curl_ch_connc(struct SessionHandle *data,
+                       struct conncache *c,
+                       long newamount);
 
 int Curl_protocol_getsock(struct connectdata *conn,
                           curl_socket_t *socks,
index 9f8e6de806e218f69ee3205ef37fc3bb36adb2ff..d90c6cd08db0d1f31b9fb19b4edf8afc19caabe9 100644 (file)
@@ -951,7 +951,7 @@ struct conncache {
   /* 'connects' will be an allocated array with pointers. If the pointer is
      set, it holds an allocated connection. */
   struct connectdata **connects;
-  long num;           /* size of the 'connects' array */
+  long num;           /* number of entries of the 'connects' array */
   enum {
     CONNCACHE_PRIVATE, /* used for an easy handle alone */
     CONNCACHE_MULTI    /* shared within a multi handle */