Jelajahi Sumber

-fixing #2899: allow clients to customize MHD_BUF_INC_SIZE via option

Christian Grothoff 12 tahun lalu
induk
melakukan
6e07994a8b

+ 1 - 0
AUTHORS

@@ -22,6 +22,7 @@ Matthew Moore
 Colin Caughie <[email protected]>
 David Carvalho <[email protected]>
 David Reiss <[email protected]>
+Matt Holiday 
 Mika Raento <[email protected]>
 Mike Crowe <[email protected]>
 John Muth <[email protected]>

+ 5 - 0
ChangeLog

@@ -1,3 +1,8 @@
+Fri Jul  5 12:05:01 CEST 2013
+	Added MHD_OPTION_CONNECTION_MEMORY_INCREMENT to allow users
+	to specify a custom value for incrementing read buffer
+	sizes (#2899). -MH
+
 Fri Jun 28 14:05:15 CEST 2013
 	If we shutdown connection for reading on POST due to error,
 	really do not process further requests even if we already

+ 9 - 0
doc/libmicrohttpd.texi

@@ -509,6 +509,15 @@ result in much benefit, as half of the memory will be typically used
 for IO, and TCP buffers are unlikely to support window sizes above 64k
 on most systems.
 
+@item MHD_OPTION_CONNECTION_MEMORY_INCREMENT
+@cindex memory
+Increment to use for growing the read buffer (followed by a
+@code{size_t}).  The default is 1024 (bytes).  Increasing this value
+will make MHD use memory for reading more aggressively, which can
+reduce the number of @code{recvfrom} calls but may increase the number
+of @code{sendto} calls.  The given value must fit within
+MHD_OPTION_CONNECTION_MEMORY_LIMIT.
+
 @item MHD_OPTION_CONNECTION_LIMIT
 @cindex connection, limiting number of connections
 Maximum number of concurrent connections to accept (followed by an

+ 7 - 1
src/include/microhttpd.h

@@ -673,7 +673,13 @@ enum MHD_OPTION
    * HTTPS daemon for client authentification.
    * This option should be followed by a "const char*" argument.
    */
-  MHD_OPTION_HTTPS_MEM_TRUST = 20
+  MHD_OPTION_HTTPS_MEM_TRUST = 20,
+
+  /**
+   * Increment to use for growing the read buffer (followed by a
+   * size_t). Must fit within MHD_OPTION_CONNECTION_MEMORY_LIMIT.
+   */
+  MHD_OPTION_CONNECTION_MEMORY_INCREMENT = 21
 };
 
 

+ 1 - 9
src/microhttpd/connection.c

@@ -37,14 +37,6 @@
 #include <netinet/tcp.h>
 #endif
 
-/**
- * Minimum size by which MHD tries to increment read/write buffers.
- * We usually begin with half the available pool space for the
- * IO-buffer, but if absolutely needed we additively grow by the
- * number of bytes given here (up to -- theoretically -- the full pool
- * space).
- */
-#define MHD_BUF_INC_SIZE 1024
 
 /**
  * Message to transmit when http 1.1 request is received
@@ -1861,7 +1853,7 @@ MHD_connection_handle_read (struct MHD_Connection *connection)
     return MHD_YES;
   /* make sure "read" has a reasonable number of bytes
      in buffer to use per system call (if possible) */
-  if (connection->read_buffer_offset + MHD_BUF_INC_SIZE >
+  if (connection->read_buffer_offset + connection->daemon->pool_increment >
       connection->read_buffer_size)
     try_grow_read_buffer (connection);
   if (MHD_NO == do_read (connection))

+ 7 - 5
src/microhttpd/daemon.c

@@ -1305,8 +1305,6 @@ MHD_accept_connection (struct MHD_Daemon *daemon)
   struct sockaddr *addr = (struct sockaddr *) &addrstorage;
   socklen_t addrlen;
   int s;
-  int flags;
-  int need_fcntl;
   int fd;
 
   addrlen = sizeof (addrstorage);
@@ -1315,10 +1313,8 @@ MHD_accept_connection (struct MHD_Daemon *daemon)
     return MHD_NO;
 #if HAVE_ACCEPT4
   s = accept4 (fd, addr, &addrlen, SOCK_CLOEXEC);
-  need_fcntl = MHD_NO;
 #else
   s = ACCEPT (fd, addr, &addrlen);
-  need_fcntl = MHD_YES;
 #endif
   if ((-1 == s) || (addrlen <= 0))
     {
@@ -1339,7 +1335,6 @@ MHD_accept_connection (struct MHD_Daemon *daemon)
       return MHD_NO;
     }
 #if !HAVE_ACCEPT4
-  if (MHD_YES == need_fcntl)
   {
     /* make socket non-inheritable */
 #ifdef WINDOWS
@@ -1356,6 +1351,8 @@ MHD_accept_connection (struct MHD_Daemon *daemon)
 #endif
       }
 #else
+    int flags;
+
     flags = fcntl (s, F_GETFD);
     if ( ( (-1 == flags) ||
 	   ( (flags != (flags | FD_CLOEXEC)) &&
@@ -2377,6 +2374,9 @@ parse_options_va (struct MHD_Daemon *daemon,
         case MHD_OPTION_CONNECTION_MEMORY_LIMIT:
           daemon->pool_size = va_arg (ap, size_t);
           break;
+        case MHD_OPTION_CONNECTION_MEMORY_INCREMENT:
+          daemon->pool_increment= va_arg (ap, size_t);
+          break;
         case MHD_OPTION_CONNECTION_LIMIT:
           daemon->max_connections = va_arg (ap, unsigned int);
           break;
@@ -2500,6 +2500,7 @@ parse_options_va (struct MHD_Daemon *daemon,
 		{
 		  /* all options taking 'size_t' */
 		case MHD_OPTION_CONNECTION_MEMORY_LIMIT:
+		case MHD_OPTION_CONNECTION_MEMORY_INCREMENT:
 		case MHD_OPTION_THREAD_STACK_SIZE:
 		  if (MHD_YES != parse_options (daemon,
 						servaddr,
@@ -2786,6 +2787,7 @@ MHD_start_daemon_va (unsigned int flags,
   daemon->default_handler_cls = dh_cls;
   daemon->max_connections = MHD_MAX_CONNECTIONS_DEFAULT;
   daemon->pool_size = MHD_POOL_SIZE_DEFAULT;
+  daemon->pool_increment = MHD_BUF_INC_SIZE;
   daemon->unescape_callback = &MHD_http_unescape;
   daemon->connection_timeout = 0;       /* no timeout */
   daemon->wpipe[0] = -1;

+ 15 - 0
src/microhttpd/internal.h

@@ -47,6 +47,16 @@
 #define MHD_MIN(a,b) ((a)<(b)) ? (a) : (b)
 
 
+/**
+ * Minimum size by which MHD tries to increment read/write buffers.
+ * We usually begin with half the available pool space for the
+ * IO-buffer, but if absolutely needed we additively grow by the
+ * number of bytes given here (up to -- theoretically -- the full pool
+ * space).
+ */
+#define MHD_BUF_INC_SIZE 1024
+
+
 /**
  * Handler for fatal errors.
  */
@@ -1001,6 +1011,11 @@ struct MHD_Daemon
    */
   size_t pool_size;
 
+  /**
+   * Increment for growth of the per-connection memory pools.
+   */
+  size_t pool_increment;
+
   /**
    * Size of threads created by MHD.
    */