Commit 2d5fc39d authored by Daniel Stenberg's avatar Daniel Stenberg
Browse files

Resize the connection cache upwards when adding more handles than what

currently fits in the cache, to make the cache work better especially for
pipelining cases but also for "mere" (persistent) connection re-use.
parent c001ed53
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -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++;

+45 −40
Original line number Diff line number Diff line
@@ -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:
    /*
+4 −0
Original line number Diff line number Diff line
@@ -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,
+1 −1
Original line number Diff line number Diff line
@@ -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 */