Index: docs/libcurl/curl_easy_setopt.3
===================================================================
RCS file: /cvsroot/curl/curl/docs/libcurl/curl_easy_setopt.3,v
retrieving revision 1.180
diff -u -r1.180 curl_easy_setopt.3
--- docs/libcurl/curl_easy_setopt.3	15 May 2007 00:28:50 -0000	1.180
+++ docs/libcurl/curl_easy_setopt.3	30 May 2007 10:54:08 -0000
@@ -1129,10 +1129,10 @@
 .IP CURLOPT_MAXCONNECTS
 Pass a long. The set number will be the persistent connection cache size. The
 set amount will be the maximum amount of simultaneously open connections that
-libcurl may cache. Default is 5, and there isn't much point in changing this
-value unless you are perfectly aware of how this work and changes libcurl's
-behaviour. This concerns connection using any of the protocols that support
-persistent connections.
+libcurl may cache in this easy handle. Default is 5, and there isn't much
+point in changing this value unless you are perfectly aware of how this work
+and changes libcurl's behaviour. This concerns connection using any of the
+protocols that support persistent connections.
 
 When reaching the maximum limit, curl closes the oldest one in the cache to
 prevent the number of open connections to increase.
@@ -1140,6 +1140,10 @@
 If you already have performed transfers with this curl handle, setting a
 smaller MAXCONNECTS than before may cause open connections to get closed
 unnecessarily.
+
+Note that if you add this easy handle to a multi handle, this setting is not
+being acknowledged, but you must instead use \fIcurl_multi_setopt(3)\fP and
+the \fICURLMOPT_MAXCONNECTS\fP option.
 .IP CURLOPT_CLOSEPOLICY
 (Obsolete) This option does nothing.
 .IP CURLOPT_FRESH_CONNECT
Index: docs/libcurl/curl_multi_setopt.3
===================================================================
RCS file: /cvsroot/curl/curl/docs/libcurl/curl_multi_setopt.3,v
retrieving revision 1.5
diff -u -r1.5 curl_multi_setopt.3
--- docs/libcurl/curl_multi_setopt.3	12 Oct 2006 08:36:49 -0000	1.5
+++ docs/libcurl/curl_multi_setopt.3	30 May 2007 10:54:08 -0000
@@ -56,6 +56,22 @@
 \fBcurl_multi_timer_callback\fP's third argument, the userp pointer.  This is
 not used by libcurl but only passed-thru as-is. Set the callback pointer with
 \fICURLMOPT_TIMERFUNCTION\fP. (Added in 7.16.0)
+.IP CURLMOPT_MAXCONNECTS
+Pass a long. The set number will be the persistent connection cache size for
+this multi handle. The set amount will be the maximum amount of simultaneously
+open connections that libcurl may cache. Default is 10, but libcurl will
+enlarge the size for each added easy handle to make it fit 4 times the number
+of added easy handles.
+
+By setting this option, you can prevent the cache size to grow beyond the
+limit set by you.
+
+When reaching the maximum limit, curl closes the oldest one in the cache to
+prevent the number of open connections to increase.
+
+If you already have performed transfers with this curl handle, setting a
+smaller MAXCONNECTS than before may cause open connections to get closed
+unnecessarily.
 .SH RETURNS
 The standard CURLMcode for multi interface error codes. Note that it returns a
 CURLM_UNKNOWN_OPTION if you try setting an option that this version of libcurl
Index: include/curl/multi.h
===================================================================
RCS file: /cvsroot/curl/curl/include/curl/multi.h,v
retrieving revision 1.43
diff -u -r1.43 multi.h
--- include/curl/multi.h	16 Apr 2007 16:34:08 -0000	1.43
+++ include/curl/multi.h	30 May 2007 10:54:08 -0000
@@ -309,6 +309,9 @@
   /* This is the argument passed to the timer callback */
   CINIT(TIMERDATA, OBJECTPOINT, 5),
 
+  /* maximum number of entries in the connection cache */
+  CINIT(MAXCONNECTS, LONG, 6),
+
   CURLMOPT_LASTENTRY /* the last unused */
 } CURLMoption;
 
Index: lib/multi.c
===================================================================
RCS file: /cvsroot/curl/curl/lib/multi.c,v
retrieving revision 1.143
diff -u -r1.143 multi.c
--- lib/multi.c	7 May 2007 07:07:55 -0000	1.143
+++ lib/multi.c	30 May 2007 10:54:08 -0000
@@ -160,6 +160,8 @@
 
   /* shared connection cache */
   struct conncache *connc;
+  long maxconnects; /* if >0, a fixed limit of the maximum number of entries
+                       we're allowed to grow the connection cache to */
 
   /* list of easy handles kept around for doing nice connection closures */
   struct closure *closure;
@@ -484,11 +486,19 @@
     /* We want the connection cache to have plenty room. Before we supported
        the shared cache every single easy handle had 5 entries in their cache
        by default. */
-    CURLcode res = Curl_ch_connc(easy_handle, multi->connc,
-                                 multi->num_easy * 4);
-    if(res != CURLE_OK)
-      /* TODO: we need to do some cleaning up here! */
-      return CURLM_OUT_OF_MEMORY;
+    int newmax = multi->num_easy * 4;
+
+    if(multi->maxconnects && (multi->maxconnects < newmax))
+      /* don't grow beyond the allowed size */
+      newmax = multi->maxconnects;
+
+    if(newmax > multi->connc->num) {
+      /* we only do this is we can in fact grow the cache */
+      CURLcode res = Curl_ch_connc(easy_handle, multi->connc, newmax);
+      if(res != CURLE_OK)
+        /* TODO: we need to do some cleaning up here! */
+        return CURLM_OUT_OF_MEMORY;
+    }
   }
 
   /* increase the alive-counter */
@@ -1810,6 +1820,9 @@
   case CURLMOPT_TIMERDATA:
     multi->timer_userp = va_arg(param, void *);
     break;
+  case CURLMOPT_MAXCONNECTS:
+    multi->maxconnects = va_arg(param, long);
+    break;
   default:
     res = CURLM_UNKNOWN_OPTION;
     break;
